Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
-
import httpx
|
3 |
-
from fastrtc import ReplyOnPause, Stream, get_stt_model, get_tts_model
|
4 |
from openai import OpenAI
|
5 |
|
6 |
# Initialize Sambanova Client
|
@@ -12,20 +12,23 @@ sambanova_client = OpenAI(
|
|
12 |
stt_model = get_stt_model()
|
13 |
tts_model = get_tts_model()
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
model="mistralai/Mistral-Small-24B-Instruct-2501",
|
20 |
-
messages=[{"role": "user", "content": prompt}],
|
21 |
-
max_tokens=200,
|
22 |
-
)
|
23 |
-
reply = response.choices[0].message.content
|
24 |
-
for audio_chunk in tts_model.stream_tts_sync(reply):
|
25 |
-
yield audio_chunk
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def get_cloudflare_turn_credentials(
|
30 |
turn_key_id=None,
|
31 |
turn_key_api_token=None,
|
@@ -41,9 +44,9 @@ def get_cloudflare_turn_credentials(
|
|
41 |
]
|
42 |
}
|
43 |
|
44 |
-
#
|
45 |
stream = Stream(
|
46 |
-
handler=
|
47 |
rtc_configuration=get_cloudflare_turn_credentials,
|
48 |
modality="audio",
|
49 |
mode="send-receive"
|
|
|
1 |
import os
|
2 |
+
import httpx
|
3 |
+
from fastrtc import ReplyOnPause, Stream, get_stt_model, get_tts_model, StreamHandlerBase
|
4 |
from openai import OpenAI
|
5 |
|
6 |
# Initialize Sambanova Client
|
|
|
12 |
stt_model = get_stt_model()
|
13 |
tts_model = get_tts_model()
|
14 |
|
15 |
+
# Create a proper handler subclass
|
16 |
+
class EchoHandler(StreamHandlerBase):
|
17 |
+
def __init__(self):
|
18 |
+
super().__init__()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def on_audio(self, audio):
|
21 |
+
prompt = stt_model.stt(audio)
|
22 |
+
response = sambanova_client.chat.completions.create(
|
23 |
+
model="mistralai/Mistral-Small-24B-Instruct-2501",
|
24 |
+
messages=[{"role": "user", "content": prompt}],
|
25 |
+
max_tokens=200,
|
26 |
+
)
|
27 |
+
reply = response.choices[0].message.content
|
28 |
+
for audio_chunk in tts_model.stream_tts_sync(reply):
|
29 |
+
yield audio_chunk
|
30 |
+
|
31 |
+
# Dummy TURN config
|
32 |
def get_cloudflare_turn_credentials(
|
33 |
turn_key_id=None,
|
34 |
turn_key_api_token=None,
|
|
|
44 |
]
|
45 |
}
|
46 |
|
47 |
+
# Launch stream with correct handler
|
48 |
stream = Stream(
|
49 |
+
handler=EchoHandler(),
|
50 |
rtc_configuration=get_cloudflare_turn_credentials,
|
51 |
modality="audio",
|
52 |
mode="send-receive"
|