Spaces:
Running
Running
import gradio as gr | |
import requests | |
import json | |
import base64 | |
def get_latex_from_image_all_formats(image_path): | |
# ํ์ผ ๊ฒฝ๋ก์์ ์ด๋ฏธ์ง ํ์ผ์ ๋ฐ์ดํธ ํ์์ผ๋ก ์ฝ์ | |
with open(image_path, "rb") as image_file: | |
image_bytes = image_file.read() | |
# ์ด๋ฏธ์ง ํ์ผ์ base64 ์ธ์ฝ๋ฉ | |
image_base64 = base64.b64encode(image_bytes).decode('utf-8') | |
# Mathpix API ์์ฒญ ํค๋ | |
headers = { | |
"app_id": "arxivgpt_2c0986", # ์ค์ ์ฌ์ฉํ๋ app_id๋ก ๋์ฒด | |
"app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5", # ์ค์ ์ฌ์ฉํ๋ app_key๋ก ๋์ฒด | |
"Content-Type": "application/json" | |
} | |
# ๋ชจ๋ ํฌ๋งท์ ํฌํจํ๋ ๋ฐ์ดํฐ | |
data = { | |
"src": f"data:image/jpeg;base64,{image_base64}", | |
"formats": ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"] | |
} | |
# Mathpix API ์์ฒญ ๋ณด๋ด๊ธฐ | |
response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, json=data) | |
# ์์ฒญ์ ์คํจํ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ | |
try: | |
response.raise_for_status() | |
except requests.HTTPError as e: | |
return {"error": f"Mathpix API ์์ฒญ ์คํจ: {e}"} | |
# ๊ฐ ํฌ๋งท์ ๋ฐ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋ ๋์ ๋๋ฆฌ | |
result = response.json() | |
results_by_format = {format_: result.get(format_, "์ถ์ถ ์คํจ") for format_ in data["formats"]} | |
return results_by_format | |
# Gradio ์ฑ ์ ์ | |
def build_gradio_app(): | |
with gr.Blocks() as app: | |
with gr.Row(): | |
image_input = gr.Image(type="filepath", label="์ด๋ฏธ์ง ์ ๋ก๋") | |
submit_button = gr.Button("๋ณํํ๊ธฐ") | |
outputs = {format_: gr.Textbox(label=f"{format_} ๊ฒฐ๊ณผ") for format_ in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]} | |
# ๋ณํ ๋ฒํผ์ ๋๋ ์ ๋ ์ฒ๋ฆฌ | |
submit_button.click(fn=get_latex_from_image_all_formats, inputs=image_input, outputs=outputs) | |
return app | |
# Gradio ์ฑ ์คํ | |
if __name__ == "__main__": | |
app = build_gradio_app() | |
app.launch() | |