zineb36 commited on
Commit
4a18b90
·
verified ·
1 Parent(s): 760b531

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -0
app.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CodeAlpha Task 3: Language Detection App
2
+ import gradio as gr
3
+ from langdetect import detect, DetectorFactory, LangDetectException
4
+ from langdetect.lang_detect_exception import LangDetectException
5
+
6
+ # Fix randomness for consistent results
7
+ DetectorFactory.seed = 0
8
+
9
+ # Language codes to full names
10
+ LANGUAGES = {
11
+ 'ar': 'Arabic - العربية', 'en': 'English', 'fr': 'Français', 'es': 'Español',
12
+ 'de': 'Deutsch', 'it': 'Italiano', 'pt': 'Português', 'ru': 'Русский',
13
+ 'ja': 'Japanese - 日本語', 'ko': 'Korean - 한국어', 'zh-cn': 'Chinese - 中文',
14
+ 'hi': 'Hindi - हिन्दी', 'tr': 'Türkçe', 'nl': 'Nederlands', 'pl': 'Polski',
15
+ 'sv': 'Svenska', 'da': 'Dansk', 'no': 'Norsk', 'fi': 'Suomi'
16
+ }
17
+
18
+ def detect_language(text):
19
+ """Detect language using langdetect library"""
20
+ if not text.strip():
21
+ return "⚠️ Please enter some text to detect its language", ""
22
+
23
+ if len(text.strip()) < 3:
24
+ return "⚠️ Text too short! Please enter at least 3 characters", ""
25
+
26
+ try:
27
+ lang_code = detect(text)
28
+ lang_name = LANGUAGES.get(lang_code, f"Unknown ({lang_code})")
29
+
30
+ # Confidence simulation - langdetect doesn't give %, so we estimate
31
+ confidence = min(95, max(70, len(text) * 2)) # Longer text = higher confidence
32
+
33
+ result = f"🌍 **Detected Language:** {lang_name}"
34
+ details = f"📊 **Language Code:** `{lang_code}`\n🎯 **Confidence:** ~{confidence}%\n📝 **Characters:** {len(text)}"
35
+
36
+ return result, details
37
+
38
+ except LangDetectException:
39
+ return "❌ **Detection Failed**", "Could not detect language. Try longer text or different words."
40
+
41
+ # Premium CSS
42
+ custom_css = """
43
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
44
+
45
+ .gradio-container {
46
+ font-family: 'Poppins', sans-serif!important;
47
+ background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab)!important;
48
+ background-size: 400% 400%!important;
49
+ animation: gradientBG 15s ease infinite!important;
50
+ }
51
+
52
+ @keyframes gradientBG {
53
+ 0% { background-position: 0% 50%; }
54
+ 50% { background-position: 100% 50%; }
55
+ 100% { background-position: 0% 50%; }
56
+ }
57
+
58
+ #header {
59
+ text-align: center;
60
+ color: white;
61
+ padding: 40px 20px;
62
+ background: rgba(255, 255, 255, 0.15);
63
+ backdrop-filter: blur(20px);
64
+ border-radius: 30px;
65
+ margin: 20px;
66
+ border: 2px solid rgba(255, 255, 255, 0.3);
67
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
68
+ }
69
+
70
+ #header h1 {
71
+ font-size: 3em;
72
+ font-weight: 700;
73
+ margin-bottom: 10px;
74
+ text-shadow: 2px 2px 10px rgba(0,0,0,0.3);
75
+ }
76
+
77
+ .gr-button-primary {
78
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%)!important;
79
+ border: none!important;
80
+ color: white!important;
81
+ font-weight: 600!important;
82
+ border-radius: 12px!important;
83
+ }
84
+
85
+ .gr-button-primary:hover {
86
+ transform: translateY(-3px)!important;
87
+ box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4)!important;
88
+ }
89
+
90
+ #footer {
91
+ text-align: center;
92
+ color: white;
93
+ padding: 25px;
94
+ margin-top: 30px;
95
+ background: rgba(0, 0, 0, 0.2);
96
+ backdrop-filter: blur(10px);
97
+ border-radius: 20px;
98
+ }
99
+ """
100
+
101
+ # Create Gradio App
102
+ with gr.Blocks() as demo:
103
+ gr.HTML("""
104
+ <div id="header">
105
+ <h1>🌍 Language Detection AI</h1>
106
+ <p>Task 3: Language Detection | CodeAlpha AI Internship 2026</p>
107
+ <p>Detect 20+ languages instantly using NLP</p>
108
+ </div>
109
+ """)
110
+
111
+ with gr.Row():
112
+ with gr.Column():
113
+ text_input = gr.Textbox(
114
+ label="Enter Text to Detect",
115
+ placeholder="Type anything... Bonjour, Hello, مرحبا, Hola, 你好...",
116
+ lines=5
117
+ )
118
+ detect_btn = gr.Button("🔍 Detect Language", variant="primary", size="lg")
119
+
120
+ gr.Examples(
121
+ examples=[
122
+ ["Hello, how are you today?"],
123
+ ["Bonjour le monde, comment allez-vous?"],
124
+ ["مرحبا بالعالم، كيف حالك؟"],
125
+ ["Hola mundo, ¿cómo estás?"],
126
+ ["你好世界,你好吗?"],
127
+ ["こんにちは世界、元気ですか?"]
128
+ ],
129
+ inputs=text_input,
130
+ label="Click any example:"
131
+ )
132
+
133
+ with gr.Column():
134
+ result_output = gr.Markdown(label="Detection Result")
135
+ details_output = gr.Markdown(label="Details")
136
+
137
+ detect_btn.click(
138
+ fn=detect_language,
139
+ inputs=text_input,
140
+ outputs=[result_output, details_output]
141
+ )
142
+
143
+ gr.HTML("""
144
+ <div id="footer">
145
+ <p>© 2026 CodeAlpha AI Internship | Built with ❤️ using Gradio + langdetect</p>
146
+ <p>🚀 Demonstrating NLP Skills: Language Detection + Text Analysis</p>
147
+ </div>
148
+ """)
149
+
150
+ demo.launch(css=custom_css, theme=gr.themes.Base())