Spaces:
Running
Running
File size: 2,308 Bytes
112423f 5d72210 b8f450e 9a35c66 25931ce 504e313 9a35c66 18e6128 b18c11c 015da21 b18c11c 3bd25bb 25931ce 282ba81 f5d8dcc 015da21 b8f450e f5d8dcc 5d72210 b18c11c b8f450e 112423f b18c11c b8f450e c8dcc39 b18c11c 3bd25bb b8f450e b18c11c 6b7accd 32eac0f b18c11c a15d0f2 e54f391 9cb8f75 6b7accd e5c94e2 25931ce a15d0f2 b18c11c 6b7accd a15d0f2 25931ce e6c3cb6 25931ce 58a27cd 112423f 6b7accd |
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 61 62 63 64 65 66 67 68 69 70 71 |
import gradio as gr
import requests
import json
import base64
from PIL import Image
import io
import os
css = """
footer {
visibility: hidden;
}
"""
app_id = os.getenv("app_id")
app_key = os.getenv("app_key")
app_url = os.getenv("app_url")
def get_latex_from_image_all_formats(image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
headers = {
"app_id": app_id,
"app_key": app_key,
"Content-Type": "application/json"
}
# ํด๋น ๋ถ๋ถ์ ["text"]๋ก ๋ณ๊ฒฝํ์ฌ formats๋ฅผ ๋ฆฌ์คํธ๋ก ์ค์
data = {
"src": f"data:image/jpeg;base64,{image_base64}",
"formats": ["text"]
}
response = requests.post(app_url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
# formats_results ๋ณ์๋ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ์ ๊ฑฐํ์ฌ ์ฝ๋๋ฅผ ๋จ์ํ
return result
def build_gradio_app(css=css):
with gr.Blocks(css=css) as demo:
gr.Markdown("# ArXivGPT OCR LaTeX")
gr.Markdown("๋ณํํ๊ธฐ ๋ฒํผ ๋๋ฅด๊ณ ์ถ๋ ฅ๋ ๊ฒฐ๊ณผ๊ฐ์ '๋ณต์ฌ'ํ์ฌ ํ๋ฉด ์๋ ๋ฐ์ค('ํ
์คํธ ์ง์ฐ๊ณ ๋น ์ํ๋ก')์ ๋ถ์ฌ๋ฃ๊ณ 'Render HTML'๋ฒํผ์ ๋๋ฅด์ธ์.")
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
def process_and_output(image):
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ฐ ๊ฒฐ๊ณผ ๋ฐํ ๋ก์ง
latex_result = get_latex_from_image_all_formats(image)
text_result = latex_result.get("text", "No result")
return text_result
submit_button = gr.Button("๋ณํํ๊ธฐ")
outputs = gr.Textbox(label="๊ฒฐ๊ณผ")
submit_button.click(fn=process_and_output, inputs=[image_input], outputs=[outputs])
examples = gr.Examples(examples=[["ko.png"], ["en.png"], ["hand.jpg"]], inputs=[image_input], fn=process_and_output, outputs=[outputs])
latex_iframe = gr.HTML(value='<iframe src="https://mathjax.github.io/MathJax-demos-web/input-tex_mml2chtml.html" style="width: 100%; height: 700px; border: 2px solid #007bff; border-radius: 8px;"></iframe>', elem_id="latex_iframe")
return demo
if __name__ == "__main__":
app = build_gradio_app()
app.launch() |