Skip to content

Commit 4214f89

Browse files
committed
mathpix call pdf-to-mmd; mathpix llm sandbox mmd-to-json
1 parent 39c852a commit 4214f89

File tree

3 files changed

+1189
-1
lines changed

3 files changed

+1189
-1
lines changed

wizard/mathpix_call.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import requests
2+
import json
3+
import os
4+
import time
5+
from dotenv import load_dotenv
6+
7+
# Load API keys from .env file
8+
load_dotenv()
9+
MATHPIX_API_KEY = os.getenv("MATHPIX_API_KEY")
10+
MATHPIX_APP_ID = os.getenv("MATHPIX_APP_ID")
11+
12+
with open("wizard/pastpapers/2024_paper.pdf", "rb") as file:
13+
r = requests.post(
14+
"https://api.mathpix.com/v3/pdf",
15+
headers={
16+
"app_id": MATHPIX_APP_ID,
17+
"app_key": MATHPIX_API_KEY,
18+
},
19+
files={"file": file},
20+
)
21+
pdf_id = r.json()["pdf_id"]
22+
print("PDF ID:", pdf_id)
23+
print("Response:", r.json())
24+
25+
url = f"https://api.mathpix.com/v3/pdf/{pdf_id}.mmd"
26+
headers = {
27+
"app_id": MATHPIX_APP_ID,
28+
"app_key": MATHPIX_API_KEY,
29+
}
30+
31+
max_retries = 10
32+
retry_delay = 5 # seconds
33+
for attempt in range(max_retries):
34+
response = requests.get(url, headers=headers)
35+
if response.status_code == 200:
36+
# Save the result if the request is successful
37+
with open(f"{pdf_id}.mmd", "w") as f:
38+
f.write(response.text)
39+
print("Downloaded MMD successfully.")
40+
break
41+
else:
42+
print(f"Attempt {attempt + 1}/{max_retries}: Processing not complete. Retrying in {retry_delay} seconds...")
43+
time.sleep(retry_delay)
44+
else:
45+
print("Failed to retrieve processed PDF after multiple attempts:", response.status_code, response.text)

wizard/sandbox.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@
841841
"name": "python",
842842
"nbconvert_exporter": "python",
843843
"pygments_lexer": "ipython3",
844-
"version": "3.12.8"
844+
"version": "3.12.9"
845845
}
846846
},
847847
"nbformat": 4,

0 commit comments

Comments
 (0)