shamimjony1000 commited on
Commit
d93bc11
1 Parent(s): e621a44

Update voice_handler.py

Browse files
Files changed (1) hide show
  1. voice_handler.py +27 -4
voice_handler.py CHANGED
@@ -1,12 +1,32 @@
1
  import speech_recognition as sr
 
2
 
3
  class VoiceHandler:
4
  def __init__(self):
5
  self.recognizer = sr.Recognizer()
6
- # Adjust recognition settings for better accuracy
7
- self.recognizer.energy_threshold = 4000
8
- self.recognizer.dynamic_energy_threshold = True
9
- self.recognizer.pause_threshold = 0.8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def listen_for_voice(self, language="mixed"):
12
  """
@@ -16,6 +36,9 @@ class VoiceHandler:
16
  - "en-US" for English
17
  - "mixed" for both Arabic and English
18
  """
 
 
 
19
  try:
20
  with sr.Microphone() as source:
21
  print("Adjusting for ambient noise...")
 
1
  import speech_recognition as sr
2
+ import platform
3
 
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
 
31
  def listen_for_voice(self, language="mixed"):
32
  """
 
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:
44
  print("Adjusting for ambient noise...")