import gradio as gr import requests import json import base64 # Mathpix에 이미지 파일을 보내어 LaTeX 문자열을 추출하는 함수 def get_latex_from_image(image): image_base64 = base64.b64encode(image).decode('utf-8') # Mathpix API 요청 헤더 headers = { "app_id": "arxivgpt_2c0986", "app_key": "b5c14c78ea645a6d673195e6360a1cc33ef2bab7a79b90f7cebf6465177171f5", "Content-Type": "application/json" } # Mathpix API 요청 바디 data = { "src": f"data:image/jpeg;base64,{image_base64}", "formats": ["latex_normal"] } # Mathpix API 요청 보내기 response = requests.post("https://api.mathpix.com/v3/latex", headers=headers, json=data) response.raise_for_status() # 요청 실패 시 예외 발생 # 응답에서 LaTeX 추출 result = response.json() latex = result.get('latex_normal', "LaTeX 추출 실패") return latex # Gradio 앱 정의 def build_gradio_app(): with gr.Blocks() as app: with gr.Row(): image_input = gr.Image(type="file", label="이미지 업로드") submit_button = gr.Button("변환하기") latex_output = gr.Textbox(label="추출된 LaTeX") submit_button.click(fn=get_latex_from_image, inputs=image_input, outputs=latex_output) return app # Gradio 앱 실행 if __name__ == "__main__": app = build_gradio_app() app.launch()