Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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():
|