freddyaboulton HF staff commited on
Commit
a2ed330
1 Parent(s): 9e4771a
Files changed (2) hide show
  1. app.py +23 -21
  2. requirements.txt +1 -1
app.py CHANGED
@@ -3,18 +3,18 @@ import logging
3
  # Configure the root logger to WARNING to suppress debug messages from other libraries
4
  logging.basicConfig(level=logging.WARNING)
5
 
6
- # Create a console handler
7
- console_handler = logging.StreamHandler()
8
- console_handler.setLevel(logging.DEBUG)
9
 
10
- # Create a formatter
11
- formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
12
- console_handler.setFormatter(formatter)
13
 
14
  # Configure the logger for your specific library
15
  logger = logging.getLogger("gradio_webrtc")
16
  logger.setLevel(logging.DEBUG)
17
- logger.addHandler(console_handler)
18
 
19
 
20
  import base64
@@ -24,8 +24,7 @@ import tempfile
24
  import time
25
  import traceback
26
  from dataclasses import dataclass
27
- from queue import Queue
28
- from threading import Thread
29
 
30
  import gradio as gr
31
  import librosa
@@ -47,11 +46,11 @@ snapshot_download(repo_id, local_dir="./checkpoint", revision="main")
47
  IP = "0.0.0.0"
48
  PORT = 60808
49
 
50
- # thread = Thread(target=serve, daemon=True)
51
- # thread.start()
52
 
53
 
54
- API_URL = "https://freddyaboulton-omni-mini-webrtc-backend.hf.space/chat"
55
 
56
  account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
57
  auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
@@ -231,7 +230,7 @@ class OmniHandler(StreamHandler):
231
  super().__init__(
232
  expected_layout="mono", output_sample_rate=OUT_RATE, output_frame_size=480
233
  )
234
- self.chunk_queue = Queue()
235
  self.state = AppState()
236
  self.generator = None
237
  self.duration = 0
@@ -241,22 +240,25 @@ class OmniHandler(StreamHandler):
241
  return
242
  process_audio(frame, self.state)
243
  if self.state.pause_detected:
244
- self.chunk_queue.put(True)
245
 
246
  def reset(self):
247
  self.generator = None
 
248
  self.state = AppState()
249
  self.duration = 0
250
 
251
  def emit(self):
252
- if not self.generator:
253
- self.chunk_queue.get()
 
 
 
254
  self.state.responding = True
255
- self.generator = response(self.state)
256
- try:
257
- return next(self.generator)
258
- except StopIteration:
259
- self.reset()
260
 
261
 
262
  with gr.Blocks() as demo:
 
3
  # Configure the root logger to WARNING to suppress debug messages from other libraries
4
  logging.basicConfig(level=logging.WARNING)
5
 
6
+ # Create a file handler instead of console handler
7
+ file_handler = logging.FileHandler("gradio_webrtc.log")
8
+ file_handler.setLevel(logging.DEBUG)
9
 
10
+ # Create a formatter (you might want to add timestamp to file logs)
11
+ formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
12
+ file_handler.setFormatter(formatter)
13
 
14
  # Configure the logger for your specific library
15
  logger = logging.getLogger("gradio_webrtc")
16
  logger.setLevel(logging.DEBUG)
17
+ logger.addHandler(file_handler)
18
 
19
 
20
  import base64
 
24
  import time
25
  import traceback
26
  from dataclasses import dataclass
27
+ from threading import Event, Thread
 
28
 
29
  import gradio as gr
30
  import librosa
 
46
  IP = "0.0.0.0"
47
  PORT = 60808
48
 
49
+ thread = Thread(target=serve, daemon=True)
50
+ thread.start()
51
 
52
 
53
+ API_URL = "http://0.0.0.0:60808/chat"
54
 
55
  account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
56
  auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
 
230
  super().__init__(
231
  expected_layout="mono", output_sample_rate=OUT_RATE, output_frame_size=480
232
  )
233
+ self.event = Event()
234
  self.state = AppState()
235
  self.generator = None
236
  self.duration = 0
 
240
  return
241
  process_audio(frame, self.state)
242
  if self.state.pause_detected:
243
+ self.event.set()
244
 
245
  def reset(self):
246
  self.generator = None
247
+ self.event.clear()
248
  self.state = AppState()
249
  self.duration = 0
250
 
251
  def emit(self):
252
+ if not self.event.is_set():
253
+ return None
254
+ else:
255
+ if not self.generator:
256
+ self.generator = response(self.state)
257
  self.state.responding = True
258
+ try:
259
+ return next(self.generator)
260
+ except StopIteration:
261
+ self.reset()
 
262
 
263
 
264
  with gr.Blocks() as demo:
requirements.txt CHANGED
@@ -12,5 +12,5 @@ fastapi==0.112.4
12
  librosa==0.10.2.post1
13
  flask==3.0.3
14
  fire
15
- gradio_webrtc==0.0.5a1
16
  twilio
 
12
  librosa==0.10.2.post1
13
  flask==3.0.3
14
  fire
15
+ gradio_webrtc==0.0.6a3
16
  twilio