Spaces:
Running
Running
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 LaTeX Tool") | |
gr.Markdown("This comprehensive solution encompasses an automated workflow...") | |
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์ ๋ก๋") | |
submit_button = gr.Button("๋ณํํ๊ธฐ") | |
outputs = gr.Textbox(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 | |
# Examples ์ ์๋ฅผ process_and_output ํจ์ ์ ์ ํ๋ก ์ฎ๊น๋๋ค. | |
examples = gr.Examples(examples=[["ko.png"], ["en.png"], ["hand.jpg"]], inputs=image_input, outputs=outputs, fn=process_and_output) | |
submit_button.click(fn=process_and_output, inputs=image_input, outputs=outputs) | |
demo.add(examples) # gr.Blocks์ examples ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํฉ๋๋ค. | |
return demo | |
if __name__ == "__main__": | |
app = build_gradio_app() | |
app.launch() |