Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,136 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from gtts import gTTS
|
| 2 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def text_to_speech(text, language):
|
| 5 |
tts = gTTS(text=text, lang=language)
|
| 6 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 7 |
tts.save(temp_file.name)
|
| 8 |
return temp_file.name
|
| 9 |
-
import pyttsx3
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
engine.runAndWait()
|
| 22 |
-
return "output_audio.mp3"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Function to translate text
|
| 35 |
-
def translate_text(text, target_language):
|
| 36 |
-
"""
|
| 37 |
-
Translate text to the target language.
|
| 38 |
-
:param text: Input text to be translated.
|
| 39 |
-
:param target_language: Target language code ('en', 'ur', 'zh-cn', etc.).
|
| 40 |
-
:return: Translated text.
|
| 41 |
-
"""
|
| 42 |
-
try:
|
| 43 |
-
translated = GoogleTranslator(source='auto', target=target_language).translate(text)
|
| 44 |
-
return translated
|
| 45 |
-
except Exception as e:
|
| 46 |
-
return f"Error: {str(e)}"
|
| 47 |
-
|
| 48 |
-
# Function to convert speech to text
|
| 49 |
-
def speech_to_text(audio_file):
|
| 50 |
-
"""
|
| 51 |
-
Convert speech to text using SpeechRecognition.
|
| 52 |
-
:param audio_file: Audio file from user input.
|
| 53 |
-
:return: Transcribed text.
|
| 54 |
-
"""
|
| 55 |
-
recognizer = sr.Recognizer()
|
| 56 |
-
try:
|
| 57 |
-
with sr.AudioFile(audio_file) as source:
|
| 58 |
-
audio = recognizer.record(source)
|
| 59 |
-
text = recognizer.recognize_google(audio)
|
| 60 |
-
return text
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return f"Error: {str(e)}"
|
| 63 |
-
|
| 64 |
-
# Function to convert text to speech
|
| 65 |
-
def text_to_speech(text, language):
|
| 66 |
-
"""
|
| 67 |
-
Convert text to speech using pyttsx3.
|
| 68 |
-
:param text: Text to be spoken.
|
| 69 |
-
:param language: Language of the text.
|
| 70 |
-
:return: None (speaks the text directly).
|
| 71 |
-
"""
|
| 72 |
-
engine = pyttsx3.init()
|
| 73 |
-
# Set language for TTS
|
| 74 |
-
voices = engine.getProperty('voices')
|
| 75 |
-
if language == 'en':
|
| 76 |
-
engine.setProperty('voice', voices[0].id) # English
|
| 77 |
-
elif language == 'ur':
|
| 78 |
-
engine.setProperty('voice', voices[-1].id) # Try for Urdu (depends on system setup)
|
| 79 |
-
else:
|
| 80 |
-
engine.setProperty('voice', voices[0].id) # Default to English
|
| 81 |
-
engine.say(text)
|
| 82 |
-
engine.runAndWait()
|
| 83 |
-
|
| 84 |
-
# Streamlit app UI
|
| 85 |
-
st.title("Real-Time Voice-to-Voice Translator 🌍🗣️")
|
| 86 |
-
st.markdown("""
|
| 87 |
-
This app translates voice input between different languages and provides the translated output as speech.
|
| 88 |
-
1. Select the input and output languages.
|
| 89 |
-
2. Upload your audio file for translation.
|
| 90 |
-
3. Get the translated audio output.
|
| 91 |
-
""")
|
| 92 |
-
|
| 93 |
-
# Language options
|
| 94 |
-
language_options = {
|
| 95 |
-
"English 🇺🇸": "en",
|
| 96 |
-
"Urdu 🇵🇰": "ur",
|
| 97 |
-
"Chinese 🇨🇳": "zh-cn",
|
| 98 |
-
"Italian 🇮🇹": "it",
|
| 99 |
-
"German 🇩🇪": "de",
|
| 100 |
-
"Japanese 🇯🇵": "ja",
|
| 101 |
-
"Korean 🇰🇷": "ko"
|
| 102 |
-
}
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
st.success(f"Transcribed Text: {transcribed_text}")
|
| 124 |
-
|
| 125 |
-
# Step 2: Translate text
|
| 126 |
-
st.info("Translating text...")
|
| 127 |
-
translated_text = translate_text(transcribed_text, output_lang_code)
|
| 128 |
-
if "Error" in translated_text:
|
| 129 |
-
st.error(translated_text)
|
| 130 |
-
else:
|
| 131 |
-
st.success(f"Translated Text: {translated_text}")
|
| 132 |
-
|
| 133 |
-
# Step 3: Convert translated text to speech
|
| 134 |
-
st.info("Converting translated text to speech...")
|
| 135 |
-
text_to_speech(translated_text, output_lang_code)
|
| 136 |
-
st.success("Translation complete! Check the output above.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
from gtts import gTTS
|
| 5 |
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
import base64
|
| 8 |
+
|
| 9 |
+
# Initialize the translation pipeline
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_translation_pipeline(model_name):
|
| 12 |
+
return pipeline("translation", model=model_name)
|
| 13 |
+
|
| 14 |
+
# Initialize speech recognition
|
| 15 |
+
recognizer = sr.Recognizer()
|
| 16 |
+
|
| 17 |
+
def translate_text(input_text, model):
|
| 18 |
+
return model(input_text)[0]["translation_text"]
|
| 19 |
+
|
| 20 |
+
def speech_to_text(audio_file):
|
| 21 |
+
with sr.AudioFile(audio_file) as source:
|
| 22 |
+
audio_data = recognizer.record(source)
|
| 23 |
+
return recognizer.recognize_google(audio_data)
|
| 24 |
|
| 25 |
def text_to_speech(text, language):
|
| 26 |
tts = gTTS(text=text, lang=language)
|
| 27 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 28 |
tts.save(temp_file.name)
|
| 29 |
return temp_file.name
|
|
|
|
| 30 |
|
| 31 |
+
# Streamlit app
|
| 32 |
+
st.title("Real-Time Language Translator")
|
| 33 |
+
st.write("Translate voice and text between multiple languages in real-time!")
|
| 34 |
+
|
| 35 |
+
# Language selection
|
| 36 |
+
st.sidebar.header("Settings")
|
| 37 |
+
input_lang = st.sidebar.selectbox("Select Input Language", ["English", "French", "Spanish", "German", "Hindi"])
|
| 38 |
+
output_lang = st.sidebar.selectbox("Select Output Language", ["English", "French", "Spanish", "German", "Hindi"])
|
| 39 |
|
| 40 |
+
# Language codes mapping
|
| 41 |
+
lang_codes = {
|
| 42 |
+
"English": "en",
|
| 43 |
+
"French": "fr",
|
| 44 |
+
"Spanish": "es",
|
| 45 |
+
"German": "de",
|
| 46 |
+
"Hindi": "hi"
|
| 47 |
+
}
|
| 48 |
|
| 49 |
+
input_code = lang_codes[input_lang]
|
| 50 |
+
output_code = lang_codes[output_lang]
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Model selection
|
| 53 |
+
model_name = f"Helsinki-NLP/opus-mt-{input_code}-{output_code}"
|
| 54 |
+
translation_pipeline = load_translation_pipeline(model_name)
|
| 55 |
|
| 56 |
+
# Input options
|
| 57 |
+
st.header("Input Options")
|
| 58 |
+
input_mode = st.radio("Choose Input Mode:", ["Text", "Voice"])
|
| 59 |
|
| 60 |
+
if input_mode == "Text":
|
| 61 |
+
input_text = st.text_area(f"Enter text in {input_lang}:")
|
| 62 |
+
if st.button("Translate"):
|
| 63 |
+
if input_text.strip():
|
| 64 |
+
translated_text = translate_text(input_text, translation_pipeline)
|
| 65 |
+
st.success(f"Translated Text in {output_lang}: {translated_text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Option to download translation as audio
|
| 68 |
+
if st.checkbox("Play Translated Audio"):
|
| 69 |
+
audio_file = text_to_speech(translated_text, output_code)
|
| 70 |
+
audio_bytes = open(audio_file, "rb").read()
|
| 71 |
+
st.audio(audio_bytes, format="audio/mp3")
|
| 72 |
|
| 73 |
+
# Provide download link
|
| 74 |
+
b64 = base64.b64encode(audio_bytes).decode()
|
| 75 |
+
href = f'<a href="data:audio/mp3;base64,{b64}" download="translation.mp3">Download Translated Audio</a>'
|
| 76 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 77 |
+
else:
|
| 78 |
+
audio_file = st.file_uploader("Upload an audio file (WAV format)", type=["wav"])
|
| 79 |
+
if audio_file is not None:
|
| 80 |
+
if st.button("Translate"):
|
| 81 |
+
try:
|
| 82 |
+
input_text = speech_to_text(audio_file)
|
| 83 |
+
st.write(f"Recognized Text in {input_lang}: {input_text}")
|
| 84 |
+
translated_text = translate_text(input_text, translation_pipeline)
|
| 85 |
+
st.success(f"Translated Text in {output_lang}: {translated_text}")
|
| 86 |
|
| 87 |
+
# Option to download translation as audio
|
| 88 |
+
if st.checkbox("Play Translated Audio"):
|
| 89 |
+
audio_file = text_to_speech(translated_text, output_code)
|
| 90 |
+
audio_bytes = open(audio_file, "rb").read()
|
| 91 |
+
st.audio(audio_bytes, format="audio/mp3")
|
| 92 |
+
|
| 93 |
+
# Provide download link
|
| 94 |
+
b64 = base64.b64encode(audio_bytes).decode()
|
| 95 |
+
href = f'<a href="data:audio/mp3;base64,{b64}" download="translation.mp3">Download Translated Audio</a>'
|
| 96 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|