shahpalash10 commited on
Commit
a93b78a
1 Parent(s): 59a0f95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
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')
@@ -18,16 +18,19 @@ 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')
@@ -50,3 +53,8 @@ if st.checkbox("Use Microphone for English Input"):
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.")
 
 
 
 
 
 
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  import speech_recognition as sr
5
+ import pyaudio
6
 
7
  # Create a translation pipeline
8
  pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
 
18
  with audio_input:
19
  st.warning("Listening for audio input... Speak in English.")
20
  try:
21
+ with sr.Microphone() as source:
22
+ # Adjust the microphone settings as needed
23
+ p = pyaudio.PyAudio()
24
+ stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
 
25
 
26
+ audio = b""
27
+ for _ in range(0, int(16000 / 1024 * 5)): # Adjust the recording duration as needed
28
+ audio += stream.read(1024)
29
+
30
+ # Recognize the English speech
31
+ english_text = recognizer.recognize_google(audio, language='en')
32
 
33
+ st.success("Audio input recorded. Translating...")
 
34
 
35
  # Translate the English text to Hindi
36
  out = pipe(english_text, src_lang='en', tgt_lang='hi')
 
53
  st.error(f"Could not request results from Google Speech Recognition service: {e}")
54
  except sr.UnknownValueError:
55
  st.warning("Speech recognition could not understand the audio.")
56
+ finally:
57
+ # Clean up audio resources
58
+ stream.stop_stream()
59
+ stream.close()
60
+ p.terminate()