seawolf2357 commited on
Commit
25931ce
ยท
verified ยท
1 Parent(s): 6538de8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
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
- image_base64 = base64.b64encode(image).decode('utf-8')
 
 
 
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="filepath", label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ") # 'file' ํƒ€์ž…์„ 'filepath'๋กœ ๋ณ€๊ฒฝ
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()