Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
.
|
| 16 |
-
font-
|
| 17 |
-
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
text-align: center;
|
| 22 |
-
color: white;
|
| 23 |
-
|
| 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 |
-
"
|
| 94 |
-
"
|
| 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 |
-
|
| 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 |
-
<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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|