import streamlit as st from transformers import pipeline import speech_recognition as sr import soundfile as sf # Load translation models models = { "Urdu": "Helsinki-NLP/opus-mt-en-ur", "Punjabi": "Helsinki-NLP/opus-mt-en-pu", "Sindhi": "Helsinki-NLP/opus-mt-en-sd", "Bihari": "Helsinki-NLP/opus-mt-en-hi", # Bihari is dialectically close to Hindi "Arabic": "Helsinki-NLP/opus-mt-en-ar", "Chinese": "Helsinki-NLP/opus-mt-en-zh", "French": "Helsinki-NLP/opus-mt-en-fr", "German": "Helsinki-NLP/opus-mt-en-de" } # Streamlit UI st.title("Language Translator App") st.write("Convert voice and text from English to various languages.") # Input method selection input_method = st.radio("Select input method:", ["Text", "Voice"]) # Select target language target_language = st.selectbox("Select target language:", list(models.keys())) # Load translation pipeline translation_pipeline = pipeline("translation_en_to_" + target_language.lower(), model=models[target_language]) def translate_text(text): translation = translation_pipeline(text)[0]['translation_text'] return translation def translate_voice(audio_file): recognizer = sr.Recognizer() with sr.AudioFile(audio_file) as source: audio_data = recognizer.record(source) text = recognizer.recognize_google(audio_data) return translate_text(text) # Text translation if input_method == "Text": text_input = st.text_area("Enter English text here:") if st.button("Translate Text"): if text_input: translated_text = translate_text(text_input) st.write(f"**Translated Text ({target_language}):** {translated_text}") else: st.write("Please enter some text to translate.") # Voice translation elif input_method == "Voice": st.write("Upload an English voice file (WAV format).") voice_input = st.file_uploader("Upload Voice File", type=["wav"]) if st.button("Translate Voice"): if voice_input: translated_voice = translate_voice(voice_input) st.write(f"**Translated Text ({target_language}):** {translated_voice}") else: st.write("Please upload a voice file to translate.")