Update web/core/views_tts.py

This commit is contained in:
Joshua Laymon 2025-08-21 01:38:17 +00:00
parent 02153083b6
commit d8b512aaa6

View File

@ -33,19 +33,17 @@ def api_tts_for_entry(request, entry_id):
if not combined:
return HttpResponseBadRequest("No text available for this entry.")
# ---- Call OpenAI TTS (MP3) ----
# ---- Call OpenAI TTS (MP3) ----
try:
# gpt-4o-mini-tts → mp3 bytes
# voices: "alloy", "verse", "aria", etc.
speech = client.audio.speech.create(
# Use the streaming helper for broad SDK compatibility.
# It yields MP3 audio bytes by default.
with client.audio.speech.with_streaming_response.create(
model="gpt-4o-mini-tts",
voice="alloy",
voice="alloy", # or another supported voice
input=combined,
format="mp3",
)
audio_bytes = speech.read() # returns raw bytes
) as resp:
audio_bytes = resp.read() # raw MP3 bytes
except Exception as e:
# Return a plain text 500 so your client can preview it
return HttpResponse(f"OpenAI TTS failed: {e}", status=500, content_type="text/plain")
# ---- Serve as audio/mpeg ----