Skip to content

Commit 6a0cc6f

Browse files
Brandon LeiBrandon Lei
Brandon Lei
authored and
Brandon Lei
committed
added speech endpoint
1 parent 2a9305a commit 6a0cc6f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

main.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,38 @@ class ChatResponse(BaseModel):
224224
audio_path: str
225225

226226

227+
class TextInput(BaseModel):
228+
text: str
229+
230+
231+
@app.post("/speak")
232+
@handle_errors
233+
async def speak_endpoint(input_data: TextInput):
234+
"""Convert text to speech and return audio file"""
235+
assistant = VoiceAssistant()
236+
try:
237+
audio_path = await assistant.synthesize_speech(input_data.text)
238+
239+
with open(audio_path, 'rb') as f:
240+
audio_content = f.read()
241+
242+
assistant.cleanup()
243+
244+
temp_response_path = tempfile.mktemp(suffix='.wav')
245+
with open(temp_response_path, 'wb') as f:
246+
f.write(audio_content)
247+
248+
return FileResponse(
249+
path=temp_response_path,
250+
media_type="audio/wav",
251+
filename="speech.wav"
252+
)
253+
except Exception as e:
254+
if assistant:
255+
assistant.cleanup()
256+
raise VoiceAssistantError(f"Speech synthesis failed: {str(e)}")
257+
258+
227259
@app.get("/health")
228260
async def root():
229261
"""Health check endpoint"""

0 commit comments

Comments
 (0)