-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI_audio_stream.py
More file actions
26 lines (23 loc) · 814 Bytes
/
AI_audio_stream.py
File metadata and controls
26 lines (23 loc) · 814 Bytes
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
import os
from fastrtc import (ReplyOnPause, Stream, get_stt_model, get_tts_model)
from openai import OpenAI
openrouter_client = OpenAI(api_key=os.getenv("OPENROUTER_API_KEY"), base_url="https://openrouter.ai/api/v1")
model="google/gemini-2.0-flash-thinking-exp"
stt_model = get_stt_model()
tts_model = get_tts_model()
def ai_response(audio):
prompt = stt_model.stt(audio)
response = openrouter_client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_tokens=200,
)
response_text = response.choices[0].message.content
for audio_chunk in tts_model.stream_tts_sync(response_text):
yield audio_chunk
stream = Stream(
handler=ReplyOnPause(ai_response),
modality="audio",
mode="send-receive"
)
stream.ui.launch()