Spaces:
Sleeping

arkanum / src /voice /tts.py
benny4982's picture
Claude Opus 4.6
Add YouTube transcripts to RAG, walkthrough voice guide, and intro story
60a07be
Raw History Blame
988 Bytes
"""
Text-to-Speech via OpenAI TTS API
==================================
Wandelt Text in MP3-Audio um.
Verwendung:
from src.voice.tts import synthesize
audio_bytes = synthesize("Willkommen bei S.T.A.L.K.E.R.")
"""
import openai
from dotenv import load_dotenv
load_dotenv()
MODEL = "tts-1"
VOICE = "onyx" # Tiefe, ruhige Stimme – passt zum Erklärer-Stil
_client = None
def _get_client() -> openai.OpenAI:
global _client
if _client is None:
_client = openai.OpenAI()
return _client
def synthesize(text: str, voice: str = VOICE) -> bytes:
"""
Wandelt Text in MP3-Audio um.
Args:
text: Der zu sprechende Text
voice: OpenAI TTS Stimme (alloy, echo, fable, onyx, nova, shimmer)
Returns:
MP3-Audio als Bytes
"""
client = _get_client()
response = client.audio.speech.create(
model=MODEL,
voice=voice,
input=text,
response_format="mp3",
)
return response.content