seawolf2357 commited on
Commit
b18c11c
ยท
verified ยท
1 Parent(s): 75e6b3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -20
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
- # Mathpix์— ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ณด๋‚ด์–ด ์—ฌ๋Ÿฌ ํฌ๋งท์˜ LaTeX ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
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
- # Mathpix API ์š”์ฒญ ๋ฐ”๋””
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 = {f: result.get(f, f"{f} ์ถ”์ถœ ์‹คํŒจ") for f in data["formats"]}
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
- return [latex_result.get(f) for f in "text"] # ์ˆ˜์ •๋œ ๋ถ€๋ถ„
 
 
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
+