File size: 2,155 Bytes
112423f
5d72210
b8f450e
9a35c66
 
5be7449
 
4fa0559
 
 
 
0350c55
4fa0559
282ba81
b8f450e
f5d8dcc
5be7449
 
b8f450e
f5d8dcc
5d72210
5be7449
b8f450e
112423f
5be7449
b8f450e
 
 
112423f
5be7449
 
 
 
 
 
b8f450e
5be7449
b8f450e
5be7449
b8f450e
5be7449
32eac0f
112423f
 
 
fd7ec7d
0350c55
112423f
5be7449
112423f
5be7449
 
112423f
 
 
 
 
 
 
5be7449
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
import requests
import json
import base64


def get_latex_from_image_all_formats(image_path):
    # ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ฐ”์ดํŠธ ํ˜•์‹์œผ๋กœ ์ฝ์Œ
    with open(image_path, "rb") as image_file:
        image_bytes = image_file.read()
    
    # ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ base64 ์ธ์ฝ”๋”ฉ
    image_base64 = base64.b64encode(image_bytes).decode('utf-8')

    # Mathpix API ์š”์ฒญ ํ—ค๋”
    headers = {
        "app_id": "arxivgpt_2c0986",  # ์‹ค์ œ ์‚ฌ์šฉํ•˜๋Š” app_id๋กœ ๋Œ€์ฒด
        "app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5",  # ์‹ค์ œ ์‚ฌ์šฉํ•˜๋Š” app_key๋กœ ๋Œ€์ฒด
        "Content-Type": "application/json"
    }

    # ๋ชจ๋“  ํฌ๋งท์„ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ
    data = {
        "src": f"data:image/jpeg;base64,{image_base64}",
        "formats": ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]
    }

    # Mathpix API ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
    response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, json=data)
    
    # ์š”์ฒญ์— ์‹คํŒจํ•œ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
    try:
        response.raise_for_status()
    except requests.HTTPError as e:
        return {"error": f"Mathpix API ์š”์ฒญ ์‹คํŒจ: {e}"}

    # ๊ฐ ํฌ๋งท์— ๋”ฐ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ
    result = response.json()
    results_by_format = {format_: result.get(format_, "์ถ”์ถœ ์‹คํŒจ") for format_ in data["formats"]}

    return results_by_format

# Gradio ์•ฑ ์ •์˜
def build_gradio_app():
    with gr.Blocks() as app:
        with gr.Row():
            image_input = gr.Image(type="filepath", label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
            submit_button = gr.Button("๋ณ€ํ™˜ํ•˜๊ธฐ")
        outputs = {format_: gr.Textbox(label=f"{format_} ๊ฒฐ๊ณผ") for format_ in ["text", "latex_styled", "latex_normal", "latex_list", "latex_simplified", "asciimath", "mathml"]}

        # ๋ณ€ํ™˜ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์ฒ˜๋ฆฌ
        submit_button.click(fn=get_latex_from_image_all_formats, inputs=image_input, outputs=outputs)

    return app

# Gradio ์•ฑ ์‹คํ–‰
if __name__ == "__main__":
    app = build_gradio_app()
    app.launch()