|
import os |
|
import httpx |
|
from fastrtc import ReplyOnPause, Stream, get_stt_model, get_tts_model |
|
from openai import OpenAI |
|
|
|
|
|
sambanova_client = OpenAI( |
|
api_key=os.getenv("key"), base_url="https://api.deepinfra.com/v1" |
|
) |
|
|
|
|
|
stt_model = get_stt_model() |
|
tts_model = get_tts_model() |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
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"] |
|
} |
|
] |
|
} |
|
|
|
|
|
stream = Stream( |
|
handler=echo, |
|
rtc_configuration=get_cloudflare_turn_credentials, |
|
modality="audio", |
|
mode="send-receive" |
|
) |
|
|
|
stream.fastphone() |
|
|