shamimjony1000 commited on
Commit
081fb8f
·
verified ·
1 Parent(s): ed3c183

Update voice_handler.py

Browse files
Files changed (1) hide show
  1. voice_handler.py +57 -13
voice_handler.py CHANGED
@@ -1,6 +1,8 @@
1
  import speech_recognition as sr
2
  import streamlit as st
3
  from typing import Optional, Tuple
 
 
4
 
5
  class VoiceHandler:
6
  def __init__(self):
@@ -9,17 +11,47 @@ class VoiceHandler:
9
  self.recognizer.dynamic_energy_threshold = True
10
  self.recognizer.pause_threshold = 0.8
11
  self.permission_granted = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def check_microphone_access(self) -> Tuple[bool, str]:
14
  """Check if microphone is accessible and return status with message"""
15
  try:
16
- with sr.Microphone() as source:
 
17
  self.recognizer.adjust_for_ambient_noise(source, duration=0.1)
18
  return True, "Microphone access granted"
19
- except (OSError, AttributeError) as e:
20
- return False, "Could not access microphone. Please check your browser settings and microphone connection."
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  except sr.RequestError as e:
22
- return False, f"Error accessing speech service: {str(e)}"
23
  except Exception as e:
24
  return False, f"Unexpected error: {str(e)}"
25
 
@@ -28,7 +60,17 @@ class VoiceHandler:
28
  success, message = self.check_microphone_access()
29
  if success:
30
  self.permission_granted = True
31
- return success, message
 
 
 
 
 
 
 
 
 
 
32
 
33
  def listen_for_voice(self, language: str = "mixed") -> str:
34
  """
@@ -38,23 +80,25 @@ class VoiceHandler:
38
  - "en-US" for English
39
  - "mixed" for both Arabic and English
40
  """
41
- # Check permissions first
 
 
42
  if not self.permission_granted:
43
  success, message = self.request_permissions()
44
  if not success:
45
  st.error(message)
46
  st.markdown("""
47
- ### Troubleshooting Steps:
48
- 1. Click the lock/info icon in your browser's address bar
49
- 2. Ensure microphone access is allowed for this site
50
- 3. Check if your microphone is properly connected
51
- 4. Try refreshing the page
52
- 5. If using Chrome, go to Settings > Privacy and Security > Site Settings > Microphone
53
  """)
54
  return message
55
 
56
  try:
57
- with sr.Microphone() as source:
58
  st.info("Adjusting for ambient noise... Please wait.")
59
  self.recognizer.adjust_for_ambient_noise(source, duration=1)
60
 
 
1
  import speech_recognition as sr
2
  import streamlit as st
3
  from typing import Optional, Tuple
4
+ import platform
5
+ import sys
6
 
7
  class VoiceHandler:
8
  def __init__(self):
 
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)}"
55
  except Exception as e:
56
  return False, f"Unexpected error: {str(e)}"
57
 
 
60
  success, message = self.check_microphone_access()
61
  if success:
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
  - "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:
89
  st.error(message)
90
  st.markdown("""
91
+ ### 🎤 Troubleshooting Steps:
92
+ 1. Check browser compatibility (Chrome/Firefox/Edge recommended)
93
+ 2. Verify microphone connection
94
+ 3. Check system sound settings
95
+ 4. Try a different microphone if available
96
+ 5. Restart browser if needed
97
  """)
98
  return message
99
 
100
  try:
101
+ with self.mic as source:
102
  st.info("Adjusting for ambient noise... Please wait.")
103
  self.recognizer.adjust_for_ambient_noise(source, duration=1)
104