Spaces:
Sleeping
Sleeping
import speech_recognition as sr | |
import platform | |
class VoiceHandler: | |
def __init__(self): | |
self.recognizer = sr.Recognizer() | |
self.is_cloud = self._is_cloud_environment() | |
if not self.is_cloud: | |
# Only configure microphone settings if not in cloud | |
self.recognizer.energy_threshold = 4000 | |
self.recognizer.dynamic_energy_threshold = True | |
self.recognizer.pause_threshold = 0.8 | |
def _is_cloud_environment(self): | |
"""Check if running in a cloud environment""" | |
# Check common cloud platform indicators | |
return ( | |
platform.system() == "Linux" and | |
not self._has_audio_device() | |
) | |
def _has_audio_device(self): | |
"""Check if system has an audio input device""" | |
try: | |
sr.Microphone() | |
return True | |
except (OSError, AttributeError): | |
return False | |
def listen_for_voice(self, language="mixed"): | |
""" | |
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 self.is_cloud: | |
return "Error: Voice input is not available in cloud deployment. Please use the text input option instead." | |
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...") | |
# Try Arabic first if mixed or Arabic is specified | |
if language in ["ar-SA", "mixed"]: | |
try: | |
text = self.recognizer.recognize_google(audio, language="ar-SA") | |
return text | |
except sr.UnknownValueError: | |
if language == "mixed": | |
# If Arabic fails and mixed is specified, try English | |
text = self.recognizer.recognize_google(audio, language="en-US") | |
return text | |
raise | |
else: | |
# English only | |
text = self.recognizer.recognize_google(audio, language="en-US") | |
return text | |
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 Exception as e: | |
return f"Error: {str(e)}" |