seawolf2357 commited on
Commit
5be7449
ยท
verified ยท
1 Parent(s): 4fa0559

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -3,8 +3,8 @@ import requests
3
  import json
4
  import base64
5
 
6
- # Mathpix์— ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ณด๋‚ด์–ด LaTeX ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
7
- def get_latex_from_image(image_path):
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
- # Mathpix API ์š”์ฒญ ๋ฐ”๋””
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
- response.raise_for_status() # ์š”์ฒญ ์‹คํŒจ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
 
 
 
 
 
31
 
32
- # ์‘๋‹ต์—์„œ LaTeX ์ถ”์ถœ
33
  result = response.json()
34
- latex = result.get('latex_normal', "LaTeX ์ถ”์ถœ ์‹คํŒจ")
35
 
36
- return latex
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
- latex_output = gr.Textbox(label="์ถ”์ถœ๋œ LaTeX")
45
 
46
- submit_button.click(fn=get_latex_from_image, inputs=image_input, outputs=latex_output)
 
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
+