Spaces:
Sleeping
Sleeping
shamimjony1000
commited on
Commit
•
f896422
1
Parent(s):
201991a
Update voice_handler.py
Browse files- voice_handler.py +5 -18
voice_handler.py
CHANGED
@@ -15,7 +15,7 @@ class VoiceHandler:
|
|
15 |
self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
|
16 |
return True
|
17 |
except OSError as e:
|
18 |
-
print("OSError:
|
19 |
print(f"Details: {str(e)}")
|
20 |
return False
|
21 |
except Exception as e:
|
@@ -34,16 +34,9 @@ class VoiceHandler:
|
|
34 |
return False
|
35 |
|
36 |
def listen_for_voice(self, language: str = "mixed") -> str:
|
37 |
-
"""
|
38 |
-
Listen for voice input in specified language.
|
39 |
-
language can be:
|
40 |
-
- "ar-SA" for Arabic
|
41 |
-
- "en-US" for English
|
42 |
-
- "mixed" for both Arabic and English
|
43 |
-
"""
|
44 |
if not self.permission_granted and not self.request_permissions():
|
45 |
-
return "Error:
|
46 |
-
|
47 |
try:
|
48 |
with sr.Microphone() as source:
|
49 |
print("Adjusting for ambient noise...")
|
@@ -51,22 +44,20 @@ class VoiceHandler:
|
|
51 |
print("Listening...")
|
52 |
audio = self.recognizer.listen(source, timeout=5, phrase_time_limit=10)
|
53 |
print("Processing speech...")
|
54 |
-
|
55 |
return self._process_audio(audio, language)
|
56 |
|
57 |
except sr.RequestError as e:
|
58 |
return f"Could not request results from speech service: {str(e)}"
|
59 |
except sr.UnknownValueError:
|
60 |
-
return "Could not understand audio. Please
|
61 |
except sr.WaitTimeoutError:
|
62 |
return "Listening timed out. Please try again."
|
63 |
except OSError as e:
|
64 |
-
return "OSError:
|
65 |
except Exception as e:
|
66 |
return f"Error: {str(e)}"
|
67 |
|
68 |
def _process_audio(self, audio, language: str) -> str:
|
69 |
-
"""Process audio input and convert to text."""
|
70 |
if language in ["ar-SA", "mixed"]:
|
71 |
try:
|
72 |
return self.recognizer.recognize_google(audio, language="ar-SA")
|
@@ -75,7 +66,3 @@ class VoiceHandler:
|
|
75 |
return self.recognizer.recognize_google(audio, language="en-US")
|
76 |
raise
|
77 |
return self.recognizer.recognize_google(audio, language="en-US")
|
78 |
-
|
79 |
-
# Usage example:
|
80 |
-
# voice_handler = VoiceHandler()
|
81 |
-
# print(voice_handler.listen_for_voice(language="en-US"))
|
|
|
15 |
self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
|
16 |
return True
|
17 |
except OSError as e:
|
18 |
+
print("OSError: Microphone not accessible. Please check your microphone connection and ALSA configuration.")
|
19 |
print(f"Details: {str(e)}")
|
20 |
return False
|
21 |
except Exception as e:
|
|
|
34 |
return False
|
35 |
|
36 |
def listen_for_voice(self, language: str = "mixed") -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if not self.permission_granted and not self.request_permissions():
|
38 |
+
return "Error: Microphone permissions required."
|
39 |
+
|
40 |
try:
|
41 |
with sr.Microphone() as source:
|
42 |
print("Adjusting for ambient noise...")
|
|
|
44 |
print("Listening...")
|
45 |
audio = self.recognizer.listen(source, timeout=5, phrase_time_limit=10)
|
46 |
print("Processing speech...")
|
|
|
47 |
return self._process_audio(audio, language)
|
48 |
|
49 |
except sr.RequestError as e:
|
50 |
return f"Could not request results from speech service: {str(e)}"
|
51 |
except sr.UnknownValueError:
|
52 |
+
return "Could not understand audio. Please try again."
|
53 |
except sr.WaitTimeoutError:
|
54 |
return "Listening timed out. Please try again."
|
55 |
except OSError as e:
|
56 |
+
return "OSError: Microphone not accessible. Check your connection and ALSA configuration."
|
57 |
except Exception as e:
|
58 |
return f"Error: {str(e)}"
|
59 |
|
60 |
def _process_audio(self, audio, language: str) -> str:
|
|
|
61 |
if language in ["ar-SA", "mixed"]:
|
62 |
try:
|
63 |
return self.recognizer.recognize_google(audio, language="ar-SA")
|
|
|
66 |
return self.recognizer.recognize_google(audio, language="en-US")
|
67 |
raise
|
68 |
return self.recognizer.recognize_google(audio, language="en-US")
|
|
|
|
|
|
|
|