File size: 1,462 Bytes
7b786dd
e5f9d96
 
7b786dd
c3ca602
e5f9d96
7b786dd
 
c3ca602
e5f9d96
 
7b786dd
 
 
e5f9d96
7b786dd
 
 
 
 
 
 
e5f9d96
 
7b786dd
 
e5f9d96
 
c7ba53a
 
 
 
 
 
 
e5f9d96
 
 
 
 
 
 
 
 
 
 
29e4c2a
8eef934
 
 
be8583c
e5f9d96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import httpx  # This was missing
from fastrtc import ReplyOnPause, Stream, get_stt_model, get_tts_model
from openai import OpenAI

# Initialize Sambanova Client
sambanova_client = OpenAI(
    api_key=os.getenv("key"), base_url="https://api.deepinfra.com/v1"
)

# Load STT and TTS models
stt_model = get_stt_model()
tts_model = get_tts_model()

# Echo function to convert speech to text, then back to speech
def echo(audio):
    prompt = stt_model.stt(audio)
    response = sambanova_client.chat.completions.create(
        model="mistralai/Mistral-Small-24B-Instruct-2501",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=200,
    )
    reply = response.choices[0].message.content
    for audio_chunk in tts_model.stream_tts_sync(reply):
        yield audio_chunk

# Dummy implementation of get_cloudflare_turn_credentials
# You’ll need to replace this with real logic if it's not a stub
def get_cloudflare_turn_credentials(
    turn_key_id=None,
    turn_key_api_token=None,
    hf_token=None,
    ttl=600,
    client: httpx.AsyncClient | None = None,
):
    return {
        "iceServers": [
            {
                "urls": ["stun:stun.l.google.com:19302"]
            }
        ]
    }

# Create and start the stream
stream = Stream(
    handler=echo,  # You had handler=... which is invalid
    rtc_configuration=get_cloudflare_turn_credentials,
    modality="audio",
    mode="send-receive"
)

stream.fastphone()