shamimjony1000 commited on
Commit
42892f4
1 Parent(s): 1157a1c

Update voice_handler.py

Browse files
Files changed (1) hide show
  1. voice_handler.py +11 -42
voice_handler.py CHANGED
@@ -11,44 +11,24 @@ class VoiceHandler:
11
  self.recognizer.dynamic_energy_threshold = True
12
  self.recognizer.pause_threshold = 0.8
13
  self.permission_granted = False
14
- self._init_microphone()
15
-
16
- def _init_microphone(self):
17
- """Initialize microphone with proper settings based on platform"""
18
- try:
19
- # List available microphones
20
- mics = sr.Microphone.list_microphone_names()
21
- if not mics:
22
- st.error("No microphones detected. Please connect a microphone and refresh the page.")
23
- return
24
-
25
- # Use default microphone
26
- self.mic = sr.Microphone(device_index=None)
27
-
28
- except Exception as e:
29
- st.error(f"Error initializing microphone: {str(e)}")
30
 
31
  def check_microphone_access(self) -> Tuple[bool, str]:
32
  """Check if microphone is accessible and return status with message"""
33
  try:
34
  with self.mic as source:
35
- # Shorter duration for initial test
36
  self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
37
  return True, "Microphone access granted"
38
- except AttributeError:
39
  return False, """
40
- Microphone not initialized properly. Please ensure:
41
- 1. You have a working microphone connected
42
- 2. Your browser supports audio input
43
- 3. You're using a modern browser (Chrome, Firefox, Edge)
44
- """
45
- except OSError as e:
46
- return False, f"""
47
- Could not access microphone. Error: {str(e)}
48
- Please check:
49
- 1. Microphone is properly connected
50
- 2. No other application is using the microphone
51
- 3. Browser has permission to access microphone
52
  """
53
  except sr.RequestError as e:
54
  return False, f"Speech recognition service error: {str(e)}"
@@ -62,15 +42,7 @@ class VoiceHandler:
62
  self.permission_granted = True
63
  return True, "Microphone access granted successfully"
64
 
65
- return False, f"""
66
- Microphone access denied. Please:
67
- 1. Click the lock/camera icon in your browser's address bar
68
- 2. Select 'Allow' for microphone access
69
- 3. Refresh the page
70
- 4. If using Chrome, verify settings at chrome://settings/content/microphone
71
-
72
- Error details: {message}
73
- """
74
 
75
  def listen_for_voice(self, language: str = "mixed") -> str:
76
  """
@@ -80,9 +52,6 @@ class VoiceHandler:
80
  - "en-US" for English
81
  - "mixed" for both Arabic and English
82
  """
83
- if not hasattr(self, 'mic'):
84
- return "Error: Microphone not properly initialized"
85
-
86
  if not self.permission_granted:
87
  success, message = self.request_permissions()
88
  if not success:
 
11
  self.recognizer.dynamic_energy_threshold = True
12
  self.recognizer.pause_threshold = 0.8
13
  self.permission_granted = False
14
+
15
+ # Initialize microphone without device selection
16
+ self.mic = sr.Microphone()
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def check_microphone_access(self) -> Tuple[bool, str]:
19
  """Check if microphone is accessible and return status with message"""
20
  try:
21
  with self.mic as source:
22
+ # Quick test to verify microphone access
23
  self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
24
  return True, "Microphone access granted"
25
+ except (OSError, AttributeError) as e:
26
  return False, """
27
+ Could not access microphone. Please:
28
+ 1. Click the lock icon in your browser's address bar
29
+ 2. Allow microphone access for this site
30
+ 3. Refresh the page after granting access
31
+ 4. Ensure your microphone is properly connected
 
 
 
 
 
 
 
32
  """
33
  except sr.RequestError as e:
34
  return False, f"Speech recognition service error: {str(e)}"
 
42
  self.permission_granted = True
43
  return True, "Microphone access granted successfully"
44
 
45
+ return False, message
 
 
 
 
 
 
 
 
46
 
47
  def listen_for_voice(self, language: str = "mixed") -> str:
48
  """
 
52
  - "en-US" for English
53
  - "mixed" for both Arabic and English
54
  """
 
 
 
55
  if not self.permission_granted:
56
  success, message = self.request_permissions()
57
  if not success: