shamimjony1000 commited on
Commit
129d88f
1 Parent(s): cf292fb

Update voice_handler.py

Browse files
Files changed (1) hide show
  1. voice_handler.py +18 -7
voice_handler.py CHANGED
@@ -1,5 +1,4 @@
1
  import speech_recognition as sr
2
- import platform
3
  from typing import Optional
4
 
5
  class VoiceHandler:
@@ -16,23 +15,35 @@ class VoiceHandler:
16
  with sr.Microphone() as source:
17
  self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
18
  return True
19
- except (OSError, AttributeError, sr.RequestError):
 
 
 
 
 
 
 
 
 
 
20
  return False
21
 
22
  def request_permissions(self) -> bool:
23
- """Request microphone permissions from the browser"""
24
  try:
25
  if self.check_microphone_access():
26
  self.permission_granted = True
27
  return True
 
28
  return False
29
- except Exception:
 
30
  return False
31
 
32
  def listen_for_voice(self, language: str = "mixed") -> str:
33
  """
34
  Listen for voice input in specified language.
35
- language can be:
36
  - "ar-SA" for Arabic
37
  - "en-US" for English
38
  - "mixed" for both Arabic and English
@@ -51,7 +62,7 @@ class VoiceHandler:
51
  return self._process_audio(audio, language)
52
 
53
  except sr.RequestError as e:
54
- return f"Could not request results from speech service: {str(e)}"
55
  except sr.UnknownValueError:
56
  return "Could not understand audio. Please speak clearly and try again."
57
  except sr.WaitTimeoutError:
@@ -68,4 +79,4 @@ class VoiceHandler:
68
  if language == "mixed":
69
  return self.recognizer.recognize_google(audio, language="en-US")
70
  raise
71
- return self.recognizer.recognize_google(audio, language="en-US")
 
1
  import speech_recognition as sr
 
2
  from typing import Optional
3
 
4
  class VoiceHandler:
 
15
  with sr.Microphone() as source:
16
  self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
17
  return True
18
+ except OSError as e:
19
+ print(f"OSError: {e}. This may occur if the microphone is not connected or accessible.")
20
+ return False
21
+ except AttributeError as e:
22
+ print(f"AttributeError: {e}. This may happen due to an issue with the 'sr.Microphone' configuration.")
23
+ return False
24
+ except sr.RequestError as e:
25
+ print(f"RequestError: {e}. This may happen if there's an issue with the SpeechRecognition service.")
26
+ return False
27
+ except Exception as e:
28
+ print(f"Unexpected error: {e}")
29
  return False
30
 
31
  def request_permissions(self) -> bool:
32
+ """Request microphone permissions"""
33
  try:
34
  if self.check_microphone_access():
35
  self.permission_granted = True
36
  return True
37
+ print("Error: Could not access microphone. Please check your system or browser permissions.")
38
  return False
39
+ except Exception as e:
40
+ print(f"Permission request error: {e}")
41
  return False
42
 
43
  def listen_for_voice(self, language: str = "mixed") -> str:
44
  """
45
  Listen for voice input in specified language.
46
+ language can be:
47
  - "ar-SA" for Arabic
48
  - "en-US" for English
49
  - "mixed" for both Arabic and English
 
62
  return self._process_audio(audio, language)
63
 
64
  except sr.RequestError as e:
65
+ return f"Could not request results from the speech service: {str(e)}"
66
  except sr.UnknownValueError:
67
  return "Could not understand audio. Please speak clearly and try again."
68
  except sr.WaitTimeoutError:
 
79
  if language == "mixed":
80
  return self.recognizer.recognize_google(audio, language="en-US")
81
  raise
82
+ return self.recognizer.recognize_google(audio, language="en-US")