seawolf2357 commited on
Commit
f5d8dcc
ยท
verified ยท
1 Parent(s): a812288

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -1,25 +1,29 @@
1
  import gradio as gr
2
  import requests
3
- import urllib.parse
4
 
5
- def latex_to_image_via_api(latex_code):
6
- encoded_latex = urllib.parse.quote(latex_code) # LaTeX ์ฝ”๋“œ๋ฅผ URL ์ธ์ฝ”๋”ฉ
7
- api_url = f"https://latex.codecogs.com/png.download?{encoded_latex}" # URL ์ƒ์„ฑ
8
-
9
- response = requests.get(api_url) # API ์š”์ฒญ
 
 
 
 
 
 
 
 
 
 
10
 
11
- if response.status_code == 200:
12
- # ์‘๋‹ต์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ž„์‹œ ํŒŒ์ผ์— ์ €์žฅ
13
- with open("latex_image.png", "wb") as f:
14
- f.write(response.content) # ์ด๋ฏธ์ง€ ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์“ด๋‹ค
15
- return "latex_image.png" # ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
16
- else:
17
- return "LaTeX ๋ณ€ํ™˜ ์‹คํŒจ: API ํ˜ธ์ถœ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
 
18
 
19
- iface = gr.Interface(fn=latex_to_image_via_api,
20
- inputs=gr.TextArea(placeholder="LaTeX ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”..."),
21
- outputs=gr.Image(label="๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€"),
22
- title="LaTeX ์ฝ”๋“œ๋ฅผ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜",
23
- description="Codecogs LaTeX Rendering API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ LaTeX ์ฝ”๋“œ๋ฅผ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
24
-
25
- iface.launch()
 
1
  import gradio as gr
2
  import requests
 
3
 
4
+ # Mathpix ์š”์ฒญ์„ ์œ„ํ•œ ํ•จ์ˆ˜
5
+ def process_image_to_latex(file_path):
6
+ headers = {
7
+ "app_id": "arxivgpt_2c0986",
8
+ "app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5",
9
+ "Content-type": "application/json"
10
+ }
11
+ with open(file_path, "rb") as file:
12
+ img_data = file.read()
13
+ response = requests.post(
14
+ "https://api.mathpix.com/v3/latex",
15
+ headers=headers,
16
+ json={"src": "data:image/jpeg;base64," + base64.b64encode(img_data).decode()}
17
+ )
18
+ return response.json()["latex"]
19
 
20
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
21
+ def create_gradio_interface():
22
+ with gr.Blocks() as demo:
23
+ gr.Markdown("## LaTeX ์ถ”์ถœ๊ธฐ")
24
+ file_input = gr.File(label="์ด๋ฏธ์ง€ ๋˜๋Š” PDF ํŒŒ์ผ ์—…๋กœ๋“œ")
25
+ latex_output = gr.Textbox(label="์ถ”์ถœ๋œ LaTeX")
26
+ file_input.change(fn=process_image_to_latex, inputs=[file_input], outputs=[latex_output])
27
+ demo.launch()
28
 
29
+ create_gradio_interface()