Spaces:
Sleeping
Sleeping
Download src/voice/tts.py from FC-Bit/arkanum: direct link, hf CLI and curl.
- Browser
- Download file 988 Bytes
-
https://huggingface.co/spaces/FC-Bit/arkanum/resolve/main/src/voice/tts.py
- Command line
-
hf download hf://spaces/FC-Bit/arkanum/src/voice/tts.py
-
curl -L -o tts.py https://huggingface.co/spaces/FC-Bit/arkanum/resolve/main/src/voice/tts.py
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 | |