""" Interfaz Gradio para el Spanish Menu Translator. """ import gradio as gr from config import MAX_QUEUE_SIZE, SERVER_PORT from core.pipeline import MenuTranslatorPipeline def build_interface(pipeline_instance: MenuTranslatorPipeline) -> gr.Blocks: """Construye y retorna la interfaz Gradio.""" def process_image(image, target_lang, detail_level, include_cultural_notes): if image is None: return "⚠️ Por favor, sube una imagen de menΓΊ.", "{}", "" return pipeline_instance.process_menu( image, target_lang, detail_level, include_cultural_notes ) with gr.Blocks(theme=gr.themes.Soft(), title="Spanish Menu Translator") as demo: gr.Markdown(""" # 🍽️ Spanish Menu Translator & Cultural Guide Upload an image of a Spanish menu to get: - βœ… **Accurate OCR** text extraction - βœ… **Professional translation** to English (or other languages) - βœ… **Cultural descriptions** of each dish **Powered by 3 AI Models:** - πŸ“· OCR: LightOnOCR-2-1B (1.1B params) - 🌐 Translation: NLLB-200 (600M params) - πŸ“ Descriptions: GPT-2 Spanish (355M params) """) with gr.Row(): # --- INPUT --- with gr.Column(scale=1): gr.Markdown("## πŸ“₯ Input") image_input = gr.Image( type="pil", label="Upload Menu Image", sources=["upload", "clipboard", "webcam"], ) with gr.Accordion("βš™οΈ Settings", open=True): target_lang = gr.Dropdown( choices=["English", "French", "German", "Italian"], value="English", label="Target Language", ) detail_level = gr.Slider( minimum=1, maximum=3, value=2, step=1, label="Description Detail Level", info="1=Brief, 2=Medium, 3=Detailed", ) include_cultural = gr.Checkbox( value=True, label="Include Cultural Notes" ) process_btn = gr.Button( "πŸš€ Translate Menu", variant="primary", size="lg" ) # --- OUTPUT --- with gr.Column(scale=1): gr.Markdown("## πŸ“€ Output") with gr.Tabs(): with gr.TabItem("πŸ“„ Translation"): markdown_output = gr.Markdown( label="Translated Menu", value="*Results will appear here...*", ) with gr.TabItem("πŸ“Š Statistics"): stats_output = gr.Code( language="json", label="Processing Statistics", value="{}", ) with gr.TabItem("πŸ” Raw OCR Text"): raw_output = gr.Textbox( label="Extracted Spanish Text", lines=15, value="" ) process_btn.click( fn=process_image, inputs=[image_input, target_lang, detail_level, include_cultural], outputs=[markdown_output, stats_output, raw_output], ) with gr.Accordion("ℹ️ About This Tool", open=False): gr.Markdown(""" ### How it works: 1. **OCR Extraction** (LightOnOCR-2-1B) β€” Extracts text from your menu image (~3-4s) 2. **Translation** (NLLB-200) β€” Translates dish names preserving culinary terminology 3. **Description Generation** (GPT-2 Spanish) β€” Creates cultural context for each dish ### Limitations: - Processing time: ~15-20 seconds - Works best with clear, well-lit images - Maximum 20 dishes per menu ### Privacy: - All processing happens on this server - Images are not stored - No data is sent to external APIs """) return demo def launch(demo: gr.Blocks): """Lanza la aplicaciΓ³n.""" demo.queue(max_size=MAX_QUEUE_SIZE) demo.launch(server_name="0.0.0.0", server_port=SERVER_PORT, share=False)