Spaces:
Sleeping
Sleeping
| """ | |
| Gradio Interface Module | |
| Defines the web interface layout and interactions | |
| """ | |
| import gradio as gr | |
| from .utils import create_performance_chart, create_metrics_table, get_example | |
| # Custom CSS | |
| CUSTOM_CSS = """ | |
| .gradio-container { | |
| font-family: 'Arial', sans-serif; | |
| } | |
| .header { | |
| text-align: center; | |
| padding: 20px; | |
| background: linear-gradient(90deg, #3498db, #e74c3c); | |
| color: white; | |
| border-radius: 10px; | |
| margin-bottom: 20px; | |
| } | |
| """ | |
| def create_interface(inference_engine): | |
| """ | |
| Create Gradio interface | |
| Args: | |
| inference_engine: QAInference instance | |
| Returns: | |
| Gradio Blocks interface | |
| """ | |
| with gr.Blocks() as demo: | |
| # Header | |
| gr.Markdown(""" | |
| <div class="header"> | |
| <h1>π Multilingual Question Answering System</h1> | |
| <p>Fine-tuned mBART-large with LoRA on SQuAD (English) and XQuAD (German)</p> | |
| <p><i>Supporting English π¬π§ and German π©πͺ</i></p> | |
| </div> | |
| """) | |
| with gr.Tabs(): | |
| # Tab 1: Question Answering | |
| with gr.Tab("β Ask Questions"): | |
| gr.Markdown("""### Enter your question and provide context for the model to extract the answer from: | |
| π‘ Tips for Best Results: | |
| - β Keep context under 300 words | |
| - β Make sure the answer is explicitly stated in the context | |
| - β Use clear, direct questions | |
| - β Avoid questions requiring reasoning across multiple sentences | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| language_choice = gr.Radio( | |
| choices=["English", "German"], | |
| value="English", | |
| label="π Select Language", | |
| info="Choose the language for your question and context" | |
| ) | |
| question_input = gr.Textbox( | |
| label="π Question", | |
| placeholder="Enter your question here...", | |
| lines=2 | |
| ) | |
| context_input = gr.Textbox( | |
| label="π Context", | |
| placeholder="Provide the context/passage containing the answer...", | |
| lines=6 | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("π Get Answer", variant="primary", size="lg") | |
| clear_btn = gr.Button("ποΈ Clear", variant="secondary") | |
| gr.Markdown("### π‘ Try Examples:") | |
| example_type = gr.Radio( | |
| choices=["General Knowledge", "Historical", "Scientific"], | |
| value="General Knowledge", | |
| label="Example Type" | |
| ) | |
| load_example_btn = gr.Button("π₯ Load Example") | |
| with gr.Column(scale=1): | |
| gr.Markdown("### π― Answer") | |
| answer_output = gr.Textbox( | |
| label="Model Answer", | |
| lines=3, | |
| interactive=False | |
| ) | |
| response_details = gr.Markdown("") | |
| # Button actions | |
| submit_btn.click( | |
| fn=inference_engine.answer_question, | |
| inputs=[question_input, context_input, language_choice], | |
| outputs=[answer_output, response_details] | |
| ) | |
| clear_btn.click( | |
| fn=lambda: ("", "", ""), | |
| outputs=[question_input, context_input, answer_output] | |
| ) | |
| load_example_btn.click( | |
| fn=get_example, | |
| inputs=[example_type, language_choice], | |
| outputs=[question_input, context_input] | |
| ) | |
| # Tab 2: Performance Metrics | |
| with gr.Tab("π Performance Metrics"): | |
| gr.Markdown(""" | |
| ### Model Performance Analysis | |
| Evaluation results on SQuAD (English) and XQuAD (German) test sets | |
| """) | |
| performance_plot = gr.Plot( | |
| value=create_performance_chart(), | |
| label="Performance Comparison" | |
| ) | |
| gr.Markdown("### π Detailed Metrics Table") | |
| metrics_df = create_metrics_table() | |
| metrics_table = gr.Dataframe( | |
| value=metrics_df, | |
| label="Performance Metrics by Language" | |
| ) | |
| gr.Markdown(""" | |
| ### π Key Insights | |
| β **German Performance**: 107.2% of English performance (Avg EM+F1) | |
| - BLEU: 43.12 vs 37.79 (+5.33 points) | |
| - F1 Score: 0.6580 vs 0.6329 (+0.025) | |
| - Exact Match: 48.74% vs 43.60% (+5.14%) | |
| β **Strong Transfer Learning**: Model successfully adapted to German with limited data | |
| β **Training Details**: | |
| - Base Model: facebook/mbart-large-50-many-to-many-mmt | |
| - Fine-tuning: LoRA (r=8, alpha=32) | |
| - English Training: 20,000 samples from SQuAD | |
| - German Training: ~950 samples from XQuAD | |
| - Total Training Time: ~2.5 hours on T4 GPU | |
| """) | |
| # Tab 3: About | |
| with gr.Tab("βΉοΈ About"): | |
| gr.Markdown(""" | |
| # Multilingual Question Answering System | |
| ## π― Project Overview | |
| This is a state-of-the-art multilingual question answering system that can extract answers from context in both English and German. | |
| ## π οΈ Architecture | |
| - **Base Model**: mBART-large-50-many-to-many-mmt (610M parameters) | |
| - **Fine-tuning Method**: LoRA (Low-Rank Adaptation) | |
| - **Trainable Parameters**: 1.77M (0.29% of total) | |
| - **Training Data**: | |
| - English: Stanford Question Answering Dataset (SQuAD) | |
| - German: Cross-lingual Question Answering Dataset (XQuAD) | |
| ## π Key Features | |
| - β Bilingual support (English & German) | |
| - β Fast inference (<1 second per query) | |
| - β Memory-efficient with LoRA | |
| - β High accuracy (>0.65 F1 score on both languages) | |
| ## π Performance Highlights | |
| - Achieved 48.74% exact match on German with minimal training data | |
| - BLEU score of 43.12 on German (better than English baseline) | |
| - Successfully demonstrated positive transfer learning across languages | |
| ## β οΈ Known Limitations | |
| - Long contexts (>500 words) may affect performance | |
| - Complex multi-hop reasoning questions may fail | |
| - Limited to extractive QA (answer must be in context) | |
| ## π¨βπ» Author | |
| Praanshull Verma | |
| - GitHub: Praanshull | |
| ## π License | |
| MIT License | |
| """) | |
| # Footer | |
| gr.Markdown(""" | |
| --- | |
| <div style="text-align: center; padding: 10px;"> | |
| <p>Built using HuggingFace Transformers, PEFT, and Gradio</p> | |
| <p><i>Last Updated: December 2025</i></p> | |
| </div> | |
| """) | |
| return demo |