diff --git a/sijapi/routers/tts.py b/sijapi/routers/tts.py index f6d2457..a285ff8 100644 --- a/sijapi/routers/tts.py +++ b/sijapi/routers/tts.py @@ -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): - # Debug logging - debug(f"API.EXTENSIONS: {API.EXTENSIONS}") - debug(f"API.EXTENSIONS.elevenlabs: {getattr(API.EXTENSIONS, 'elevenlabs', None)}") - debug(f"Tts config: {Tts}") + voice_id = await determine_voice_id(voice) - if getattr(API.EXTENSIONS, 'elevenlabs', False): - voice_id = await determine_voice_id(voice) - - url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}" - payload = { - "text": input_text, - "model_id": model - } - # Make sure this is the correct way to access the API key - headers = {"Content-Type": "application/json", "xi-api-key": Tts.elevenlabs.key} - try: - async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client: - response = await client.post(url, json=payload, headers=headers) - output_dir = output_dir if output_dir else TTS_OUTPUT_DIR - title = title if title else dt_datetime.now().strftime("%Y%m%d%H%M%S") - filename = f"{sanitize_filename(title)}.mp3" - file_path = Path(output_dir) / filename - if response.status_code == 200: - with open(file_path, "wb") as audio_file: - audio_file.write(response.content) - return file_path - else: - raise HTTPException(status_code=response.status_code, detail="Error from ElevenLabs API") - - except Exception as e: - err(f"Error from Elevenlabs API: {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") + url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}" + payload = { + "text": input_text, + "model_id": model + } + headers = {"Content-Type": "application/json", "xi-api-key": Tts.elevenlabs.key} + debug(f"Using ElevenLabs API key: {Tts.elevenlabs.key}") + try: + async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client: # 5 minutes timeout + response = await client.post(url, json=payload, headers=headers) + output_dir = output_dir if output_dir else TTS_OUTPUT_DIR + title = title if title else dt_datetime.now().strftime("%Y%m%d%H%M%S") + filename = f"{sanitize_filename(title)}.mp3" + file_path = Path(output_dir) / filename + if response.status_code == 200: + with open(file_path, "wb") as audio_file: + audio_file.write(response.content) + return file_path + else: + err(f"Error from ElevenLabs API. Status code: {response.status_code}") + err(f"Response content: {response.text}") + raise HTTPException(status_code=response.status_code, detail=f"Error from ElevenLabs API: {response.text}") + + except Exception as e: + err(f"Error from Elevenlabs API: {e}") + raise HTTPException(status_code=500, detail=f"Error from ElevenLabs API: {str(e)}")