TRIAL_CODE / app.py
mishiawan's picture
Create app.py
5a503e3 verified
raw
history blame
1.56 kB
# Import required libraries
import streamlit as st
import speech_recognition as sr
from transformers import pipeline
import pyttsx3
# Initialize the text-to-speech engine
tts_engine = pyttsx3.init()
tts_engine.setProperty('rate', 150) # Set the speech rate for TTS
# Function to transcribe audio to text
def transcribe_audio(audio_file, input_language):
try:
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language=input_language)
return text
except Exception as e:
return f"Error: {str(e)}"
# Function to translate text
def translate_text(text, input_language, output_language):
try:
model_name = f"Helsinki-NLP/opus-mt-{input_language}-{output_language}"
translator = pipeline("translation", model=model_name)
translated = translator(text)[0]["translation_text"]
return translated
except Exception as e:
return f"Error: {str(e)}"
# Function to convert text to speech
def speak_text(text):
try:
tts_engine.say(text)
tts_engine.runAndWait()
except Exception as e:
st.error(f"Error in text-to-speech: {str(e)}")
# Streamlit app UI
st.title("Real-Time Voice-to-Voice Translator 🌍🎀")
st.markdown("""
This app translates spoken input between multiple languages in real time.
1. Select input and output languages.
2. Upload an audio file for translation.
3. Listen to the translated speech.
""")
#