Subbu1304 commited on
Commit
59b3579
Β·
verified Β·
1 Parent(s): 8be53af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import gradio as gr
2
  import speech_recognition as sr
3
  import pyttsx3
 
 
 
4
 
5
  # Initialize the speech engine
6
  engine = pyttsx3.init()
@@ -10,26 +13,38 @@ def speak(text):
10
  engine.say(text)
11
  engine.runAndWait()
12
 
13
- # Function to listen to user voice input
14
  def listen():
15
  recognizer = sr.Recognizer()
16
- microphone = sr.Microphone()
17
-
18
- with microphone as source:
19
- print("Listening for your input...")
20
- recognizer.adjust_for_ambient_noise(source)
21
- audio = recognizer.listen(source)
22
- print("Recognizing...")
23
- try:
24
- text = recognizer.recognize_google(audio)
25
- print(f"Recognized text: {text}")
26
- return text
27
- except sr.UnknownValueError:
28
- print("Sorry, I could not understand the audio.")
29
- return None
30
- except sr.RequestError:
31
- print("Sorry, the speech recognition service is down.")
32
- return None
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  # Define the function for Gradio interface
35
  def voice_login():
 
1
  import gradio as gr
2
  import speech_recognition as sr
3
  import pyttsx3
4
+ import sounddevice as sd
5
+ import numpy as np
6
+ import io
7
 
8
  # Initialize the speech engine
9
  engine = pyttsx3.init()
 
13
  engine.say(text)
14
  engine.runAndWait()
15
 
16
+ # Function to listen to user voice input using sounddevice
17
  def listen():
18
  recognizer = sr.Recognizer()
19
+
20
+ # Set up sounddevice recording parameters
21
+ fs = 16000 # Sampling frequency
22
+ duration = 5 # seconds to record
23
+ speak("Listening for your input...")
24
+
25
+ # Record audio
26
+ recording = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
27
+ sd.wait()
28
+
29
+ # Convert the recording to audio data for recognition
30
+ audio_data = np.array(recording, dtype=np.float32)
31
+ audio_data = io.BytesIO(audio_data)
32
+
33
+ # Use SpeechRecognition to process the audio
34
+ with sr.AudioData(audio_data, fs, 2) as source:
35
+ audio = recognizer.record(source)
36
+
37
+ print("Recognizing...")
38
+ try:
39
+ text = recognizer.recognize_google(audio)
40
+ print(f"Recognized text: {text}")
41
+ return text
42
+ except sr.UnknownValueError:
43
+ print("Sorry, I could not understand the audio.")
44
+ return None
45
+ except sr.RequestError:
46
+ print("Sorry, the speech recognition service is down.")
47
+ return None
48
 
49
  # Define the function for Gradio interface
50
  def voice_login():