latex / app.py
arxivgpt kim
Update app.py
6503ea6 verified
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"], ["qq1.jpg"], ["qq2.png"], ["qq3.jpg"], ["qq4.png"], ["qq5.png"]], 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()