Spaces:
Sleeping
Sleeping
Update app.py (#35)
Browse files- Update app.py (51c6789955837c2814183b51caa8500a0ea3874d)
app.py
CHANGED
|
@@ -104,28 +104,52 @@ def get_groq_response(query):
|
|
| 104 |
return "Error in API request."
|
| 105 |
|
| 106 |
# Function for speech recognition
|
|
|
|
| 107 |
def listen_to_speech():
|
| 108 |
recognizer = sr.Recognizer()
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
st.error("
|
| 121 |
-
return None
|
| 122 |
-
except sr.RequestError:
|
| 123 |
-
st.error("Could not request results from Google Speech Recognition service.")
|
| 124 |
-
return None
|
| 125 |
-
except Exception as e:
|
| 126 |
-
st.error(f"An error occurred: {e}")
|
| 127 |
return None
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# Function to check if the text is a greeting
|
| 130 |
def is_greeting(text):
|
| 131 |
greetings = ["hello", "hi", "hey", "good morning", "good afternoon", "good evening", "hola"]
|
|
|
|
| 104 |
return "Error in API request."
|
| 105 |
|
| 106 |
# Function for speech recognition
|
| 107 |
+
# Function for speech recognition with automatic device detection
|
| 108 |
def listen_to_speech():
|
| 109 |
recognizer = sr.Recognizer()
|
| 110 |
+
|
| 111 |
+
# Check available microphone devices
|
| 112 |
+
try:
|
| 113 |
+
audio = pyaudio.PyAudio()
|
| 114 |
+
input_devices = []
|
| 115 |
+
for i in range(audio.get_device_count()):
|
| 116 |
+
device_info = audio.get_device_info_by_index(i)
|
| 117 |
+
if device_info['maxInputChannels'] > 0:
|
| 118 |
+
input_devices.append((i, device_info['name']))
|
| 119 |
+
|
| 120 |
+
if not input_devices:
|
| 121 |
+
st.error("❌ No input devices found. Please connect a microphone.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
return None
|
| 123 |
|
| 124 |
+
default_device_index = input_devices[0][0]
|
| 125 |
+
device_name = input_devices[0][1]
|
| 126 |
+
st.info(f"🎙 Using microphone: {device_name}")
|
| 127 |
+
|
| 128 |
+
with sr.Microphone(device_index=default_device_index) as source:
|
| 129 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 130 |
+
st.write("Listening... 🎧")
|
| 131 |
+
|
| 132 |
+
try:
|
| 133 |
+
audio_data = recognizer.listen(source, timeout=5, phrase_time_limit=10)
|
| 134 |
+
st.write("Recognizing... 🧠")
|
| 135 |
+
text = recognizer.recognize_google(audio_data)
|
| 136 |
+
st.success(f"Recognized: {text}")
|
| 137 |
+
return text
|
| 138 |
+
except sr.UnknownValueError:
|
| 139 |
+
st.warning("Sorry, I could not understand the audio.")
|
| 140 |
+
return None
|
| 141 |
+
except sr.RequestError:
|
| 142 |
+
st.error("Could not request results from Google Speech Recognition service.")
|
| 143 |
+
return None
|
| 144 |
+
except Exception as e:
|
| 145 |
+
st.error(f"An unexpected error occurred: {e}")
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
+
except Exception as e:
|
| 149 |
+
st.error(f"Microphone access failed: {e}")
|
| 150 |
+
return None
|
| 151 |
+
|
| 152 |
+
|
| 153 |
# Function to check if the text is a greeting
|
| 154 |
def is_greeting(text):
|
| 155 |
greetings = ["hello", "hi", "hey", "good morning", "good afternoon", "good evening", "hola"]
|