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

Update voice_handler.py

Browse files
Files changed (1) hide show
  1. voice_handler.py +15 -16
voice_handler.py CHANGED
@@ -1,5 +1,4 @@
1
  import speech_recognition as sr
2
- from typing import Optional
3
 
4
  class VoiceHandler:
5
  def __init__(self):
@@ -10,40 +9,34 @@ class VoiceHandler:
10
  self.permission_granted = False
11
 
12
  def check_microphone_access(self) -> bool:
13
- """Check if microphone is accessible"""
14
  try:
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,16 +55,18 @@ class VoiceHandler:
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:
69
  return "Listening timed out. Please try again."
 
 
70
  except Exception as e:
71
  return f"Error: {str(e)}"
72
 
73
  def _process_audio(self, audio, language: str) -> str:
74
- """Process audio input and convert to text"""
75
  if language in ["ar-SA", "mixed"]:
76
  try:
77
  return self.recognizer.recognize_google(audio, language="ar-SA")
@@ -80,3 +75,7 @@ class VoiceHandler:
80
  return self.recognizer.recognize_google(audio, language="en-US")
81
  raise
82
  return self.recognizer.recognize_google(audio, language="en-US")
 
 
 
 
 
1
  import speech_recognition as sr
 
2
 
3
  class VoiceHandler:
4
  def __init__(self):
 
9
  self.permission_granted = False
10
 
11
  def check_microphone_access(self) -> bool:
12
+ """Check if microphone is accessible."""
13
  try:
14
  with sr.Microphone() as source:
15
  self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
16
  return True
17
  except OSError as e:
18
+ print("OSError: No Default Input Device Available. This may occur if the microphone is not connected or accessible.")
19
+ print(f"Details: {str(e)}")
 
 
 
 
 
20
  return False
21
  except Exception as e:
22
+ print(f"Unexpected error while accessing microphone: {str(e)}")
23
  return False
24
 
25
  def request_permissions(self) -> bool:
26
+ """Request microphone permissions."""
27
  try:
28
  if self.check_microphone_access():
29
  self.permission_granted = True
30
  return True
 
31
  return False
32
  except Exception as e:
33
+ print(f"Error requesting permissions: {str(e)}")
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
 
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 speak clearly and try again."
61
  except sr.WaitTimeoutError:
62
  return "Listening timed out. Please try again."
63
+ except OSError as e:
64
+ return "OSError: No Default Input Device Available. Please check your microphone connection."
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
  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"))