|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
from utils import settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpenAITTS:
|
|
|
|
|
"""
|
|
|
|
|
A Text-to-Speech engine that uses an OpenAI-like TTS API endpoint to generate audio from text.
|
|
|
|
@ -12,15 +15,20 @@ class OpenAITTS:
|
|
|
|
|
api_url (str): The complete API endpoint URL, built from a base URL provided in the config.
|
|
|
|
|
available_voices (list): Static list of supported voices (according to current docs).
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
# Set maximum input size based on API limits (4096 characters per request)
|
|
|
|
|
self.max_chars = 4096
|
|
|
|
|
self.api_key = settings.config["settings"]["tts"].get("openai_api_key")
|
|
|
|
|
if not self.api_key:
|
|
|
|
|
raise ValueError("No OpenAI API key provided in settings! Please set 'openai_api_key' in your config.")
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"No OpenAI API key provided in settings! Please set 'openai_api_key' in your config."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Lese den Basis-URL aus der Konfiguration (z. B. "https://api.openai.com/v1" oder "https://api.openai.com/v1/")
|
|
|
|
|
base_url = settings.config["settings"]["tts"].get("openai_api_url", "https://api.openai.com/v1")
|
|
|
|
|
base_url = settings.config["settings"]["tts"].get(
|
|
|
|
|
"openai_api_url", "https://api.openai.com/v1"
|
|
|
|
|
)
|
|
|
|
|
# Entferne ggf. den abschließenden Slash
|
|
|
|
|
if base_url.endswith("/"):
|
|
|
|
|
base_url = base_url[:-1]
|
|
|
|
@ -69,12 +77,9 @@ class OpenAITTS:
|
|
|
|
|
"model": model,
|
|
|
|
|
"voice": voice,
|
|
|
|
|
"input": text,
|
|
|
|
|
"response_format": "mp3" # allowed formats: "mp3", "aac", "opus", "flac", "pcm" or "wav"
|
|
|
|
|
}
|
|
|
|
|
headers = {
|
|
|
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
"response_format": "mp3", # allowed formats: "mp3", "aac", "opus", "flac", "pcm" or "wav"
|
|
|
|
|
}
|
|
|
|
|
headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.api_url, headers=headers, json=payload)
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
|