ocrlatex / app.py
seawolf2357's picture
Update app.py
b8f450e verified
raw
history blame
1.17 kB
import requests
import json
import base64
# Mathpix์— ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ณด๋‚ด์–ด LaTeX ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
def get_latex_from_image(image_path):
# ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ base64 ์ธ์ฝ”๋”ฉ
with open(image_path, "rb") as f:
image_base64 = base64.b64encode(f.read()).decode()
# Mathpix API ์š”์ฒญ ํ—ค๋”
headers = {
"app_id": "arxivgpt_2c0986", # ์—ฌ๊ธฐ์— Mathpix ID๋ฅผ ์ž…๋ ฅ
"app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5", # ์—ฌ๊ธฐ์— Mathpix API ํ‚ค๋ฅผ ์ž…๋ ฅ
"Content-Type": "application/json"
}
# Mathpix API ์š”์ฒญ ๋ฐ”๋””
data = {
"src": "data:image/png;base64," + image_base64,
"formats": ["latex_normal"]
}
# Mathpix API ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, data=json.dumps(data))
response.raise_for_status() # ์š”์ฒญ ์‹คํŒจ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
# ์‘๋‹ต์—์„œ LaTeX ์ถ”์ถœ
result = response.json()
latex = result['latex_normal']
return latex
# ์ถ”์ถœํ•œ LaTeX ์ถœ๋ ฅ
latex_string = get_latex_from_image("path/to/image.png")
print(latex_string)