File size: 1,663 Bytes
66be83b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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
|