shahpalash10 commited on
Commit
1eadbef
1 Parent(s): 43cd868

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -39
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
- # Create a Streamlit input element for microphone input
14
- audio_input = st.empty()
 
 
 
 
 
 
 
 
 
15
 
16
- # Initialize PyAudio and stream variables
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
- st.warning("Listening for audio input... Speak in English.")
24
- try:
25
- with sr.Microphone() as source:
26
- # Open the audio stream
27
- stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
28
-
29
- audio = b""
30
- for _ in range(0, int(16000 / 1024 * 5)): # Adjust the recording duration as needed
31
- audio += stream.read(1024)
32
 
33
  # Recognize the English speech
34
  english_text = recognizer.recognize_google(audio, language='en')
35
 
36
- st.success("Audio input recorded. Translating...")
37
-
38
- # Translate the English text to Hindi
39
- out = pipe(english_text, src_lang='en', tgt_lang='hi')
40
 
41
- # Extract the translation
42
- translation_text = out[0]['translation_text']
43
- st.text(f"English Input: {english_text}")
44
- st.text(f"Hindi Translation: {translation_text}")
45
 
46
- # Convert the translated text to speech
47
- tts = gTTS(translation_text, lang='hi')
48
- tts.save("translated_audio.mp3")
49
 
50
- # Display the audio player for listening to the speech
51
- st.audio("translated_audio.mp3", format='audio/mp3')
52
 
53
- except sr.WaitTimeoutError:
54
- st.warning("No speech detected. Please speak into the microphone.")
55
- except sr.RequestError as e:
56
- st.error(f"Could not request results from Google Speech Recognition service: {e}")
57
- except sr.UnknownValueError:
58
- st.warning("Speech recognition could not understand the audio.")
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.")