Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,74 +3,255 @@ from deep_translator import GoogleTranslator
|
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
languages = {
|
| 7 |
-
"
|
| 8 |
"French": "fr",
|
| 9 |
"Spanish": "es",
|
| 10 |
"German": "de",
|
| 11 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
```
|
| 17 |
-
if not text:
|
| 18 |
-
return "Please enter text", None
|
| 19 |
|
| 20 |
try:
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
translated = GoogleTranslator(
|
| 25 |
-
source=
|
| 26 |
-
target=
|
| 27 |
).translate(text)
|
| 28 |
|
| 29 |
-
temp_audio = tempfile.NamedTemporaryFile(
|
| 30 |
-
delete=False,
|
| 31 |
-
suffix=".mp3"
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
tts = gTTS(
|
| 35 |
text=translated,
|
| 36 |
-
lang=
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
-
return str(e), None
|
| 45 |
```
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
```
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
lines=5,
|
| 54 |
-
placeholder="
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
gr.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
)
|
| 62 |
-
],
|
| 63 |
|
| 64 |
-
|
| 65 |
-
gr.Textbox(label="Translation"),
|
| 66 |
-
gr.Audio(type="filepath")
|
| 67 |
-
],
|
| 68 |
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
|
|
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|