Spaces:
Sleeping
Sleeping
File size: 3,374 Bytes
df9db76 07730d1 cf292fb d93bc11 c8b7640 201991a d93bc11 c8b7640 795e681 c8b7640 129d88f 201991a 129d88f 201991a c8b7640 795e681 c8b7640 201991a c8b7640 129d88f 201991a c8b7640 df9db76 795e681 df9db76 201991a df9db76 c8b7640 795e681 df9db76 c8b7640 df9db76 c8b7640 df9db76 c8b7640 df9db76 795e681 df9db76 201991a df9db76 c8b7640 df9db76 c8b7640 201991a df9db76 c8b7640 795e681 201991a 795e681 129d88f 201991a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import speech_recognition as sr
class VoiceHandler:
def __init__(self):
self.recognizer = sr.Recognizer()
self.recognizer.energy_threshold = 4000
self.recognizer.dynamic_energy_threshold = True
self.recognizer.pause_threshold = 0.8
self.permission_granted = False
def check_microphone_access(self) -> bool:
"""Check if microphone is accessible."""
try:
with sr.Microphone() as source:
self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
return True
except OSError as e:
print("OSError: No Default Input Device Available. This may occur if the microphone is not connected or accessible.")
print(f"Details: {str(e)}")
return False
except Exception as e:
print(f"Unexpected error while accessing microphone: {str(e)}")
return False
def request_permissions(self) -> bool:
"""Request microphone permissions."""
try:
if self.check_microphone_access():
self.permission_granted = True
return True
return False
except Exception as e:
print(f"Error requesting permissions: {str(e)}")
return False
def listen_for_voice(self, language: str = "mixed") -> str:
"""
Listen for voice input in specified language.
language can be:
- "ar-SA" for Arabic
- "en-US" for English
- "mixed" for both Arabic and English
"""
if not self.permission_granted and not self.request_permissions():
return "Error: Please grant microphone permissions to use voice input."
try:
with sr.Microphone() as source:
print("Adjusting for ambient noise...")
self.recognizer.adjust_for_ambient_noise(source, duration=1)
print("Listening...")
audio = self.recognizer.listen(source, timeout=5, phrase_time_limit=10)
print("Processing speech...")
return self._process_audio(audio, language)
except sr.RequestError as e:
return f"Could not request results from speech service: {str(e)}"
except sr.UnknownValueError:
return "Could not understand audio. Please speak clearly and try again."
except sr.WaitTimeoutError:
return "Listening timed out. Please try again."
except OSError as e:
return "OSError: No Default Input Device Available. Please check your microphone connection."
except Exception as e:
return f"Error: {str(e)}"
def _process_audio(self, audio, language: str) -> str:
"""Process audio input and convert to text."""
if language in ["ar-SA", "mixed"]:
try:
return self.recognizer.recognize_google(audio, language="ar-SA")
except sr.UnknownValueError:
if language == "mixed":
return self.recognizer.recognize_google(audio, language="en-US")
raise
return self.recognizer.recognize_google(audio, language="en-US")
# Usage example:
# voice_handler = VoiceHandler()
# print(voice_handler.listen_for_voice(language="en-US"))
|