Auto-update: Thu Aug 8 19:46:30 PDT 2024

This commit is contained in:
sanj 2024-08-08 19:46:30 -07:00
parent 59415fbf9a
commit 4f194eb628

View file

@ -257,42 +257,34 @@ async def determine_voice_id(voice_name: str) -> str:
async def elevenlabs_tts(model: str, input_text: str, voice: str, title: str = None, output_dir: str = None): async def elevenlabs_tts(model: str, input_text: str, voice: str, title: str = None, output_dir: str = None):
# Debug logging voice_id = await determine_voice_id(voice)
debug(f"API.EXTENSIONS: {API.EXTENSIONS}")
debug(f"API.EXTENSIONS.elevenlabs: {getattr(API.EXTENSIONS, 'elevenlabs', None)}")
debug(f"Tts config: {Tts}")
if getattr(API.EXTENSIONS, 'elevenlabs', False): url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
voice_id = await determine_voice_id(voice) payload = {
"text": input_text,
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}" "model_id": model
payload = { }
"text": input_text, headers = {"Content-Type": "application/json", "xi-api-key": Tts.elevenlabs.key}
"model_id": model debug(f"Using ElevenLabs API key: {Tts.elevenlabs.key}")
} try:
# Make sure this is the correct way to access the API key async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client: # 5 minutes timeout
headers = {"Content-Type": "application/json", "xi-api-key": Tts.elevenlabs.key} response = await client.post(url, json=payload, headers=headers)
try: output_dir = output_dir if output_dir else TTS_OUTPUT_DIR
async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client: title = title if title else dt_datetime.now().strftime("%Y%m%d%H%M%S")
response = await client.post(url, json=payload, headers=headers) filename = f"{sanitize_filename(title)}.mp3"
output_dir = output_dir if output_dir else TTS_OUTPUT_DIR file_path = Path(output_dir) / filename
title = title if title else dt_datetime.now().strftime("%Y%m%d%H%M%S") if response.status_code == 200:
filename = f"{sanitize_filename(title)}.mp3" with open(file_path, "wb") as audio_file:
file_path = Path(output_dir) / filename audio_file.write(response.content)
if response.status_code == 200: return file_path
with open(file_path, "wb") as audio_file: else:
audio_file.write(response.content) err(f"Error from ElevenLabs API. Status code: {response.status_code}")
return file_path err(f"Response content: {response.text}")
else: raise HTTPException(status_code=response.status_code, detail=f"Error from ElevenLabs API: {response.text}")
raise HTTPException(status_code=response.status_code, detail="Error from ElevenLabs API")
except Exception as e:
except Exception as e: err(f"Error from Elevenlabs API: {e}")
err(f"Error from Elevenlabs API: {e}") raise HTTPException(status_code=500, detail=f"Error from ElevenLabs API: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error from ElevenLabs API: {str(e)}")
else:
warn(f"elevenlabs_tts called but ElevenLabs module is not enabled in config.")
raise HTTPException(status_code=400, detail="ElevenLabs TTS is not enabled")