Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,41 +2,57 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import json
|
4 |
import base64
|
|
|
|
|
5 |
|
|
|
6 |
def get_latex_from_image_all_formats(image):
|
7 |
-
|
|
|
|
|
|
|
8 |
|
|
|
9 |
headers = {
|
10 |
"app_id": "arxivgpt_2c0986",
|
11 |
"app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5",
|
12 |
"Content-Type": "application/json"
|
13 |
}
|
14 |
|
|
|
15 |
data = {
|
16 |
"src": f"data:image/jpeg;base64,{image_base64}",
|
17 |
"formats": ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]
|
18 |
}
|
19 |
|
|
|
20 |
response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, json=data)
|
21 |
-
response.raise_for_status()
|
22 |
|
|
|
23 |
result = response.json()
|
24 |
-
formats_results = {f: result.get(f, "์ถ์ถ ์คํจ") for f in data["formats"]}
|
25 |
|
26 |
return formats_results
|
27 |
|
|
|
28 |
def build_gradio_app():
|
29 |
with gr.Blocks() as app:
|
30 |
with gr.Row():
|
31 |
-
image_input = gr.Image(type="
|
32 |
submit_button = gr.Button("๋ณํํ๊ธฐ")
|
|
|
33 |
outputs = [gr.Textbox(label=f"{f} ๊ฒฐ๊ณผ") for f in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]]
|
34 |
-
submit_button.click(fn=get_latex_from_image_all_formats, inputs=[image_input], outputs=outputs)
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
return app
|
39 |
|
|
|
40 |
if __name__ == "__main__":
|
41 |
app = build_gradio_app()
|
42 |
app.launch()
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import base64
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
|
8 |
+
# Mathpix์ ์ด๋ฏธ์ง ํ์ผ์ ๋ณด๋ด์ด ์ฌ๋ฌ ํฌ๋งท์ LaTeX ๋ฌธ์์ด์ ์ถ์ถํ๋ ํจ์
|
9 |
def get_latex_from_image_all_formats(image):
|
10 |
+
# PIL ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํ
|
11 |
+
buffered = io.BytesIO()
|
12 |
+
image.save(buffered, format="JPEG")
|
13 |
+
image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
14 |
|
15 |
+
# Mathpix API ์์ฒญ ํค๋
|
16 |
headers = {
|
17 |
"app_id": "arxivgpt_2c0986",
|
18 |
"app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5",
|
19 |
"Content-Type": "application/json"
|
20 |
}
|
21 |
|
22 |
+
# Mathpix API ์์ฒญ ๋ฐ๋
|
23 |
data = {
|
24 |
"src": f"data:image/jpeg;base64,{image_base64}",
|
25 |
"formats": ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]
|
26 |
}
|
27 |
|
28 |
+
# Mathpix API ์์ฒญ ๋ณด๋ด๊ธฐ
|
29 |
response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, json=data)
|
30 |
+
response.raise_for_status() # ์์ฒญ ์คํจ ์ ์์ธ ๋ฐ์
|
31 |
|
32 |
+
# ์๋ต์์ ๊ฐ ํฌ๋งท์ LaTeX ์ถ์ถ
|
33 |
result = response.json()
|
34 |
+
formats_results = {f: result.get(f, f"{f} ์ถ์ถ ์คํจ") for f in data["formats"]}
|
35 |
|
36 |
return formats_results
|
37 |
|
38 |
+
# Gradio ์ฑ ์ ์
|
39 |
def build_gradio_app():
|
40 |
with gr.Blocks() as app:
|
41 |
with gr.Row():
|
42 |
+
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
43 |
submit_button = gr.Button("๋ณํํ๊ธฐ")
|
44 |
+
|
45 |
outputs = [gr.Textbox(label=f"{f} ๊ฒฐ๊ณผ") for f in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]]
|
|
|
46 |
|
47 |
+
def process_and_output(image):
|
48 |
+
results = get_latex_from_image_all_formats(image)
|
49 |
+
return [results.get(f) for f in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]]
|
50 |
+
|
51 |
+
submit_button.click(fn=process_and_output, inputs=image_input, outputs=outputs)
|
52 |
+
|
53 |
return app
|
54 |
|
55 |
+
# Gradio ์ฑ ์คํ
|
56 |
if __name__ == "__main__":
|
57 |
app = build_gradio_app()
|
58 |
app.launch()
|