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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -33
app.py CHANGED
@@ -1,9 +1,6 @@
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,38 +10,24 @@ def speak(text):
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():
@@ -69,3 +52,4 @@ iface = gr.Interface(fn=voice_login, inputs=[], outputs="text", live=True)
69
 
70
  # Launch the interface
71
  iface.launch()
 
 
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
  engine.say(text)
11
  engine.runAndWait()
12
 
13
+ # Function to listen to user voice input using SpeechRecognition
14
  def listen():
15
  recognizer = sr.Recognizer()
16
+ with sr.Microphone() as source:
17
+ print("Listening for your input...")
18
+ recognizer.adjust_for_ambient_noise(source)
19
+ audio = recognizer.listen(source)
20
+ print("Recognizing...")
21
+ try:
22
+ text = recognizer.recognize_google(audio)
23
+ print(f"Recognized text: {text}")
24
+ return text
25
+ except sr.UnknownValueError:
26
+ print("Sorry, I could not understand the audio.")
27
+ return None
28
+ except sr.RequestError:
29
+ print("Sorry, the speech recognition service is down.")
30
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Define the function for Gradio interface
33
  def voice_login():
 
52
 
53
  # Launch the interface
54
  iface.launch()
55
+