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: if not combined:
return HttpResponseBadRequest("No text available for this entry.") return HttpResponseBadRequest("No text available for this entry.")
# ---- Call OpenAI TTS (MP3) ---- # ---- Call OpenAI TTS (MP3) ----
try: try:
# gpt-4o-mini-tts → mp3 bytes # Use the streaming helper for broad SDK compatibility.
# voices: "alloy", "verse", "aria", etc. # It yields MP3 audio bytes by default.
speech = client.audio.speech.create( with client.audio.speech.with_streaming_response.create(
model="gpt-4o-mini-tts", model="gpt-4o-mini-tts",
voice="alloy", voice="alloy", # or another supported voice
input=combined, input=combined,
format="mp3", ) as resp:
) audio_bytes = resp.read() # raw MP3 bytes
audio_bytes = speech.read() # returns raw bytes
except Exception as e: 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") return HttpResponse(f"OpenAI TTS failed: {e}", status=500, content_type="text/plain")
# ---- Serve as audio/mpeg ---- # ---- Serve as audio/mpeg ----