import streamlit as st import speech_recognition as sr from transformers import pipeline st.title("Speech-to-Text and Translation Chatbot") # Choose the translation models from Hugging Face translation_models = { "English to German": "Helsinki-NLP/opus-mt-en-de", "German to English": "Helsinki-NLP/opus-mt-de-en", "English to French": "Helsinki-NLP/opus-mt-en-fr", "French to English": "Helsinki-NLP/opus-mt-fr-en", "English to Urdu": "Helsinki-NLP/opus-mt-en-ur", "Urdu to English": "Helsinki-NLP/opus-mt-ur-en", # Add more language pairs as needed } selected_translation = st.selectbox("Select translation model", list(translation_models.keys())) # Load the translation pipeline translator = pipeline(task="translation", model=translation_models[selected_translation]) # Function to perform speech-to-text and translation def speech_to_text_and_translate(): recognizer = sr.Recognizer() st.info("Speak into your microphone...") with sr.Microphone() as source: audio_data = recognizer.listen(source, timeout=10) try: text = recognizer.recognize_google(audio_data) st.success(f"Speech-to-Text Result: {text}") # Perform translation translated_text = translator(text, max_length=500)[0]['translation_text'] st.success(f"Translated Text: {translated_text}") except sr.UnknownValueError: st.warning("Speech recognition could not understand audio.") except sr.RequestError as e: st.error(f"Error with the speech recognition service: {e}") # Use speech-to-text and translation if st.button("Start Speech-to-Text and Translation"): speech_to_text_and_translate()