File size: 10,954 Bytes
72f90b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import gradio as gr
from backend.language_detector import LanguageDetector

def main():
    # Initialize the language detector with default model (Model A Dataset A)
    detector = LanguageDetector()
    
    # Create Gradio interface
    with gr.Blocks(title="Language Detection App", theme=gr.themes.Soft()) as app:
        gr.Markdown("# 🌍 Language Detection App")
        gr.Markdown("Select a model and enter text below to detect its language with confidence scores.")
        
        # Model Selection Section with visual styling
        with gr.Group():
            gr.Markdown(
                "<div style='text-align: center; padding: 16px 0 8px 0; margin-bottom: 16px; font-size: 18px; font-weight: 600; border-bottom: 2px solid; background: linear-gradient(90deg, transparent, rgba(99, 102, 241, 0.1), transparent); border-radius: 8px 8px 0 0;'>🤖 Model Selection</div>"
            )
            
            # Get available models
            available_models = detector.get_available_models()
            model_choices = []
            model_info_map = {}
            
            for key, info in available_models.items():
                if info["status"] == "available":
                    model_choices.append((info["display_name"], key))
                else:
                    model_choices.append((f"{info['display_name']} (Coming Soon)", key))
                model_info_map[key] = info
            
            model_selector = gr.Dropdown(
                choices=model_choices,
                value="model-a-dataset-a",  # Default to Model A Dataset A
                label="Choose Language Detection Model",
                interactive=True
            )
            
            # Model Information Display
            model_info_display = gr.Markdown(
                value=_format_model_info(detector.get_current_model_info()),
                label="Model Information"
            )
        
        # Add visual separator
        gr.Markdown(
            "<div style='margin: 24px 0; border-top: 3px solid rgba(99, 102, 241, 0.2); background: linear-gradient(90deg, transparent, rgba(99, 102, 241, 0.05), transparent); height: 2px;'></div>"
        )
        
        # Analysis Section
        with gr.Group():
            gr.Markdown(
                "<div style='text-align: center; padding: 16px 0 8px 0; margin-bottom: 16px; font-size: 18px; font-weight: 600; border-bottom: 2px solid; background: linear-gradient(90deg, transparent, rgba(34, 197, 94, 0.1), transparent); border-radius: 8px 8px 0 0;'>🔍 Language Analysis</div>"
            )
            
            with gr.Row():
                with gr.Column(scale=2):
                    # Input section
                    text_input = gr.Textbox(
                        label="Text to Analyze",
                        placeholder="Enter text here to detect its language...",
                        lines=5,
                        max_lines=10
                    )
                    
                    detect_btn = gr.Button("🔍 Detect Language", variant="primary", size="lg")
                    
                    # Example texts
                    gr.Examples(
                        examples=[
                            ["Hello, how are you today?"],
                            ["Bonjour, comment allez-vous?"],
                            ["Hola, ¿cómo estás?"],
                            ["Guten Tag, wie geht es Ihnen?"],
                            ["こんにちは、元気ですか?"],
                            ["Привет, как дела?"],
                            ["Ciao, come stai?"],
                            ["Olá, como você está?"],
                            ["你好,你好吗?"],
                            ["안녕하세요, 어떻게 지내세요?"]
                        ],
                        inputs=text_input,
                        label="Try these examples:"
                    )
                
                with gr.Column(scale=2):
                    # Output section
                    with gr.Group():
                        gr.Markdown(
                            "<div style='text-align: center; padding: 16px 0 8px 0; margin-bottom: 12px; font-size: 18px; font-weight: 600; border-bottom: 2px solid; background: linear-gradient(90deg, transparent, rgba(168, 85, 247, 0.1), transparent); border-radius: 8px 8px 0 0;'>📊 Detection Results</div>"
                        )
                        
                        detected_language = gr.Textbox(
                            label="Detected Language",
                            interactive=False
                        )
                        
                        confidence_score = gr.Number(
                            label="Confidence Score",
                            interactive=False,
                            precision=4
                        )
                        
                        language_code = gr.Textbox(
                            label="Language Code (ISO 639-1)",
                            interactive=False
                        )
                        
                        # Top predictions table
                        top_predictions = gr.Dataframe(
                            headers=["Language", "Code", "Confidence"],
                            label="Top 5 Predictions",
                            interactive=False,
                            wrap=True
                        )
        
        # Status/Info section
        with gr.Row():
            status_text = gr.Textbox(
                label="Status",
                interactive=False,
                visible=False
            )
        
        # Event handlers
        def detect_language_wrapper(text, selected_model):
            if not text.strip():
                return (
                    "No text provided",
                    0.0,
                    "",
                    [],
                    gr.update(value="Please enter some text to analyze.", visible=True)
                )
            
            try:
                # Switch model if needed
                if detector.current_model_key != selected_model:
                    try:
                        detector.switch_model(selected_model)
                    except NotImplementedError:
                        return (
                            "Model unavailable",
                            0.0,
                            "",
                            [],
                            gr.update(value="This model is not yet implemented. Please select an available model.", visible=True)
                        )
                    except Exception as e:
                        return (
                            "Model error",
                            0.0,
                            "",
                            [],
                            gr.update(value=f"Error loading model: {str(e)}", visible=True)
                        )
                
                result = detector.detect_language(text)
                
                # Extract main prediction
                main_lang = result['language']
                main_confidence = result['confidence']
                main_code = result['language_code']
                
                # Format top predictions for table
                predictions_table = [
                    [pred['language'], pred['language_code'], f"{pred['confidence']:.4f}"]
                    for pred in result['top_predictions']
                ]
                
                model_info = result.get('metadata', {}).get('model_info', {})
                model_name = model_info.get('name', 'Unknown Model')
                
                return (
                    main_lang,
                    main_confidence,
                    main_code,
                    predictions_table,
                    gr.update(value=f"✅ Analysis Complete\n\nInput Text: {text[:100]}{'...' if len(text) > 100 else ''}\n\nDetected Language: {main_lang} ({main_code})\nConfidence: {main_confidence:.2%}\n\nModel: {model_name}", visible=True)
                )
                
            except Exception as e:
                return (
                    "Error occurred",
                    0.0,
                    "",
                    [],
                    gr.update(value=f"Error: {str(e)}", visible=True)
                )
        
        def update_model_info(selected_model):
            """Update model information display when model selection changes."""
            try:
                if detector.current_model_key != selected_model:
                    detector.switch_model(selected_model)
                model_info = detector.get_current_model_info()
                return _format_model_info(model_info)
            except NotImplementedError:
                return "**This model is not yet implemented.** Please select an available model."
            except Exception as e:
                return f"**Error loading model information:** {str(e)}"
        
        # Connect the button to the detection function
        detect_btn.click(
            fn=detect_language_wrapper,
            inputs=[text_input, model_selector],
            outputs=[detected_language, confidence_score, language_code, top_predictions, status_text]
        )
        
        # Also trigger on Enter key in text input
        text_input.submit(
            fn=detect_language_wrapper,
            inputs=[text_input, model_selector],
            outputs=[detected_language, confidence_score, language_code, top_predictions, status_text]
        )
        
        # Update model info when selection changes
        model_selector.change(
            fn=update_model_info,
            inputs=[model_selector],
            outputs=[model_info_display]
        )
    
    return app


def _format_model_info(model_info):
    """Format model information for display."""
    if not model_info:
        return "No model information available."
    
    formatted_info = f"""
**{model_info.get('name', 'Unknown Model')}**

{model_info.get('description', 'No description available.')}

**📊 Performance:**
- Accuracy: {model_info.get('accuracy', 'N/A')}
- Model Size: {model_info.get('model_size', 'N/A')}

**🏗️ Architecture:**
- Model Architecture: {model_info.get('architecture', 'N/A')}
- Base Model: {model_info.get('base_model', 'N/A')}
- Training Dataset: {model_info.get('dataset', 'N/A')}

**🌐 Languages:** {model_info.get('languages_supported', 'N/A')}

**⚙️ Training Details:** {model_info.get('training_details', 'N/A')}

**💡 Use Cases:** {model_info.get('use_cases', 'N/A')}

**✅ Strengths:** {model_info.get('strengths', 'N/A')}

**⚠️ Limitations:** {model_info.get('limitations', 'N/A')}
"""
    return formatted_info


if __name__ == "__main__":
    app = main()
    app.launch()