shamimjony1000 commited on
Commit
c8b7640
β€’
1 Parent(s): e52cb87

Update voice_handler.py

Browse files
Files changed (1) hide show
  1. voice_handler.py +25 -60
voice_handler.py CHANGED
@@ -1,8 +1,6 @@
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,38 +9,25 @@ class VoiceHandler:
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)}"
35
- except Exception as e:
36
- return False, f"Unexpected error: {str(e)}"
37
 
38
- def request_permissions(self) -> Tuple[bool, str]:
39
  """Request microphone permissions from the browser"""
40
- success, message = self.check_microphone_access()
41
- if success:
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,47 +37,27 @@ class VoiceHandler:
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:
58
- st.error(message)
59
- st.markdown("""
60
- ### 🎀 Troubleshooting Steps:
61
- 1. Check browser compatibility (Chrome/Firefox/Edge recommended)
62
- 2. Verify microphone connection
63
- 3. Check system sound settings
64
- 4. Try a different microphone if available
65
- 5. Restart browser if needed
66
- """)
67
- return message
68
 
69
  try:
70
- with self.mic as source:
71
- st.info("Adjusting for ambient noise... Please wait.")
72
  self.recognizer.adjust_for_ambient_noise(source, duration=1)
73
-
74
- st.info("🎀 Listening... Speak now!")
75
  audio = self.recognizer.listen(source, timeout=5, phrase_time_limit=10)
 
76
 
77
- st.info("Processing speech...")
78
  return self._process_audio(audio, language)
79
 
80
  except sr.RequestError as e:
81
- error_msg = f"Could not request results from speech service: {str(e)}"
82
- st.error(error_msg)
83
- return error_msg
84
  except sr.UnknownValueError:
85
- error_msg = "Could not understand audio. Please speak clearly and try again."
86
- st.warning(error_msg)
87
- return error_msg
88
  except sr.WaitTimeoutError:
89
- error_msg = "Listening timed out. Please try again."
90
- st.warning(error_msg)
91
- return error_msg
92
  except Exception as e:
93
- error_msg = f"Error: {str(e)}"
94
- st.error(error_msg)
95
- return error_msg
96
 
97
  def _process_audio(self, audio, language: str) -> str:
98
  """Process audio input and convert to text"""
 
1
  import speech_recognition as sr
 
 
2
  import platform
3
+ from typing import Optional
4
 
5
  class VoiceHandler:
6
  def __init__(self):
 
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) -> bool:
14
+ """Check if microphone is accessible"""
15
  try:
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
  """
 
37
  - "en-US" for English
38
  - "mixed" for both Arabic and English
39
  """
40
+ if not self.permission_granted and not self.request_permissions():
41
+ return "Error: Please grant microphone permissions to use voice input."
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  try:
44
+ with sr.Microphone() as source:
45
+ print("Adjusting for ambient noise...")
46
  self.recognizer.adjust_for_ambient_noise(source, duration=1)
47
+ print("Listening...")
 
48
  audio = self.recognizer.listen(source, timeout=5, phrase_time_limit=10)
49
+ print("Processing speech...")
50
 
 
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:
58
+ return "Listening timed out. Please try again."
 
 
59
  except Exception as e:
60
+ return f"Error: {str(e)}"
 
 
61
 
62
  def _process_audio(self, audio, language: str) -> str:
63
  """Process audio input and convert to text"""