ocrlatex / app.py
seawolf2357's picture
Update app.py
5d72210 verified
raw
history blame
1.14 kB
import gradio as gr
import requests
import urllib.parse
def latex_to_image_via_api(latex_code):
encoded_latex = urllib.parse.quote(latex_code) # LaTeX ์ฝ”๋“œ๋ฅผ URL ์ธ์ฝ”๋”ฉ
api_url = f"https://latex.codecogs.com/png.download?{encoded_latex}" # URL ์ƒ์„ฑ
response = requests.get(api_url) # API ์š”์ฒญ
if response.status_code == 200:
# ์‘๋‹ต์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ž„์‹œ ํŒŒ์ผ์— ์ €์žฅ
with open("latex_image.png", "wb") as f:
f.write(response.content) # ์ด๋ฏธ์ง€ ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์“ด๋‹ค
return "latex_image.png" # ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
else:
return "LaTeX ๋ณ€ํ™˜ ์‹คํŒจ: API ํ˜ธ์ถœ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
iface = gr.Interface(fn=latex_to_image_via_api,
inputs=gr.Textarea(placeholder="LaTeX ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”..."),
outputs=gr.Image(label="๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€"),
title="LaTeX ์ฝ”๋“œ๋ฅผ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜",
description="Codecogs LaTeX Rendering API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ LaTeX ์ฝ”๋“œ๋ฅผ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
iface.launch()