Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,18 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
from fastrtc import
|
4 |
from openai import OpenAI
|
5 |
|
|
|
6 |
sambanova_client = OpenAI(
|
7 |
api_key=os.getenv("key"), base_url="https://api.deepinfra.com/v1"
|
8 |
)
|
|
|
|
|
9 |
stt_model = get_stt_model()
|
10 |
tts_model = get_tts_model()
|
11 |
|
|
|
12 |
def echo(audio):
|
13 |
prompt = stt_model.stt(audio)
|
14 |
response = sambanova_client.chat.completions.create(
|
@@ -16,11 +20,12 @@ def echo(audio):
|
|
16 |
messages=[{"role": "user", "content": prompt}],
|
17 |
max_tokens=200,
|
18 |
)
|
19 |
-
|
20 |
-
for audio_chunk in tts_model.stream_tts_sync(
|
21 |
yield audio_chunk
|
22 |
|
23 |
-
|
|
|
24 |
def get_cloudflare_turn_credentials(
|
25 |
turn_key_id=None,
|
26 |
turn_key_api_token=None,
|
@@ -28,13 +33,20 @@ def get_cloudflare_turn_credentials(
|
|
28 |
ttl=600,
|
29 |
client: httpx.AsyncClient | None = None,
|
30 |
):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
rtc_configuration=get_cloudflare_turn_credentials,
|
36 |
modality="audio",
|
37 |
mode="send-receive"
|
38 |
)
|
39 |
|
40 |
-
stream.fastphone()
|
|
|
1 |
import os
|
2 |
+
import httpx # This was missing
|
3 |
+
from fastrtc import ReplyOnPause, Stream, get_stt_model, get_tts_model
|
4 |
from openai import OpenAI
|
5 |
|
6 |
+
# Initialize Sambanova Client
|
7 |
sambanova_client = OpenAI(
|
8 |
api_key=os.getenv("key"), base_url="https://api.deepinfra.com/v1"
|
9 |
)
|
10 |
+
|
11 |
+
# Load STT and TTS models
|
12 |
stt_model = get_stt_model()
|
13 |
tts_model = get_tts_model()
|
14 |
|
15 |
+
# Echo function to convert speech to text, then back to speech
|
16 |
def echo(audio):
|
17 |
prompt = stt_model.stt(audio)
|
18 |
response = sambanova_client.chat.completions.create(
|
|
|
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 |
+
# Dummy implementation of get_cloudflare_turn_credentials
|
28 |
+
# You’ll need to replace this with real logic if it's not a stub
|
29 |
def get_cloudflare_turn_credentials(
|
30 |
turn_key_id=None,
|
31 |
turn_key_api_token=None,
|
|
|
33 |
ttl=600,
|
34 |
client: httpx.AsyncClient | None = None,
|
35 |
):
|
36 |
+
return {
|
37 |
+
"iceServers": [
|
38 |
+
{
|
39 |
+
"urls": ["stun:stun.l.google.com:19302"]
|
40 |
+
}
|
41 |
+
]
|
42 |
+
}
|
43 |
+
|
44 |
+
# Create and start the stream
|
45 |
+
stream = Stream(
|
46 |
+
handler=echo, # You had handler=... which is invalid
|
47 |
rtc_configuration=get_cloudflare_turn_credentials,
|
48 |
modality="audio",
|
49 |
mode="send-receive"
|
50 |
)
|
51 |
|
52 |
+
stream.fastphone()
|