Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -36,13 +36,12 @@ class OpenAIHandler(AsyncStreamHandler):
|
|
| 36 |
input_sample_rate=SAMPLE_RATE,
|
| 37 |
)
|
| 38 |
self.connection = None
|
| 39 |
-
self.connected = asyncio.Event()
|
| 40 |
self.output_queue = asyncio.Queue()
|
| 41 |
|
| 42 |
def copy(self):
|
| 43 |
return OpenAIHandler()
|
| 44 |
|
| 45 |
-
async def
|
| 46 |
self,
|
| 47 |
):
|
| 48 |
"""Connect to realtime API. Run forever in separate thread to keep connection open."""
|
|
@@ -54,7 +53,6 @@ class OpenAIHandler(AsyncStreamHandler):
|
|
| 54 |
session={"turn_detection": {"type": "server_vad"}}
|
| 55 |
)
|
| 56 |
self.connection = conn
|
| 57 |
-
self.connected.set()
|
| 58 |
async for event in self.connection:
|
| 59 |
if event.type == "response.audio_transcript.done":
|
| 60 |
await self.output_queue.put(AdditionalOutputs(event))
|
|
@@ -70,9 +68,7 @@ class OpenAIHandler(AsyncStreamHandler):
|
|
| 70 |
|
| 71 |
async def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
| 72 |
if not self.connection:
|
| 73 |
-
|
| 74 |
-
asyncio.create_task(self._initialize_connection())
|
| 75 |
-
await self.connected.wait()
|
| 76 |
try:
|
| 77 |
_, array = frame
|
| 78 |
array = array.squeeze()
|
|
@@ -86,15 +82,12 @@ class OpenAIHandler(AsyncStreamHandler):
|
|
| 86 |
traceback.print_exc()
|
| 87 |
|
| 88 |
async def emit(self) -> tuple[int, np.ndarray] | AdditionalOutputs | None:
|
| 89 |
-
if not self.connection:
|
| 90 |
-
return None
|
| 91 |
return await self.output_queue.get()
|
| 92 |
|
| 93 |
def reset_state(self):
|
| 94 |
"""Reset connection state for new recording session"""
|
| 95 |
self.connection = None
|
| 96 |
self.args_set.clear()
|
| 97 |
-
self.connected.clear()
|
| 98 |
|
| 99 |
async def shutdown(self) -> None:
|
| 100 |
if self.connection:
|
|
|
|
| 36 |
input_sample_rate=SAMPLE_RATE,
|
| 37 |
)
|
| 38 |
self.connection = None
|
|
|
|
| 39 |
self.output_queue = asyncio.Queue()
|
| 40 |
|
| 41 |
def copy(self):
|
| 42 |
return OpenAIHandler()
|
| 43 |
|
| 44 |
+
async def start_up(
|
| 45 |
self,
|
| 46 |
):
|
| 47 |
"""Connect to realtime API. Run forever in separate thread to keep connection open."""
|
|
|
|
| 53 |
session={"turn_detection": {"type": "server_vad"}}
|
| 54 |
)
|
| 55 |
self.connection = conn
|
|
|
|
| 56 |
async for event in self.connection:
|
| 57 |
if event.type == "response.audio_transcript.done":
|
| 58 |
await self.output_queue.put(AdditionalOutputs(event))
|
|
|
|
| 68 |
|
| 69 |
async def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
| 70 |
if not self.connection:
|
| 71 |
+
return
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
_, array = frame
|
| 74 |
array = array.squeeze()
|
|
|
|
| 82 |
traceback.print_exc()
|
| 83 |
|
| 84 |
async def emit(self) -> tuple[int, np.ndarray] | AdditionalOutputs | None:
|
|
|
|
|
|
|
| 85 |
return await self.output_queue.get()
|
| 86 |
|
| 87 |
def reset_state(self):
|
| 88 |
"""Reset connection state for new recording session"""
|
| 89 |
self.connection = None
|
| 90 |
self.args_set.clear()
|
|
|
|
| 91 |
|
| 92 |
async def shutdown(self) -> None:
|
| 93 |
if self.connection:
|