tiny-code-only-tts / deepgram_tts.py
abersbail's picture
Upload folder using huggingface_hub
66be83b verified
Raw
History Blame Contribute Delete
1.66 kB
import os
import requests
import uuid
DEEPGRAM_API_KEY = os.environ.get("DEEPGRAM_API_KEY", "ad59620bea7b23c52d31d273902839e950491bf2")
DEEPGRAM_TTS_URL = "https://api.deepgram.com/v1/speak"
AVAILABLE_VOICES = {
"Thalia (En-US - Conversational / Medical)": "aura-2-thalia-en",
"Luna (En-US - Warm & Empathetic)": "aura-2-luna-en",
"Stella (En-US - Professional & Crisp)": "aura-2-stella-en",
"Zeus (En-US - Authoritative Male)": "aura-2-zeus-en",
"Orion (En-US - Friendly Male)": "aura-2-orion-en"
}
def generate_voice_audio(text: str, voice_model: str = "aura-2-thalia-en") -> str:
"""
Calls Deepgram Aura-2 Text-to-Speech API to synthesize natural audio.
Returns filepath to generated .mp3 file.
"""
if not text or not text.strip():
return None
headers = {
"Authorization": f"Token {DEEPGRAM_API_KEY}",
"Content-Type": "text/plain"
}
url = f"{DEEPGRAM_TTS_URL}?model={voice_model}"
try:
response = requests.post(url, headers=headers, data=text.encode("utf-8"), timeout=15)
if response.status_code == 200 and len(response.content) > 0:
os.makedirs("audio_output", exist_ok=True)
filename = f"audio_output/call_resp_{uuid.uuid4().hex[:8]}.mp3"
with open(filename, "wb") as f:
f.write(response.content)
return filename
else:
print(f"[DeepgramTTS] API returned status {response.status_code}: {response.text}")
return None
except Exception as e:
print(f"[DeepgramTTS] Exception calling Deepgram API: {e}")
return None