shamimjony1000 commited on
Commit
07730d1
1 Parent(s): 9fe5d26

Update voice_handler.py

Browse files
Files changed (1) hide show
  1. voice_handler.py +12 -21
voice_handler.py CHANGED
@@ -4,27 +4,17 @@ import platform
4
  class VoiceHandler:
5
  def __init__(self):
6
  self.recognizer = sr.Recognizer()
7
- self.is_cloud = self._is_cloud_environment()
8
-
9
- if not self.is_cloud:
10
- # Only configure microphone settings if not in cloud
11
- self.recognizer.energy_threshold = 4000
12
- self.recognizer.dynamic_energy_threshold = True
13
- self.recognizer.pause_threshold = 0.8
14
 
15
- def _is_cloud_environment(self):
16
- """Check if running in a cloud environment"""
17
- # Check common cloud platform indicators
18
- return (
19
- platform.system() == "Linux" and
20
- not self._has_audio_device()
21
- )
22
-
23
- def _has_audio_device(self):
24
- """Check if system has an audio input device"""
25
  try:
26
- sr.Microphone()
27
- return True
 
28
  except (OSError, AttributeError):
29
  return False
30
 
@@ -36,8 +26,9 @@ class VoiceHandler:
36
  - "en-US" for English
37
  - "mixed" for both Arabic and English
38
  """
39
- if self.is_cloud:
40
- return "Error: Voice input is not available in cloud deployment. Please use the text input option instead."
 
41
 
42
  try:
43
  with sr.Microphone() as source:
 
4
  class VoiceHandler:
5
  def __init__(self):
6
  self.recognizer = sr.Recognizer()
7
+ self.recognizer.energy_threshold = 4000
8
+ self.recognizer.dynamic_energy_threshold = True
9
+ self.recognizer.pause_threshold = 0.8
10
+ self.permission_granted = False
 
 
 
11
 
12
+ def request_permissions(self):
13
+ """Request microphone permissions from the browser"""
 
 
 
 
 
 
 
 
14
  try:
15
+ with sr.Microphone() as source:
16
+ self.permission_granted = True
17
+ return True
18
  except (OSError, AttributeError):
19
  return False
20
 
 
26
  - "en-US" for English
27
  - "mixed" for both Arabic and English
28
  """
29
+ if not self.permission_granted:
30
+ if not self.request_permissions():
31
+ return "Error: Please grant microphone permissions to use voice input."
32
 
33
  try:
34
  with sr.Microphone() as source: