Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456783 unmodified lines121314151617181920212 unmodified lines242526272829303132333435363738394041424344454647484950515253545556import streamlit as stimport timefrom transformers import pipelinest.title("Enhanced Multilingual Translator Chatbot")# Choose the translation models from Hugging Facetranslation_models = {3 unmodified lines"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","English to Spanish": "Helsinki-NLP/opus-mt-en-es","Spanish to English": "Helsinki-NLP/opus-mt-es-en","English to Chinese": "Helsinki-NLP/opus-mt-en-zh","Chinese to English": "Helsinki-NLP/opus-mt-zh-en",# Add more language pairs as needed}2 unmodified lines# Load the translation pipelinetranslator = pipeline(task="translation", model=translation_models[selected_translation])# User input for translationuser_input = st.text_area("Enter text for translation:", "")# Display loading indicatorif st.button("Translate"):with st.spinner("Translating..."):# Simulate translation delay for demonstrationtime.sleep(2)if user_input:# Perform translationtranslated_text = translator(user_input, max_length=500)[0]['translation_text']st.success(f"Translated Text: {translated_text}")else:st.warning("Please enter text for translation.")# Clear button to reset input and resultif st.button("Clear"):user_input = ""st.success("Input cleared.")st.empty() # Clear previous results if anyst.markdown("---")st.subheader("About")st.write("This is an enhanced Multilingual Translator chatbot that uses the Hugging Face Transformers library.")st.write("Select a translation model from the dropdown, enter text, and click 'Translate' to see the translation.")123456783 unmodified lines1213141516172 unmodified lines20212223242526272829303132333435363738394041424344454647import streamlit as stimport speech_recognition as srfrom transformers import pipelinest.title("Speech-to-Text and Translation Chatbot")# Choose the translation models from Hugging Facetranslation_models = {3 unmodified lines"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}2 unmodified lines# Load the translation pipelinetranslator = pipeline(task="translation", model=translation_models[selected_translation])# Function to perform speech-to-text and translationdef 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 translationtranslated_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 translationif st.button("Start Speech-to-Text and Translation"):speech_to_text_and_translate()
app.py+27-36