Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from deep_translator import GoogleTranslator
|
| 3 |
+
|
| 4 |
+
LANGUAGES = {
|
| 5 |
+
"Hindi": "hi",
|
| 6 |
+
"Tamil": "ta",
|
| 7 |
+
"Malayalam": "ml",
|
| 8 |
+
"Telugu": "te",
|
| 9 |
+
"Bengali": "bn",
|
| 10 |
+
"Kannada": "kn",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def translate_text(text: str, target_language: str) -> str:
|
| 14 |
+
if not text.strip():
|
| 15 |
+
return "⚠️ Please enter some text to translate."
|
| 16 |
+
lang_code = LANGUAGES.get(target_language)
|
| 17 |
+
if not lang_code:
|
| 18 |
+
return "⚠️ Unsupported language selected."
|
| 19 |
+
try:
|
| 20 |
+
translated = GoogleTranslator(source="en", target=lang_code).translate(text)
|
| 21 |
+
return translated
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"❌ Translation error: {str(e)}"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
with gr.Blocks(
|
| 27 |
+
title="English → Indian Languages Translator",
|
| 28 |
+
theme=gr.themes.Soft(primary_hue="indigo"),
|
| 29 |
+
css="""
|
| 30 |
+
.container { max-width: 860px; margin: auto; }
|
| 31 |
+
.lang-btn { font-size: 1rem !important; min-width: 110px !important; }
|
| 32 |
+
#title { text-align: center; margin-bottom: 8px; }
|
| 33 |
+
#subtitle { text-align: center; color: #6b7280; margin-bottom: 24px; }
|
| 34 |
+
""",
|
| 35 |
+
) as demo:
|
| 36 |
+
gr.Markdown("# 🌐 English → Indian Languages Translator", elem_id="title")
|
| 37 |
+
gr.Markdown("Translate English text into Tamil, Malayalam, Telugu, Bengali, or Kannada instantly.", elem_id="subtitle")
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
with gr.Column(scale=1):
|
| 41 |
+
input_text = gr.Textbox(
|
| 42 |
+
label="English Text",
|
| 43 |
+
placeholder="Type or paste English text here...",
|
| 44 |
+
lines=8,
|
| 45 |
+
max_lines=20,
|
| 46 |
+
)
|
| 47 |
+
with gr.Column(scale=1):
|
| 48 |
+
output_text = gr.Textbox(
|
| 49 |
+
label="Translated Text",
|
| 50 |
+
lines=8,
|
| 51 |
+
interactive=False,
|
| 52 |
+
show_copy_button=True,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
gr.Markdown("### Select Target Language")
|
| 56 |
+
|
| 57 |
+
with gr.Row(elem_classes="container"):
|
| 58 |
+
for lang in LANGUAGES:
|
| 59 |
+
btn = gr.Button(lang, elem_classes="lang-btn", variant="secondary")
|
| 60 |
+
btn.click(
|
| 61 |
+
fn=translate_text,
|
| 62 |
+
inputs=[input_text, gr.State(lang)],
|
| 63 |
+
outputs=output_text,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
clear_btn = gr.Button("🗑️ Clear", variant="stop")
|
| 68 |
+
clear_btn.click(fn=lambda: ("", ""), outputs=[input_text, output_text])
|
| 69 |
+
|
| 70 |
+
gr.Markdown(
|
| 71 |
+
"<center><sub>Powered by deep-translator · Google Translate API</sub></center>"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|