Spaces:
Sleeping
Sleeping
Commit
·
837e23c
1
Parent(s):
924a44b
Update app.py
Browse files
app.py
CHANGED
@@ -6,42 +6,40 @@ import speech_recognition as sr
|
|
6 |
# Create a translation pipeline
|
7 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
# Create a Streamlit input element for microphone input
|
13 |
-
audio_input = st.empty()
|
14 |
|
15 |
# Check if the microphone input is requested
|
16 |
if st.checkbox("Use Microphone for English Input"):
|
17 |
-
|
|
|
18 |
st.warning("Listening for audio input... Speak in English.")
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
6 |
# Create a translation pipeline
|
7 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
8 |
|
9 |
+
# Create a Streamlit input element for text input
|
10 |
+
text_input = st.text_area("Enter some English text")
|
|
|
|
|
|
|
11 |
|
12 |
# Check if the microphone input is requested
|
13 |
if st.checkbox("Use Microphone for English Input"):
|
14 |
+
recognizer = sr.Recognizer()
|
15 |
+
with sr.Microphone() as source:
|
16 |
st.warning("Listening for audio input... Speak in English.")
|
17 |
+
audio = recognizer.listen(source)
|
18 |
+
st.success("Audio input recorded. Translating...")
|
19 |
+
|
20 |
+
# Recognize the English speech using Google Web Speech API
|
21 |
+
try:
|
22 |
+
english_text = recognizer.recognize_google(audio, language='en')
|
23 |
+
text_input = st.text_area("English Input", english_text)
|
24 |
+
except sr.WaitTimeoutError:
|
25 |
+
st.warning("No speech detected. Please speak into the microphone.")
|
26 |
+
except sr.RequestError as e:
|
27 |
+
st.error(f"Could not request results from Google Speech Recognition service: {e}")
|
28 |
+
except sr.UnknownValueError:
|
29 |
+
st.warning("Speech recognition could not understand the audio.")
|
30 |
+
|
31 |
+
if text_input:
|
32 |
+
# Translate the English text to Hindi
|
33 |
+
out = pipe(text_input, src_lang='en', tgt_lang='hi')
|
34 |
+
|
35 |
+
# Extract the translation
|
36 |
+
translation_text = out[0]['translation_text']
|
37 |
+
st.text(f"English Input: {text_input}")
|
38 |
+
st.text(f"Hindi Translation: {translation_text}")
|
39 |
+
|
40 |
+
# Convert the translated text to speech
|
41 |
+
tts = gTTS(translation_text, lang='hi')
|
42 |
+
tts.save("translated_audio.mp3")
|
43 |
+
|
44 |
+
# Display the audio player for listening to the speech
|
45 |
+
st.audio("translated_audio.mp3", format='audio/mp3')
|