Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
st.title("
|
| 6 |
|
| 7 |
# Choose the translation models from Hugging Face
|
| 8 |
translation_models = {
|
|
@@ -12,10 +12,6 @@ translation_models = {
|
|
| 12 |
"French to English": "Helsinki-NLP/opus-mt-fr-en",
|
| 13 |
"English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
|
| 14 |
"Urdu to English": "Helsinki-NLP/opus-mt-ur-en",
|
| 15 |
-
"English to Spanish": "Helsinki-NLP/opus-mt-en-es",
|
| 16 |
-
"Spanish to English": "Helsinki-NLP/opus-mt-es-en",
|
| 17 |
-
"English to Chinese": "Helsinki-NLP/opus-mt-en-zh",
|
| 18 |
-
"Chinese to English": "Helsinki-NLP/opus-mt-zh-en",
|
| 19 |
# Add more language pairs as needed
|
| 20 |
}
|
| 21 |
|
|
@@ -24,33 +20,28 @@ selected_translation = st.selectbox("Select translation model", list(translation
|
|
| 24 |
# Load the translation pipeline
|
| 25 |
translator = pipeline(task="translation", model=translation_models[selected_translation])
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
st.
|
| 51 |
-
|
| 52 |
-
"This is an enhanced Multilingual Translator chatbot that uses the Hugging Face Transformers library."
|
| 53 |
-
)
|
| 54 |
-
st.write(
|
| 55 |
-
"Select a translation model from the dropdown, enter text, and click 'Translate' to see the translation."
|
| 56 |
-
)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
st.title("Speech-to-Text and Translation Chatbot")
|
| 6 |
|
| 7 |
# Choose the translation models from Hugging Face
|
| 8 |
translation_models = {
|
|
|
|
| 12 |
"French to English": "Helsinki-NLP/opus-mt-fr-en",
|
| 13 |
"English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
|
| 14 |
"Urdu to English": "Helsinki-NLP/opus-mt-ur-en",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Add more language pairs as needed
|
| 16 |
}
|
| 17 |
|
|
|
|
| 20 |
# Load the translation pipeline
|
| 21 |
translator = pipeline(task="translation", model=translation_models[selected_translation])
|
| 22 |
|
| 23 |
+
# Function to perform speech-to-text and translation
|
| 24 |
+
def speech_to_text_and_translate():
|
| 25 |
+
recognizer = sr.Recognizer()
|
| 26 |
+
|
| 27 |
+
st.info("Speak into your microphone...")
|
| 28 |
+
|
| 29 |
+
with sr.Microphone() as source:
|
| 30 |
+
audio_data = recognizer.listen(source, timeout=10)
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
text = recognizer.recognize_google(audio_data)
|
| 34 |
+
st.success(f"Speech-to-Text Result: {text}")
|
| 35 |
+
|
| 36 |
+
# Perform translation
|
| 37 |
+
translated_text = translator(text, max_length=500)[0]['translation_text']
|
| 38 |
+
st.success(f"Translated Text: {translated_text}")
|
| 39 |
+
|
| 40 |
+
except sr.UnknownValueError:
|
| 41 |
+
st.warning("Speech recognition could not understand audio.")
|
| 42 |
+
except sr.RequestError as e:
|
| 43 |
+
st.error(f"Error with the speech recognition service: {e}")
|
| 44 |
+
|
| 45 |
+
# Use speech-to-text and translation
|
| 46 |
+
if st.button("Start Speech-to-Text and Translation"):
|
| 47 |
+
speech_to_text_and_translate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|