SalexAI commited on
Commit
e5956ee
·
verified ·
1 Parent(s): d0febc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -17
app.py CHANGED
@@ -1,6 +1,6 @@
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
@@ -12,20 +12,23 @@ sambanova_client = OpenAI(
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(
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
- # 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,
@@ -41,9 +44,9 @@ def get_cloudflare_turn_credentials(
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"
 
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"