Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,8 @@ import requests
|
|
3 |
import json
|
4 |
import base64
|
5 |
|
6 |
-
|
7 |
-
def
|
8 |
# ํ์ผ ๊ฒฝ๋ก์์ ์ด๋ฏธ์ง ํ์ผ์ ๋ฐ์ดํธ ํ์์ผ๋ก ์ฝ์
|
9 |
with open(image_path, "rb") as image_file:
|
10 |
image_bytes = image_file.read()
|
@@ -14,26 +14,31 @@ def get_latex_from_image(image_path):
|
|
14 |
|
15 |
# Mathpix API ์์ฒญ ํค๋
|
16 |
headers = {
|
17 |
-
"app_id": "arxivgpt_2c0986",
|
18 |
-
"app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5",
|
19 |
"Content-Type": "application/json"
|
20 |
}
|
21 |
|
22 |
-
#
|
23 |
data = {
|
24 |
"src": f"data:image/jpeg;base64,{image_base64}",
|
25 |
-
"formats": ["latex_normal"]
|
26 |
}
|
27 |
|
28 |
# Mathpix API ์์ฒญ ๋ณด๋ด๊ธฐ
|
29 |
response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, json=data)
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
#
|
33 |
result = response.json()
|
34 |
-
|
35 |
|
36 |
-
return
|
37 |
|
38 |
# Gradio ์ฑ ์ ์
|
39 |
def build_gradio_app():
|
@@ -41,9 +46,10 @@ def build_gradio_app():
|
|
41 |
with gr.Row():
|
42 |
image_input = gr.Image(type="filepath", label="์ด๋ฏธ์ง ์
๋ก๋")
|
43 |
submit_button = gr.Button("๋ณํํ๊ธฐ")
|
44 |
-
|
45 |
|
46 |
-
|
|
|
47 |
|
48 |
return app
|
49 |
|
@@ -51,3 +57,4 @@ def build_gradio_app():
|
|
51 |
if __name__ == "__main__":
|
52 |
app = build_gradio_app()
|
53 |
app.launch()
|
|
|
|
3 |
import json
|
4 |
import base64
|
5 |
|
6 |
+
|
7 |
+
def get_latex_from_image_all_formats(image_path):
|
8 |
# ํ์ผ ๊ฒฝ๋ก์์ ์ด๋ฏธ์ง ํ์ผ์ ๋ฐ์ดํธ ํ์์ผ๋ก ์ฝ์
|
9 |
with open(image_path, "rb") as image_file:
|
10 |
image_bytes = image_file.read()
|
|
|
14 |
|
15 |
# Mathpix API ์์ฒญ ํค๋
|
16 |
headers = {
|
17 |
+
"app_id": "arxivgpt_2c0986", # ์ค์ ์ฌ์ฉํ๋ app_id๋ก ๋์ฒด
|
18 |
+
"app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5", # ์ค์ ์ฌ์ฉํ๋ app_key๋ก ๋์ฒด
|
19 |
"Content-Type": "application/json"
|
20 |
}
|
21 |
|
22 |
+
# ๋ชจ๋ ํฌ๋งท์ ํฌํจํ๋ ๋ฐ์ดํฐ
|
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 |
+
|
31 |
+
# ์์ฒญ์ ์คํจํ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
|
32 |
+
try:
|
33 |
+
response.raise_for_status()
|
34 |
+
except requests.HTTPError as e:
|
35 |
+
return {"error": f"Mathpix API ์์ฒญ ์คํจ: {e}"}
|
36 |
|
37 |
+
# ๊ฐ ํฌ๋งท์ ๋ฐ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋ ๋์
๋๋ฆฌ
|
38 |
result = response.json()
|
39 |
+
results_by_format = {format_: result.get(format_, "์ถ์ถ ์คํจ") for format_ in data["formats"]}
|
40 |
|
41 |
+
return results_by_format
|
42 |
|
43 |
# Gradio ์ฑ ์ ์
|
44 |
def build_gradio_app():
|
|
|
46 |
with gr.Row():
|
47 |
image_input = gr.Image(type="filepath", label="์ด๋ฏธ์ง ์
๋ก๋")
|
48 |
submit_button = gr.Button("๋ณํํ๊ธฐ")
|
49 |
+
outputs = {format_: gr.Textbox(label=f"{format_} ๊ฒฐ๊ณผ") for format_ in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]}
|
50 |
|
51 |
+
# ๋ณํ ๋ฒํผ์ ๋๋ ์ ๋ ์ฒ๋ฆฌ
|
52 |
+
submit_button.click(fn=get_latex_from_image_all_formats, inputs=image_input, outputs=outputs)
|
53 |
|
54 |
return app
|
55 |
|
|
|
57 |
if __name__ == "__main__":
|
58 |
app = build_gradio_app()
|
59 |
app.launch()
|
60 |
+
|