Spaces:
Running
Running
import gradio as gr | |
import requests | |
import json | |
import base64 | |
# Mathpix์ ์ด๋ฏธ์ง ํ์ผ์ ๋ณด๋ด์ด LaTeX ๋ฌธ์์ด์ ์ถ์ถํ๋ ํจ์ | |
def get_latex_from_image(image): | |
image_base64 = base64.b64encode(image).decode('utf-8') | |
# Mathpix API ์์ฒญ ํค๋ | |
headers = { | |
"app_id": "arxivgpt_2c0986", | |
"app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5", | |
"Content-Type": "application/json" | |
} | |
# Mathpix API ์์ฒญ ๋ฐ๋ | |
data = { | |
"src": f"data:image/jpeg;base64,{image_base64}", | |
"formats": ["latex_normal"] | |
} | |
# Mathpix API ์์ฒญ ๋ณด๋ด๊ธฐ | |
response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, json=data) | |
response.raise_for_status() # ์์ฒญ ์คํจ ์ ์์ธ ๋ฐ์ | |
# ์๋ต์์ LaTeX ์ถ์ถ | |
result = response.json() | |
latex = result.get('latex_normal', "LaTeX ์ถ์ถ ์คํจ") | |
return latex | |
# Gradio ์ฑ ์ ์ | |
def build_gradio_app(): | |
with gr.Blocks() as app: | |
with gr.Row(): | |
image_input = gr.Image(type="file", label="์ด๋ฏธ์ง ์ ๋ก๋") | |
submit_button = gr.Button("๋ณํํ๊ธฐ") | |
latex_output = gr.Textbox(label="์ถ์ถ๋ LaTeX") | |
submit_button.click(fn=get_latex_from_image, inputs=image_input, outputs=latex_output) | |
return app | |
# Gradio ์ฑ ์คํ | |
if __name__ == "__main__": | |
app = build_gradio_app() | |
app.launch() | |