Update agents/voiceover_agent.py
Browse files- agents/voiceover_agent.py +32 -10
agents/voiceover_agent.py
CHANGED
@@ -1,17 +1,39 @@
|
|
|
|
|
|
|
|
1 |
class VoiceoverAgent:
|
2 |
def __init__(self):
|
3 |
self.api_key = os.getenv("ELEVENLABS_API_KEY")
|
4 |
-
|
|
|
|
|
5 |
def generate_voiceover(self, text):
|
6 |
if not self.api_key:
|
|
|
7 |
return None
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
try:
|
10 |
-
response = requests.post(
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
except:
|
17 |
-
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
|
4 |
class VoiceoverAgent:
|
5 |
def __init__(self):
|
6 |
self.api_key = os.getenv("ELEVENLABS_API_KEY")
|
7 |
+
self.voice_id = "21m00Tcm4TlvDq8ikWAM" # Rachel (default female voice)
|
8 |
+
self.model = "eleven_monolingual_v1"
|
9 |
+
|
10 |
def generate_voiceover(self, text):
|
11 |
if not self.api_key:
|
12 |
+
print("Error: ELEVENLABS_API_KEY not found in environment variables.")
|
13 |
return None
|
14 |
+
|
15 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{self.voice_id}"
|
16 |
+
headers = {
|
17 |
+
"xi-api-key": self.api_key,
|
18 |
+
"Accept": "audio/mpeg",
|
19 |
+
"Content-Type": "application/json"
|
20 |
+
}
|
21 |
+
payload = {
|
22 |
+
"text": text,
|
23 |
+
"model_id": self.model,
|
24 |
+
"voice_settings": {
|
25 |
+
"stability": 0.7,
|
26 |
+
"similarity_boost": 0.8
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
try:
|
31 |
+
response = requests.post(url, headers=headers, json=payload)
|
32 |
+
if response.status_code == 200:
|
33 |
+
return response.content # audio bytes
|
34 |
+
else:
|
35 |
+
print(f"[Voiceover Error] HTTP {response.status_code}: {response.text}")
|
36 |
+
return None
|
37 |
+
except requests.exceptions.RequestException as e:
|
38 |
+
print(f"[Voiceover Exception] {e}")
|
39 |
+
return None
|