Created
May 5, 2024 18:29
-
-
Save professorwug/101d9675dce943c12af801a85d28093b to your computer and use it in GitHub Desktop.
Mathpix API for Handwriting to Markdown Converion via Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
import argparse | |
import requests | |
options = { | |
"conversion_formats": {"md": True}, | |
"math_inline_delimiters": ["$", "$"], | |
"rm_spaces": True, | |
"enable_spell_check":True, | |
"improve_mathpix":False, | |
"metadata":{ | |
"improve_mathpix":False, | |
}, | |
} | |
def get_conversation_status(app_id, app_key, pdf_id): | |
status = requests.get(f"https://api.mathpix.com/v3/converter/{pdf_id}", | |
headers={ | |
"app_id": app_id, | |
"app_key": app_key, | |
} ) | |
completed = json.loads(status.text)['status'] | |
return completed | |
def get_processing_status(app_id, app_key, pdf_id): | |
status = requests.get(f"https://api.mathpix.com/v3/pdf/{pdf_id}", | |
headers={ | |
"app_id": app_id, | |
"app_key": app_key, | |
} ) | |
completed = json.loads(status.text)['status'] | |
return completed | |
def main( | |
file_path:str, # pdf file path | |
app_id:str = "mathpix_id", # Mathpix App ID | |
app_key:str = "mathpix_api_key" # mathpix app key | |
): | |
r = requests.post("https://api.mathpix.com/v3/pdf", | |
headers={ | |
"app_id": app_id, | |
"app_key": app_key | |
}, | |
data={ | |
"options_json": json.dumps(options), | |
"metadata": { | |
"improve_mathpix": False | |
}, | |
}, | |
files={ | |
"file": open(file_path,"rb") | |
}, | |
) | |
pdf_id = json.loads(r.text)['pdf_id'] | |
while get_processing_status(app_id, app_key, pdf_id) != 'completed': | |
time.sleep(1) | |
while get_conversation_status(app_id, app_key, pdf_id) != 'completed': | |
time.sleep(1) | |
converted = requests.get(f"https://api.mathpix.com/v3/pdf/{pdf_id}.md", | |
headers={ | |
"app_id": app_id, | |
"app_key": app_key, | |
}) | |
print(converted.text) | |
main("split page path (File Path)", "mathpix_id", "mathpix_api_key") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment