zineb36 commited on
Commit
0271f98
Β·
verified Β·
1 Parent(s): ad4f4ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -244
app.py CHANGED
@@ -3,255 +3,74 @@ from deep_translator import GoogleTranslator
3
  from gtts import gTTS
4
  import tempfile
5
 
6
- # =========================
7
-
8
- # Custom CSS
9
-
10
- # =========================
11
-
12
  custom_css = """
13
  @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
14
-
15
- .gradio-container {
16
- font-family: 'Poppins', sans-serif !important;
17
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
18
- }
19
-
20
- #header {
21
- text-align: center;
22
- color: white;
23
- padding: 40px 20px;
24
- background: rgba(255,255,255,0.1);
25
- backdrop-filter: blur(10px);
26
- border-radius: 25px;
27
- margin: 20px;
28
- border: 1px solid rgba(255,255,255,0.3);
29
- box-shadow: 0 8px 32px rgba(0,0,0,0.1);
30
- }
31
-
32
- #header h1 {
33
- font-size: 3.2em;
34
- font-weight: 700;
35
- margin-bottom: 15px;
36
- text-shadow: 2px 2px 8px rgba(0,0,0,0.2);
37
- }
38
-
39
- #header p {
40
- font-size: 1.2em;
41
- opacity: 0.95;
42
- }
43
-
44
- .main-box {
45
- background: white;
46
- padding: 45px;
47
- border-radius: 25px;
48
- box-shadow: 0 20px 60px rgba(0,0,0,0.25);
49
- margin: 20px;
50
- }
51
-
52
- .translate-btn {
53
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
54
- border: none !important;
55
- font-size: 18px !important;
56
- font-weight: 600 !important;
57
- padding: 16px !important;
58
- border-radius: 12px !important;
59
- }
60
-
61
- .translate-btn:hover {
62
- transform: translateY(-3px);
63
- box-shadow: 0 12px 24px rgba(102, 126, 234, 0.5) !important;
64
- }
65
-
66
- #footer {
67
- text-align: center;
68
- color: white;
69
- padding: 30px;
70
- margin-top: 30px;
71
- }
72
-
73
- #footer a {
74
- color: white !important;
75
- text-decoration: none;
76
- margin: 0 15px;
77
- font-weight: 600;
78
- }
79
-
80
- #footer a:hover {
81
- text-decoration: underline;
82
- }
83
  """
84
 
85
- # =========================
86
-
87
- # Languages
88
-
89
- # =========================
90
-
91
  languages = {
92
- "English": "en",
93
- "French": "fr",
94
- "Spanish": "es",
95
- "German": "de",
96
- "Arabic": "ar",
97
- "Italian": "it",
98
- "Japanese": "ja",
99
- "Chinese": "zh-CN",
100
- "Korean": "ko",
101
- "Portuguese": "pt"
102
  }
103
 
104
- # =========================
105
-
106
- # Translation Function
107
-
108
- # =========================
109
-
110
  def translate_text(text, src_lang, dest_lang):
111
-
112
- ```
113
- if not text.strip():
114
- return "⚠️ Please enter text to translate", None
115
-
116
- try:
117
-
118
- src_code = languages[src_lang]
119
- dest_code = languages[dest_lang]
120
-
121
- translated = GoogleTranslator(
122
- source=src_code,
123
- target=dest_code
124
- ).translate(text)
125
-
126
- tts = gTTS(
127
- text=translated,
128
- lang=dest_code
129
- )
130
-
131
- with tempfile.NamedTemporaryFile(
132
- delete=False,
133
- suffix=".mp3"
134
- ) as fp:
135
-
136
- tts.save(fp.name)
137
-
138
- return translated, fp.name
139
-
140
- except Exception as e:
141
- return f"❌ Error: {str(e)}", None
142
- ```
143
-
144
- # =========================
145
-
146
- # Gradio Interface
147
-
148
- # =========================
149
-
150
- with gr.Blocks(
151
- css=custom_css,
152
- theme=gr.themes.Base(),
153
- title="CodeAlpha AI Translator"
154
- ) as demo:
155
-
156
- ```
157
- gr.HTML(
158
- '''
159
- <div id="header">
160
- <h1>🌍 CodeAlpha AI Translator</h1>
161
- <p>Task 1 | CodeAlpha AI Internship 2026</p>
162
- <p style="font-size: 1em; margin-top: 10px;">
163
- Real-time Translation + Text-to-Speech
164
- </p>
165
- </div>
166
- '''
167
- )
168
-
169
- with gr.Column(elem_classes="main-box"):
170
-
171
- gr.Markdown("## πŸ”„ Select Languages")
172
-
173
- with gr.Row():
174
-
175
- src = gr.Dropdown(
176
- choices=list(languages.keys()),
177
- value="English",
178
- label="From Language"
179
- )
180
-
181
- dest = gr.Dropdown(
182
- choices=list(languages.keys()),
183
- value="Arabic",
184
- label="To Language"
185
- )
186
-
187
- text = gr.Textbox(
188
- label="πŸ“ Enter Your Text",
189
- value="Hello! I am a CodeAlpha AI Intern.",
190
- lines=5,
191
- placeholder="Type or paste your text here..."
192
- )
193
-
194
- btn = gr.Button(
195
- "✨ Translate Now",
196
- variant="primary",
197
- elem_classes="translate-btn"
198
- )
199
-
200
- gr.Markdown("## 🎯 Translation Results")
201
-
202
- with gr.Row():
203
-
204
- out_text = gr.Textbox(
205
- label="✨ Translation",
206
- lines=5,
207
- interactive=False
208
- )
209
-
210
- out_audio = gr.Audio(
211
- label="πŸ”Š Listen to Audio",
212
- type="filepath"
213
- )
214
-
215
- btn.click(
216
- fn=translate_text,
217
- inputs=[text, src, dest],
218
- outputs=[out_text, out_audio]
219
- )
220
-
221
- gr.HTML(
222
- '''
223
- <div id="footer">
224
- <p>
225
- Β© 2026 CodeAlpha AI Internship
226
- </p>
227
-
228
- <p style="margin-top: 15px;">
229
-
230
- <a href="https://github.com/zineb36-star">
231
- πŸ’» GitHub
232
- </a>
233
-
234
- <a href="https://huggingface.co/zineb36">
235
- πŸ€— Hugging Face
236
- </a>
237
-
238
- <a href="https://codealpha.tech">
239
- πŸš€ CodeAlpha
240
- </a>
241
-
242
- </p>
243
- </div>
244
- '''
245
- )
246
- ```
247
-
248
- # =========================
249
-
250
- # Launch App
251
-
252
- # =========================
253
-
254
- demo.launch(
255
- server_name="0.0.0.0",
256
- server_port=7860
257
- )
 
