-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_engine.py
More file actions
40 lines (38 loc) · 1.39 KB
/
Copy pathvoice_engine.py
File metadata and controls
40 lines (38 loc) · 1.39 KB
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
import os
from elevenlabs.client import ElevenLabs
from elevenlabs import VoiceSettings
class VoiceEngine:
def __init__(self):
api_key = os.environ.get("ELEVENLABS_API_KEY")
if not api_key:
print("❌ ERROR: ELEVENLABS_API_KEY not found in .env")
self.client = None
else:
try:
self.client = ElevenLabs(api_key=api_key)
print("✅ ElevenLabs Connected")
except Exception as e:
print(f"❌ Connection Error: {e}")
self.client = None
def generate(self, text, voice_id, output_path):
if self.client is None: return False
try:
audio_stream = self.client.text_to_speech.convert(
text=text,
voice_id=voice_id,
model_id="eleven_turbo_v2_5",
output_format="mp3_44100_128",
voice_settings=VoiceSettings(
stability=0.4,
similarity_boost=0.8,
style=0.5,
use_speaker_boost=True
)
)
with open(output_path, "wb") as f:
for chunk in audio_stream:
if chunk: f.write(chunk)
return True
except Exception as e:
print(f"❌ ElevenLabs Generation Error: {e}")
return False