new_code / app.py
deepugaur's picture
Create app.py
d4eb552 verified
import streamlit as st # Make sure to import streamlit
import torch
from transformers import MarianMTModel, MarianTokenizer
import speech_recognition as sr
from gtts import gTTS
import os
# Function to recognize speech from an audio file
def recognize_speech_from_file(audio_file):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
try:
return recognizer.recognize_google(audio)
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError:
return "Could not request results; check your internet connection"
# Function to translate text
def translate_text(text):
model_name = "Helsinki-NLP/opus-mt-en-hi"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)
translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True))
return tokenizer.decode(translated[0], skip_special_tokens=True)
# Function to convert text to audio
def convert_to_audio(text):
tts = gTTS(text, lang="hi")
audio_output = "output.mp3"
tts.save(audio_output)
return audio_output
st.title("English to Hindi Audio Translation")
# Make the translation service available without time restrictions
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
if audio_file is not None:
text = recognize_speech_from_file(audio_file)
st.write(f"Recognized English Text: {text}")
translated_text = translate_text(text)
st.write(f"Translated Hindi Text: {translated_text}")
audio_output = convert_to_audio(translated_text)
st.audio(audio_output, format='audio/mp3')