3
  from gtts import gTTS
4
  import tempfile
5
 
 
 
 
 
 
 
6
  custom_css = """
7
  @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
8
+ .gradio-container {font-family: 'Poppins', sans-serif !important; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;}
9
+ #header {text-align: center; color: white; padding: 40px 20px; background: rgba(255,255,255,0.1); backdrop-filter: blur(10px); border-radius: 25px; margin: 20px; border: 1px solid rgba(255,255,255,0.3); box-shadow: 0 8px 32px rgba(0,0,0,0.1);}
10
+ #header h1 {font-size: 3.2em; font-weight: 700; margin-bottom: 15px; text-shadow: 2px 2px 8px rgba(0,0,0,0.2);}
11
+ #header p {font-size: 1.2em; opacity: 0.95;}
12
+ .main-box {background: white; padding: 45px; border-radius: 25px; box-shadow: 0 20px 60px rgba(0,0,0,0.25); margin: 20px;}
13
+ .translate-btn {background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; border: none !important; font-size: 18px !important; font-weight: 600 !important; padding: 16px !important; border-radius: 12px !important;}
14
+ .translate-btn:hover {transform: translateY(-3px); box-shadow: 0 12px 24px rgba(102, 126, 234, 0.5) !important;}
15
+ #footer {text-align: center; color: white; padding: 30px; margin-top: 30px;}
16
+ #footer a {color: white !important; text-decoration: none; margin: 0 15px; font-weight: 600;}
17
+ #footer a:hover {text-decoration: underline;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
 
 
 
 
 
 
 
20
  languages = {
21
+ "English": "en", "French": "fr", "Spanish": "es", "German": "de",
22
+ "Arabic": "ar", "Italian": "it", "Japanese": "ja", "Chinese": "zh-CN",
23
+ "Korean": "ko", "Portuguese": "pt"
 
 
 
 
 
 
 
24
  }
25
 
 
 
 
 
 
 
26
  def translate_text(text, src_lang, dest_lang):
27
+ if not text.strip():
28
+ return "⚠️ Please enter text to translate", None
29
+ try:
30
+ src_code = languages[src_lang]
31
+ dest_code = languages[dest_lang]
32
+ translated = GoogleTranslator(source=src_code, target=dest_code).translate(text)
33
+ tts = gTTS(text=translated, lang=dest_code)
34
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
35
+ tts.save(fp.name)
36
+ return translated, fp.name
37
+ except Exception as e:
38
+ return f"❌ Error: {str(e)}", None
39
+
40
+ with gr.Blocks(css=custom_css, theme=gr.themes.Base(), title="CodeAlpha AI Translator") as demo:
41
+ gr.HTML('''
42
+ <div id="header">
43
+ <h1>🌍 CodeAlpha AI Translator</h1>
44
+ <p>Task 1 | CodeAlpha AI Internship 2026</p>
45
+ <p style="font-size: 1em; margin-top: 10px;">Real-time Translation + Text-to-Speech</p>
46
+ </div>
47
+ ''')
48
+
49
+ with gr.Column(elem_classes="main-box"):
50
+ gr.Markdown("## πŸ”„ Select Languages")
51
+ with gr.Row():
52
+ src = gr.Dropdown(choices=list(languages.keys()), value="English", label="From Language")
53
+ dest = gr.Dropdown(choices=list(languages.keys()), value="Arabic", label="To Language")
54
+
55
+ text = gr.Textbox(label="πŸ“ Enter Your Text", value="Hello! I am a CodeAlpha AI Intern.", lines=5, placeholder="Type or paste your text here...")
56
+ btn = gr.Button("✨ Translate Now", variant="primary", elem_classes="translate-btn")
57
+
58
+ gr.Markdown("## 🎯 Translation Results")
59
+ with gr.Row():
60
+ out_text = gr.Textbox(label="✨ Translation", lines=5, interactive=False)
61
+ out_audio = gr.Audio(label="πŸ”Š Listen to Audio", type="filepath")
62
+
63
+ btn.click(fn=translate_text, inputs=[text, src, dest], outputs=[out_text, out_audio])
64
+
65
+ gr.HTML('''
66
+ <div id="footer">
67
+ <p>Β© 2026 CodeAlpha AI Internship</p>
68
+ <p style="margin-top: 15px;">
69
+ <a href="https://github.com/zineb36-star">πŸ’» GitHub</a>
70
+ <a href="https://huggingface.co/zineb36">πŸ€— Hugging Face</a>
71
+ <a href="https://codealpha.tech">πŸš€ CodeAlpha</a>
72
+ </p>
73
+ </div>
74
+ ''')
75
+
76
+ demo.launch()