"""
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("""
""")
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("""
---
Built with â¤ī¸ using HuggingFace Transformers, PEFT, and Gradio
Last Updated: December 2025
""")
return demo