Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,58 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
from langdetect import detect
|
| 3 |
from gtts import gTTS
|
| 4 |
-
import
|
| 5 |
-
import tempfile
|
| 6 |
-
|
| 7 |
-
# π Supported language codes (Portuguese removed)
|
| 8 |
-
LANG_CODE = {
|
| 9 |
-
"en": "English", "es": "Spanish", "fr": "French", "de": "German",
|
| 10 |
-
"it": "Italian", "nl": "Dutch", "ru": "Russian", "zh": "Chinese"
|
| 11 |
-
}
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
translation_cache = {}
|
| 18 |
-
|
| 19 |
-
def detect_language(text):
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
except:
|
| 24 |
-
return "Unknown"
|
| 25 |
-
|
| 26 |
-
def get_translation_pipeline(src_code, tgt_code):
|
| 27 |
-
model_name = f"Helsinki-NLP/opus-mt-{src_code}-{tgt_code}"
|
| 28 |
-
key = (src_code, tgt_code)
|
| 29 |
-
if key not in translation_cache:
|
| 30 |
-
try:
|
| 31 |
-
translation_cache[key] = pipeline("translation", model=model_name, device=-1)
|
| 32 |
-
except:
|
| 33 |
-
translation_cache[key] = None
|
| 34 |
-
return translation_cache[key]
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 40 |
-
tts.save(temp_file.name)
|
| 41 |
-
return temp_file.name
|
| 42 |
-
except:
|
| 43 |
-
return None
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
if detected_lang == "Unknown":
|
| 51 |
-
return "Could not detect source language.", None, detected_lang
|
| 52 |
|
| 53 |
-
if detected_lang == target_lang:
|
| 54 |
-
return f"Source and target languages are the same. Text: {input_text}", None, detected_lang
|
| 55 |
-
|
| 56 |
-
src_code = LANG_NAME_TO_CODE.get(detected_lang)
|
| 57 |
-
tgt_code = LANG_NAME_TO_CODE.get(target_lang)
|
| 58 |
-
|
| 59 |
-
if not src_code or not tgt_code:
|
| 60 |
-
return f"Unsupported language pair: {detected_lang} β {target_lang}", None, detected_lang
|
| 61 |
-
|
| 62 |
-
translator = get_translation_pipeline(src_code, tgt_code)
|
| 63 |
-
if not translator:
|
| 64 |
-
return f"No model found for {detected_lang} β {target_lang}", None, detected_lang
|
| 65 |
-
|
| 66 |
-
try:
|
| 67 |
-
result = translator(input_text, max_length=512)
|
| 68 |
-
translated_text = result[0]['translation_text']
|
| 69 |
-
audio_path = generate_audio_file(translated_text, tgt_code) if speak else None
|
| 70 |
-
return translated_text, audio_path, detected_lang
|
| 71 |
except Exception as e:
|
| 72 |
-
return f"
|
| 73 |
|
| 74 |
-
# Gradio UI
|
| 75 |
-
with gr.Blocks() as
|
| 76 |
-
gr.Markdown("# π
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column():
|
| 80 |
-
|
| 81 |
target_lang = gr.Dropdown(
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
with gr.Column():
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
detected_lang = gr.Textbox(label="Detected Language", interactive=False)
|
| 93 |
|
| 94 |
-
|
| 95 |
translate_text,
|
| 96 |
-
inputs=[
|
| 97 |
-
outputs=[
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
if __name__ == "__main__":
|
| 101 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from langdetect import detect
|
| 4 |
from gtts import gTTS
|
| 5 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# π₯ Load Hugging Face translation model
|
| 8 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
| 9 |
|
| 10 |
+
def translate_text(text, target_lang, speech_output):
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
# Detect source language
|
| 13 |
+
source_lang = detect(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Translate text
|
| 16 |
+
translation = translator(text, tgt_lang=target_lang)
|
| 17 |
+
translated_text = translation[0]['translation_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Generate speech if enabled
|
| 20 |
+
audio_file = None
|
| 21 |
+
if speech_output:
|
| 22 |
+
tts = gTTS(translated_text, lang=target_lang)
|
| 23 |
+
audio_file = "output.mp3"
|
| 24 |
+
tts.save(audio_file)
|
| 25 |
|
| 26 |
+
return f"Detected Language: {source_lang}\n\nTranslation: {translated_text}", audio_file
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
+
return f"β οΈ Error: {str(e)}", None
|
| 30 |
|
| 31 |
+
# π¨ Gradio UI
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("# π LinguaCast\nAI-Powered Multilingual Translator with Speech Output")
|
| 34 |
|
| 35 |
with gr.Row():
|
| 36 |
with gr.Column():
|
| 37 |
+
text_input = gr.Textbox(label="Enter your text", placeholder="Type here...")
|
| 38 |
target_lang = gr.Dropdown(
|
| 39 |
+
["en", "es", "fr", "de", "it", "nl", "ru", "zh"],
|
| 40 |
+
label="Select Target Language",
|
| 41 |
+
value="en"
|
| 42 |
)
|
| 43 |
+
speech_output = gr.Checkbox(label="Enable Speech Output", value=False)
|
| 44 |
+
submit_btn = gr.Button("Translate")
|
| 45 |
|
| 46 |
with gr.Column():
|
| 47 |
+
result_output = gr.Textbox(label="Translation Result")
|
| 48 |
+
audio_output = gr.Audio(label="Speech Output", type="filepath")
|
|
|
|
| 49 |
|
| 50 |
+
submit_btn.click(
|
| 51 |
translate_text,
|
| 52 |
+
inputs=[text_input, target_lang, speech_output],
|
| 53 |
+
outputs=[result_output, audio_output]
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Run the app
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
demo.launch()
|