Spaces:
Sleeping
Sleeping
shahpalash10
commited on
Commit
•
3f918a6
1
Parent(s):
d24a73d
Update app.py
Browse files
app.py
CHANGED
@@ -2,44 +2,51 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
import speech_recognition as sr
|
|
|
5 |
|
6 |
# Create a translation pipeline
|
7 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
11 |
|
12 |
# Check if the microphone input is requested
|
13 |
if st.checkbox("Use Microphone for English Input"):
|
14 |
-
|
15 |
-
with sr.Microphone() as source:
|
16 |
st.warning("Listening for audio input... Speak in English.")
|
17 |
-
|
18 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
import speech_recognition as sr
|
5 |
+
import sounddevice as sd # Import sounddevice
|
6 |
|
7 |
# Create a translation pipeline
|
8 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
9 |
|
10 |
+
# Initialize the SpeechRecognition recognizer
|
11 |
+
recognizer = sr.Recognizer()
|
12 |
+
|
13 |
+
# Create a Streamlit input element for microphone input
|
14 |
+
audio_input = st.empty()
|
15 |
|
16 |
# Check if the microphone input is requested
|
17 |
if st.checkbox("Use Microphone for English Input"):
|
18 |
+
with audio_input:
|
|
|
19 |
st.warning("Listening for audio input... Speak in English.")
|
20 |
+
try:
|
21 |
+
# Replace pyaudio.Microphone with sd.InputStream
|
22 |
+
with sd.InputStream(callback=None, channels=1, dtype='int16', samplerate=16000):
|
23 |
+
with sr.AudioFile("temp.wav") as source: # Save audio to temp.wav
|
24 |
+
recognizer.adjust_for_ambient_noise(source)
|
25 |
+
audio = recognizer.listen(source)
|
26 |
+
|
27 |
+
st.success("Audio input recorded. Translating...")
|
28 |
+
|
29 |
+
# Recognize the English speech
|
30 |
+
english_text = recognizer.recognize_google(audio, language='en')
|
31 |
+
|
32 |
+
# Translate the English text to Hindi
|
33 |
+
out = pipe(english_text, src_lang='en', tgt_lang='hi')
|
34 |
+
|
35 |
+
# Extract the translation
|
36 |
+
translation_text = out[0]['translation_text']
|
37 |
+
st.text(f"English Input: {english_text}")
|
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')
|
46 |
+
|
47 |
+
except sr.WaitTimeoutError:
|
48 |
+
st.warning("No speech detected. Please speak into the microphone.")
|
49 |
+
except sr.RequestError as e:
|
50 |
+
st.error(f"Could not request results from Google Speech Recognition service: {e}")
|
51 |
+
except sr.UnknownValueError:
|
52 |
+
st.warning("Speech recognition could not understand the audio.")
|