andito HF staff commited on
Commit
c77f359
1 Parent(s): c342d0f

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. audio_streaming_client.py +7 -12
  2. baseHandler.py +3 -0
audio_streaming_client.py CHANGED
@@ -10,7 +10,7 @@ from dataclasses import dataclass, field
10
  @dataclass
11
  class AudioStreamingClientArguments:
12
  sample_rate: int = field(default=16000, metadata={"help": "Audio sample rate in Hz. Default is 16000."})
13
- chunk_size: int = field(default=512, metadata={"help": "The size of audio chunks in samples. Default is 1024."})
14
  api_url: str = field(default="https://yxfmjcvuzgi123sw.us-east-1.aws.endpoints.huggingface.cloud", metadata={"help": "The URL of the API endpoint."})
15
  auth_token: str = field(default="your_auth_token", metadata={"help": "Authentication token for the API."})
16
 
@@ -56,19 +56,14 @@ class AudioStreamingClient:
56
  while not self.stop_event.is_set():
57
  if self.session_state != "processing" and not self.send_queue.empty():
58
  chunk = self.send_queue.get().tobytes()
59
- buffer += chunk
60
-
61
- # Calculate energy of the audio chunk
62
- energy = np.sum(np.square(np.frombuffer(chunk, dtype=np.int16))) / len(chunk)
63
- print(f"Energy: {energy}")
64
-
65
- if energy > 0.01: # Threshold for energy detection
66
- if len(buffer) >= self.args.chunk_size * 2: # * 2 because of int16
67
- self.send_request(buffer)
68
- buffer = b''
69
  else:
70
  self.send_request()
71
- time.sleep(0.1)
72
 
73
  def send_request(self, audio_data=None):
74
  payload = {"input_type": "speech",
 
10
  @dataclass
11
  class AudioStreamingClientArguments:
12
  sample_rate: int = field(default=16000, metadata={"help": "Audio sample rate in Hz. Default is 16000."})
13
+ chunk_size: int = field(default=2048, metadata={"help": "The size of audio chunks in samples. Default is 1024."})
14
  api_url: str = field(default="https://yxfmjcvuzgi123sw.us-east-1.aws.endpoints.huggingface.cloud", metadata={"help": "The URL of the API endpoint."})
15
  auth_token: str = field(default="your_auth_token", metadata={"help": "Authentication token for the API."})
16
 
 
56
  while not self.stop_event.is_set():
57
  if self.session_state != "processing" and not self.send_queue.empty():
58
  chunk = self.send_queue.get().tobytes()
59
+ buffer += chunk
60
+ if len(buffer) >= self.args.chunk_size * 2: # * 2 because of int16
61
+ self.send_request(buffer)
62
+ buffer = b''
63
+ time.sleep(4*self.args.chunk_size/self.args.sample_rate)
 
 
 
 
 
64
  else:
65
  self.send_request()
66
+ time.sleep(4*self.args.chunk_size/self.args.sample_rate)
67
 
68
  def send_request(self, audio_data=None):
69
  payload = {"input_type": "speech",
baseHandler.py CHANGED
@@ -1,7 +1,9 @@
1
  from time import perf_counter
2
  import logging
 
3
 
4
  logger = logging.getLogger(__name__)
 
5
 
6
 
7
  class BaseHandler:
@@ -37,6 +39,7 @@ class BaseHandler:
37
  for output in self.process(input):
38
  self._times.append(perf_counter() - start_time)
39
  if self.last_time > self.min_time_to_debug:
 
40
  logger.debug(f"{self.__class__.__name__}: {self.last_time: .3f} s")
41
  self.queue_out.put(output)
42
  start_time = perf_counter()
 
1
  from time import perf_counter
2
  import logging
3
+ from rich.console import Console
4
 
5
  logger = logging.getLogger(__name__)
6
+ console = Console()
7
 
8
 
9
  class BaseHandler:
 
39
  for output in self.process(input):
40
  self._times.append(perf_counter() - start_time)
41
  if self.last_time > self.min_time_to_debug:
42
+ console.print(f"{self.__class__.__name__}: {self.last_time: .3f} s")
43
  logger.debug(f"{self.__class__.__name__}: {self.last_time: .3f} s")
44
  self.queue_out.put(output)
45
  start_time = perf_counter()