Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ from fastrtc import (
|
|
13 |
AdditionalOutputs,
|
14 |
AsyncStreamHandler,
|
15 |
Stream,
|
|
|
16 |
get_twilio_turn_credentials,
|
17 |
)
|
18 |
from gradio.utils import get_space
|
@@ -46,25 +47,31 @@ class OpenAIHandler(AsyncStreamHandler):
|
|
46 |
):
|
47 |
"""Connect to realtime API. Run forever in separate thread to keep connection open."""
|
48 |
self.client = openai.AsyncOpenAI()
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
session
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
(
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
async def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
70 |
if not self.connection:
|
@@ -80,6 +87,7 @@ class OpenAIHandler(AsyncStreamHandler):
|
|
80 |
import traceback
|
81 |
|
82 |
traceback.print_exc()
|
|
|
83 |
|
84 |
async def emit(self) -> tuple[int, np.ndarray] | AdditionalOutputs | None:
|
85 |
return await self.output_queue.get()
|
@@ -142,7 +150,7 @@ if __name__ == "__main__":
|
|
142 |
import os
|
143 |
|
144 |
if (mode := os.getenv("MODE")) == "UI":
|
145 |
-
stream.ui.launch(server_port=7860
|
146 |
elif mode == "PHONE":
|
147 |
stream.fastphone(host="0.0.0.0", port=7860)
|
148 |
else:
|
|
|
13 |
AdditionalOutputs,
|
14 |
AsyncStreamHandler,
|
15 |
Stream,
|
16 |
+
WebRTCError,
|
17 |
get_twilio_turn_credentials,
|
18 |
)
|
19 |
from gradio.utils import get_space
|
|
|
47 |
):
|
48 |
"""Connect to realtime API. Run forever in separate thread to keep connection open."""
|
49 |
self.client = openai.AsyncOpenAI()
|
50 |
+
try:
|
51 |
+
async with self.client.beta.realtime.connect(
|
52 |
+
model="gpt-4o-mini-realtime-preview-2024-12-17"
|
53 |
+
) as conn:
|
54 |
+
await conn.session.update(
|
55 |
+
session={"turn_detection": {"type": "server_vad"}}
|
56 |
+
)
|
57 |
+
self.connection = conn
|
58 |
+
async for event in self.connection:
|
59 |
+
if event.type == "response.audio_transcript.done":
|
60 |
+
await self.output_queue.put(AdditionalOutputs(event))
|
61 |
+
if event.type == "response.audio.delta":
|
62 |
+
await self.output_queue.put(
|
63 |
+
(
|
64 |
+
self.output_sample_rate,
|
65 |
+
np.frombuffer(
|
66 |
+
base64.b64decode(event.delta), dtype=np.int16
|
67 |
+
).reshape(1, -1),
|
68 |
+
),
|
69 |
+
)
|
70 |
+
except Exception:
|
71 |
+
import traceback
|
72 |
+
|
73 |
+
traceback.print_exc()
|
74 |
+
raise WebRTCError(str(traceback.format_exc()))
|
75 |
|
76 |
async def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
77 |
if not self.connection:
|
|
|
87 |
import traceback
|
88 |
|
89 |
traceback.print_exc()
|
90 |
+
raise WebRTCError(str(traceback.format_exc()))
|
91 |
|
92 |
async def emit(self) -> tuple[int, np.ndarray] | AdditionalOutputs | None:
|
93 |
return await self.output_queue.get()
|
|
|
150 |
import os
|
151 |
|
152 |
if (mode := os.getenv("MODE")) == "UI":
|
153 |
+
stream.ui.launch(server_port=7860)
|
154 |
elif mode == "PHONE":
|
155 |
stream.fastphone(host="0.0.0.0", port=7860)
|
156 |
else:
|