Spaces:
Sleeping
Sleeping
shahpalash10
commited on
Commit
•
1eadbef
1
Parent(s):
43cd868
Update app.py
Browse files
app.py
CHANGED
@@ -10,55 +10,53 @@ pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
|
10 |
# Initialize the SpeechRecognition recognizer
|
11 |
recognizer = sr.Recognizer()
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
p = pyaudio.PyAudio()
|
18 |
-
stream = None
|
19 |
|
20 |
# Check if the microphone input is requested
|
21 |
if st.checkbox("Use Microphone for English Input"):
|
22 |
with audio_input:
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
audio += stream.read(1024)
|
32 |
|
33 |
# Recognize the English speech
|
34 |
english_text = recognizer.recognize_google(audio, language='en')
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
# Translate the English text to Hindi
|
39 |
-
out = pipe(english_text, src_lang='en', tgt_lang='hi')
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
finally:
|
60 |
-
# Clean up audio resources
|
61 |
-
if stream:
|
62 |
-
stream.stop_stream()
|
63 |
-
stream.close()
|
64 |
-
p.terminate()
|
|
|
10 |
# Initialize the SpeechRecognition recognizer
|
11 |
recognizer = sr.Recognizer()
|
12 |
|
13 |
+
# Find the microphone device index
|
14 |
+
def find_microphone_index():
|
15 |
+
p = pyaudio.PyAudio()
|
16 |
+
for i in range(p.get_device_count()):
|
17 |
+
device_info = p.get_device_info_by_index(i)
|
18 |
+
if "microphone" in device_info["name"].lower():
|
19 |
+
return i
|
20 |
+
return None
|
21 |
+
|
22 |
+
# Get the microphone device index
|
23 |
+
microphone_index = find_microphone_index()
|
24 |
|
25 |
+
audio_input = st.empty()
|
|
|
|
|
26 |
|
27 |
# Check if the microphone input is requested
|
28 |
if st.checkbox("Use Microphone for English Input"):
|
29 |
with audio_input:
|
30 |
+
if microphone_index is None:
|
31 |
+
st.warning("No microphone found. Please make sure your microphone is connected.")
|
32 |
+
else:
|
33 |
+
st.warning("Listening for audio input... Speak in English.")
|
34 |
+
try:
|
35 |
+
with sr.Microphone(device_index=microphone_index) as source:
|
36 |
+
audio = recognizer.listen(source)
|
37 |
+
st.success("Audio input recorded. Translating...")
|
|
|
38 |
|
39 |
# Recognize the English speech
|
40 |
english_text = recognizer.recognize_google(audio, language='en')
|
41 |
|
42 |
+
# Translate the English text to Hindi
|
43 |
+
out = pipe(english_text, src_lang='en', tgt_lang='hi')
|
|
|
|
|
44 |
|
45 |
+
# Extract the translation
|
46 |
+
translation_text = out[0]['translation_text']
|
47 |
+
st.text(f"English Input: {english_text}")
|
48 |
+
st.text(f"Hindi Translation: {translation_text}")
|
49 |
|
50 |
+
# Convert the translated text to speech
|
51 |
+
tts = gTTS(translation_text, lang='hi')
|
52 |
+
tts.save("translated_audio.mp3")
|
53 |
|
54 |
+
# Display the audio player for listening to the speech
|
55 |
+
st.audio("translated_audio.mp3", format='audio/mp3')
|
56 |
|
57 |
+
except sr.WaitTimeoutError:
|
58 |
+
st.warning("No speech detected. Please speak into the microphone.")
|
59 |
+
except sr.RequestError as e:
|
60 |
+
st.error(f"Could not request results from Google Speech Recognition service: {e}")
|
61 |
+
except sr.UnknownValueError:
|
62 |
+
st.warning("Speech recognition could not understand the audio.")
|
|
|
|
|
|
|
|
|
|
|
|