Spaces:
Runtime error
Runtime error
Create app.py
Browse filesgradio
deep-translator
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from deep_translator import GoogleTranslator
|
| 3 |
+
|
| 4 |
+
# Mapping of languages to their codes
|
| 5 |
+
LANG_MAP = {
|
| 6 |
+
"Hindi": "hi", "Bengali": "bn", "Marathi": "mr", "Telugu": "te", "Tamil": "ta",
|
| 7 |
+
"Gujarati": "gu", "Urdu": "ur", "Kannada": "kn", "Odia": "or", "Malayalam": "ml",
|
| 8 |
+
"Punjabi": "pa", "Assamese": "as", "Maithili": "mai", "Santali": "sat", "Kashmiri": "ks",
|
| 9 |
+
"Nepali": "ne", "Sindhi": "sd", "Dogri": "doi", "Konkani": "gom", "Manipuri": "mni",
|
| 10 |
+
"Bodo": "brx", "Sanskrit": "sa"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def translate_text(text, target_lang):
|
| 14 |
+
if not text:
|
| 15 |
+
return ""
|
| 16 |
+
try:
|
| 17 |
+
# Translates from auto-detected language to the selected target
|
| 18 |
+
translated = GoogleTranslator(source='auto', target=LANG_MAP[target_lang]).translate(text)
|
| 19 |
+
return translated
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error: {str(e)}"
|
| 22 |
+
|
| 23 |
+
# Custom CSS to match your screenshot (Dark theme + Orange Submit button)
|
| 24 |
+
custom_css = """
|
| 25 |
+
#submit-btn { background-color: #e67e22 !important; color: white !important; }
|
| 26 |
+
#clear-btn { background-color: #4a4a4a !important; color: white !important; }
|
| 27 |
+
.gradio-container { background-color: #121212 !important; }
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
with gr.Blocks(css=custom_css, title="BhashaBridge") as demo:
|
| 31 |
+
gr.Markdown("<h1 style='text-align: center; color: white;'>BhashaBridge</h1>")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
# Left Column: Input and Language Selection
|
| 35 |
+
with gr.Column():
|
| 36 |
+
target_lang = gr.Radio(
|
| 37 |
+
choices=list(LANG_MAP.keys()),
|
| 38 |
+
label="Target Language",
|
| 39 |
+
value="Hindi"
|
| 40 |
+
)
|
| 41 |
+
input_text = gr.Textbox(
|
| 42 |
+
label="Input Text",
|
| 43 |
+
placeholder="Be the change you wish to see in the world.",
|
| 44 |
+
lines=3
|
| 45 |
+
)
|
| 46 |
+
with gr.Row():
|
| 47 |
+
clear_btn = gr.Button("Clear", elem_id="clear-btn")
|
| 48 |
+
submit_btn = gr.Button("Submit", elem_id="submit-btn")
|
| 49 |
+
|
| 50 |
+
# Right Column: Translation Output
|
| 51 |
+
with gr.Column():
|
| 52 |
+
output_text = gr.Textbox(label="Translation", interactive=False, lines=3)
|
| 53 |
+
flag_btn = gr.Button("Flag")
|
| 54 |
+
|
| 55 |
+
# Button Logic
|
| 56 |
+
submit_btn.click(translate_text, inputs=[input_text, target_lang], outputs=output_text)
|
| 57 |
+
clear_btn.click(lambda: ["", ""], outputs=[input_text, output_text])
|
| 58 |
+
|
| 59 |
+
demo.launch()
|