Spaces:
Running
Running
File size: 2,342 Bytes
112423f 5d72210 b8f450e 9a35c66 25931ce 9a35c66 015da21 25931ce 3bd25bb 25931ce 282ba81 25931ce f5d8dcc 015da21 b8f450e f5d8dcc 5d72210 25931ce b8f450e 112423f 5be7449 b8f450e 25931ce 0e89256 25931ce 3bd25bb 25931ce b8f450e 25931ce b8f450e 3bd25bb 32eac0f 58a27cd 112423f 58a27cd fd7ec7d 25931ce 112423f 58a27cd 6538de8 112423f 015da21 58a27cd 25931ce 58a27cd 25931ce 58a27cd 112423f 58a27cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import gradio as gr
import requests
import json
import base64
from PIL import Image
import io
app_id = os.getenv("app_id")
app_key = os.getenv("app_key")
app_url = os.getenv("app_url")
# Mathpix์ ์ด๋ฏธ์ง ํ์ผ์ ๋ณด๋ด์ด ์ฌ๋ฌ ํฌ๋งท์ LaTeX ๋ฌธ์์ด์ ์ถ์ถํ๋ ํจ์
def get_latex_from_image_all_formats(image):
# PIL ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํ
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
# Mathpix API ์์ฒญ ํค๋
headers = {
"app_id": app_id,
"app_key": app_key,
"Content-Type": "application/json"
}
# Mathpix API ์์ฒญ ๋ฐ๋
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/text", headers=headers, json=data)
response.raise_for_status() # ์์ฒญ ์คํจ ์ ์์ธ ๋ฐ์
# ์๋ต์์ ๊ฐ ํฌ๋งท์ LaTeX ์ถ์ถ
result = response.json()
formats_results = {f: result.get(f, f"{f} ์ถ์ถ ์คํจ") for f in data["formats"]}
return formats_results
def build_gradio_app():
with gr.Blocks() as demo:
with gr.Row():
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
submit_button = gr.Button("๋ณํํ๊ธฐ")
outputs = [gr.Textbox(label=f"{f} ๊ฒฐ๊ณผ") for f in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]]
latex_iframe = gr.HTML(value='<iframe src="https://www.mathjax.org/#demo" style="width: 100%; height: 700px; border: 2px solid #007bff; border-radius: 8px;"></iframe>', elem_id="latex_iframe")
def process_and_output(image):
latex_result = get_latex_from_image_all_formats(image) # ๋ณ์๋ช
์์
return [latex_result.get(f) for f in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]] # ์์ ๋ ๋ถ๋ถ
submit_button.click(fn=process_and_output, inputs=image_input, outputs=outputs)
return demo
if __name__ == "__main__":
app = build_gradio_app()
app.launch() |