Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import required libraries
|
2 |
+
import streamlit as st
|
3 |
+
import speech_recognition as sr
|
4 |
+
from transformers import pipeline
|
5 |
+
import pyttsx3
|
6 |
+
|
7 |
+
# Initialize the text-to-speech engine
|
8 |
+
tts_engine = pyttsx3.init()
|
9 |
+
tts_engine.setProperty('rate', 150) # Set the speech rate for TTS
|
10 |
+
|
11 |
+
# Function to transcribe audio to text
|
12 |
+
def transcribe_audio(audio_file, input_language):
|
13 |
+
try:
|
14 |
+
recognizer = sr.Recognizer()
|
15 |
+
with sr.AudioFile(audio_file) as source:
|
16 |
+
audio_data = recognizer.record(source)
|
17 |
+
text = recognizer.recognize_google(audio_data, language=input_language)
|
18 |
+
return text
|
19 |
+
except Exception as e:
|
20 |
+
return f"Error: {str(e)}"
|
21 |
+
|
22 |
+
# Function to translate text
|
23 |
+
def translate_text(text, input_language, output_language):
|
24 |
+
try:
|
25 |
+
model_name = f"Helsinki-NLP/opus-mt-{input_language}-{output_language}"
|
26 |
+
translator = pipeline("translation", model=model_name)
|
27 |
+
translated = translator(text)[0]["translation_text"]
|
28 |
+
return translated
|
29 |
+
except Exception as e:
|
30 |
+
return f"Error: {str(e)}"
|
31 |
+
|
32 |
+
# Function to convert text to speech
|
33 |
+
def speak_text(text):
|
34 |
+
try:
|
35 |
+
tts_engine.say(text)
|
36 |
+
tts_engine.runAndWait()
|
37 |
+
except Exception as e:
|
38 |
+
st.error(f"Error in text-to-speech: {str(e)}")
|
39 |
+
|
40 |
+
# Streamlit app UI
|
41 |
+
st.title("Real-Time Voice-to-Voice Translator 🌍🎤")
|
42 |
+
st.markdown("""
|
43 |
+
This app translates spoken input between multiple languages in real time.
|
44 |
+
1. Select input and output languages.
|
45 |
+
2. Upload an audio file for translation.
|
46 |
+
3. Listen to the translated speech.
|
47 |
+
""")
|
48 |
+
|
49 |
+
#
|