Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,55 +6,55 @@ from PIL import Image
|
|
6 |
import io
|
7 |
import os
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
app_id = os.getenv("app_id")
|
10 |
app_key = os.getenv("app_key")
|
11 |
app_url = os.getenv("app_url")
|
12 |
|
13 |
-
|
14 |
def get_latex_from_image_all_formats(image):
|
15 |
-
# PIL ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํ
|
16 |
buffered = io.BytesIO()
|
17 |
image.save(buffered, format="JPEG")
|
18 |
image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
19 |
|
20 |
-
# Mathpix API ์์ฒญ ํค๋
|
21 |
headers = {
|
22 |
"app_id": app_id,
|
23 |
"app_key": app_key,
|
24 |
"Content-Type": "application/json"
|
25 |
}
|
26 |
|
27 |
-
#
|
28 |
data = {
|
29 |
"src": f"data:image/jpeg;base64,{image_base64}",
|
30 |
-
"formats": "text"
|
31 |
}
|
32 |
|
33 |
-
# Mathpix API ์์ฒญ ๋ณด๋ด๊ธฐ
|
34 |
response = requests.post(app_url, headers=headers, json=data)
|
35 |
-
response.raise_for_status()
|
36 |
|
37 |
-
# ์๋ต์์ ๊ฐ ํฌ๋งท์ LaTeX ์ถ์ถ
|
38 |
result = response.json()
|
39 |
-
formats_results
|
40 |
-
|
41 |
-
return formats_results
|
42 |
|
43 |
-
|
44 |
-
def build_gradio_app():
|
45 |
with gr.Blocks() as demo:
|
46 |
with gr.Row():
|
47 |
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
48 |
submit_button = gr.Button("๋ณํํ๊ธฐ")
|
49 |
-
|
50 |
-
outputs = [gr.Textbox(label=f"{f} ๊ฒฐ๊ณผ") for f in "text"]
|
51 |
-
|
52 |
-
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")
|
53 |
|
|
|
|
|
54 |
|
55 |
def process_and_output(image):
|
56 |
-
latex_result = get_latex_from_image_all_formats(image)
|
57 |
-
|
|
|
|
|
58 |
|
59 |
submit_button.click(fn=process_and_output, inputs=image_input, outputs=outputs)
|
60 |
|
@@ -62,4 +62,5 @@ def build_gradio_app():
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
app = build_gradio_app()
|
65 |
-
app.launch()
|
|
|
|
6 |
import io
|
7 |
import os
|
8 |
|
9 |
+
css_style = """
|
10 |
+
footer {
|
11 |
+
visibility: hidden;
|
12 |
+
}
|
13 |
+
"""
|
14 |
+
|
15 |
app_id = os.getenv("app_id")
|
16 |
app_key = os.getenv("app_key")
|
17 |
app_url = os.getenv("app_url")
|
18 |
|
19 |
+
|
20 |
def get_latex_from_image_all_formats(image):
|
|
|
21 |
buffered = io.BytesIO()
|
22 |
image.save(buffered, format="JPEG")
|
23 |
image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
24 |
|
|
|
25 |
headers = {
|
26 |
"app_id": app_id,
|
27 |
"app_key": app_key,
|
28 |
"Content-Type": "application/json"
|
29 |
}
|
30 |
|
31 |
+
# ํด๋น ๋ถ๋ถ์ ["text"]๋ก ๋ณ๊ฒฝํ์ฌ formats๋ฅผ ๋ฆฌ์คํธ๋ก ์ค์
|
32 |
data = {
|
33 |
"src": f"data:image/jpeg;base64,{image_base64}",
|
34 |
+
"formats": ["text"]
|
35 |
}
|
36 |
|
|
|
37 |
response = requests.post(app_url, headers=headers, json=data)
|
38 |
+
response.raise_for_status()
|
39 |
|
|
|
40 |
result = response.json()
|
41 |
+
# formats_results ๋ณ์๋ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ์ ๊ฑฐํ์ฌ ์ฝ๋๋ฅผ ๋จ์ํ
|
42 |
+
return result
|
|
|
43 |
|
44 |
+
def build_gradio_app(css=css):
|
|
|
45 |
with gr.Blocks() as demo:
|
46 |
with gr.Row():
|
47 |
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
48 |
submit_button = gr.Button("๋ณํํ๊ธฐ")
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
# ๋จ์ผ ์ถ๋ ฅ์ ์ํด outputs ๋ถ๋ถ์ ๋จ์ํ
|
51 |
+
outputs = gr.Textbox(label="๊ฒฐ๊ณผ")
|
52 |
|
53 |
def process_and_output(image):
|
54 |
+
latex_result = get_latex_from_image_all_formats(image)
|
55 |
+
# text ๊ฒฐ๊ณผ๋ฅผ ์ง์ ๋ฐํ
|
56 |
+
text_result = latex_result.get("text", "No result")
|
57 |
+
return text_result
|
58 |
|
59 |
submit_button.click(fn=process_and_output, inputs=image_input, outputs=outputs)
|
60 |
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
app = build_gradio_app()
|
65 |
+
app.launch() # API ํต์ ์ ํ
์คํธํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
|
66 |
+
|