Update agents/agents.py
Browse files- agents/agents.py +24 -30
agents/agents.py
CHANGED
@@ -11,12 +11,11 @@ load_dotenv()
|
|
11 |
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) if os.getenv("OPENAI_API_KEY") else None
|
12 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
13 |
|
14 |
-
|
15 |
class TopicAgent:
|
16 |
def generate_outline(self, topic, duration, difficulty):
|
17 |
if not openai_client:
|
18 |
return self._mock_outline(topic, duration, difficulty)
|
19 |
-
|
20 |
try:
|
21 |
response = openai_client.chat.completions.create(
|
22 |
model="gpt-4-turbo",
|
@@ -45,9 +44,9 @@ class TopicAgent:
|
|
45 |
response_format={"type": "json_object"}
|
46 |
)
|
47 |
return json.loads(response.choices[0].message.content)
|
48 |
-
except Exception:
|
49 |
return self._mock_outline(topic, duration, difficulty)
|
50 |
-
|
51 |
def _mock_outline(self, topic, duration, difficulty):
|
52 |
return {
|
53 |
"title": f"Mastering {topic} for Business Impact",
|
@@ -91,12 +90,11 @@ class TopicAgent:
|
|
91 |
]
|
92 |
}
|
93 |
|
94 |
-
|
95 |
class ContentAgent:
|
96 |
def generate_content(self, outline):
|
97 |
if not openai_client:
|
98 |
return self._mock_content(outline)
|
99 |
-
|
100 |
try:
|
101 |
response = openai_client.chat.completions.create(
|
102 |
model="gpt-4-turbo",
|
@@ -128,9 +126,9 @@ class ContentAgent:
|
|
128 |
response_format={"type": "json_object"}
|
129 |
)
|
130 |
return json.loads(response.choices[0].message.content)
|
131 |
-
except Exception:
|
132 |
return self._mock_content(outline)
|
133 |
-
|
134 |
def _mock_content(self, outline):
|
135 |
return {
|
136 |
"workshop_title": outline.get("title", "Premium AI Workshop"),
|
@@ -172,12 +170,11 @@ class ContentAgent:
|
|
172 |
]
|
173 |
}
|
174 |
|
175 |
-
|
176 |
class SlideAgent:
|
177 |
def generate_slides(self, content):
|
178 |
if not openai_client:
|
179 |
return self._professional_slides(content)
|
180 |
-
|
181 |
try:
|
182 |
response = openai_client.chat.completions.create(
|
183 |
model="gpt-4-turbo",
|
@@ -204,9 +201,9 @@ class SlideAgent:
|
|
204 |
max_tokens=2500
|
205 |
)
|
206 |
return response.choices[0].message.content
|
207 |
-
except Exception:
|
208 |
return self._professional_slides(content)
|
209 |
-
|
210 |
def _professional_slides(self, content):
|
211 |
return f"""---
|
212 |
marp: true
|
@@ -256,14 +253,13 @@ Calculate net ROI
|
|
256 |
|
257 |
Q&A
|
258 |
Let's discuss your specific challenges
|
259 |
-
|
260 |
-
|
261 |
|
262 |
class CodeAgent:
|
263 |
def generate_code(self, content):
|
264 |
if not openai_client:
|
265 |
return self._professional_code(content)
|
266 |
-
|
267 |
try:
|
268 |
response = openai_client.chat.completions.create(
|
269 |
model="gpt-4-turbo",
|
@@ -290,7 +286,7 @@ class CodeAgent:
|
|
290 |
max_tokens=2500
|
291 |
)
|
292 |
return response.choices[0].message.content
|
293 |
-
except Exception:
|
294 |
return self._professional_code(content)
|
295 |
|
296 |
def _professional_code(self, content):
|
@@ -332,12 +328,11 @@ def integrate_with_salesforce(prompt, salesforce_data):
|
|
332 |
return call_ai_api(enriched_prompt)
|
333 |
"""
|
334 |
|
335 |
-
|
336 |
class DesignAgent:
|
337 |
def generate_design(self, slide_content):
|
338 |
if not openai_client:
|
339 |
return None
|
340 |
-
|
341 |
try:
|
342 |
response = openai_client.images.generate(
|
343 |
model="dall-e-3",
|
@@ -350,23 +345,22 @@ class DesignAgent:
|
|
350 |
size="1024x1024"
|
351 |
)
|
352 |
return response.data[0].url
|
353 |
-
except Exception:
|
354 |
return None
|
355 |
|
356 |
-
|
357 |
class VoiceoverAgent:
|
358 |
def __init__(self):
|
359 |
self.api_key = ELEVENLABS_API_KEY
|
360 |
self.voice_id = "21m00Tcm4TlvDq8ikWAM" # Default voice ID
|
361 |
self.model = "eleven_monolingual_v1"
|
362 |
-
|
363 |
def generate_voiceover(self, text, voice_id=None):
|
364 |
if not self.api_key:
|
365 |
return None
|
366 |
-
|
367 |
try:
|
368 |
voice = voice_id if voice_id else self.voice_id
|
369 |
-
|
370 |
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice}"
|
371 |
headers = {
|
372 |
"Accept": "audio/mpeg",
|
@@ -384,24 +378,24 @@ class VoiceoverAgent:
|
|
384 |
}
|
385 |
}
|
386 |
response = requests.post(url, json=data, headers=headers)
|
387 |
-
|
388 |
if response.status_code == 200:
|
389 |
return response.content
|
390 |
return None
|
391 |
-
except Exception:
|
392 |
return None
|
393 |
-
|
394 |
def get_voices(self):
|
395 |
if not self.api_key:
|
396 |
return []
|
397 |
-
|
398 |
try:
|
399 |
url = "https://api.elevenlabs.io/v1/voices"
|
400 |
headers = {"xi-api-key": self.api_key}
|
401 |
response = requests.get(url, headers=headers)
|
402 |
-
|
403 |
if response.status_code == 200:
|
404 |
return response.json().get("voices", [])
|
405 |
return []
|
406 |
-
except Exception:
|
407 |
-
return []
|
|
|
11 |
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) if os.getenv("OPENAI_API_KEY") else None
|
12 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
13 |
|
|
|
14 |
class TopicAgent:
|
15 |
def generate_outline(self, topic, duration, difficulty):
|
16 |
if not openai_client:
|
17 |
return self._mock_outline(topic, duration, difficulty)
|
18 |
+
|
19 |
try:
|
20 |
response = openai_client.chat.completions.create(
|
21 |
model="gpt-4-turbo",
|
|
|
44 |
response_format={"type": "json_object"}
|
45 |
)
|
46 |
return json.loads(response.choices[0].message.content)
|
47 |
+
except Exception as e:
|
48 |
return self._mock_outline(topic, duration, difficulty)
|
49 |
+
|
50 |
def _mock_outline(self, topic, duration, difficulty):
|
51 |
return {
|
52 |
"title": f"Mastering {topic} for Business Impact",
|
|
|
90 |
]
|
91 |
}
|
92 |
|
|
|
93 |
class ContentAgent:
|
94 |
def generate_content(self, outline):
|
95 |
if not openai_client:
|
96 |
return self._mock_content(outline)
|
97 |
+
|
98 |
try:
|
99 |
response = openai_client.chat.completions.create(
|
100 |
model="gpt-4-turbo",
|
|
|
126 |
response_format={"type": "json_object"}
|
127 |
)
|
128 |
return json.loads(response.choices[0].message.content)
|
129 |
+
except Exception as e:
|
130 |
return self._mock_content(outline)
|
131 |
+
|
132 |
def _mock_content(self, outline):
|
133 |
return {
|
134 |
"workshop_title": outline.get("title", "Premium AI Workshop"),
|
|
|
170 |
]
|
171 |
}
|
172 |
|
|
|
173 |
class SlideAgent:
|
174 |
def generate_slides(self, content):
|
175 |
if not openai_client:
|
176 |
return self._professional_slides(content)
|
177 |
+
|
178 |
try:
|
179 |
response = openai_client.chat.completions.create(
|
180 |
model="gpt-4-turbo",
|
|
|
201 |
max_tokens=2500
|
202 |
)
|
203 |
return response.choices[0].message.content
|
204 |
+
except Exception as e:
|
205 |
return self._professional_slides(content)
|
206 |
+
|
207 |
def _professional_slides(self, content):
|
208 |
return f"""---
|
209 |
marp: true
|
|
|
253 |
|
254 |
Q&A
|
255 |
Let's discuss your specific challenges
|
256 |
+
"""
|
|
|
257 |
|
258 |
class CodeAgent:
|
259 |
def generate_code(self, content):
|
260 |
if not openai_client:
|
261 |
return self._professional_code(content)
|
262 |
+
|
263 |
try:
|
264 |
response = openai_client.chat.completions.create(
|
265 |
model="gpt-4-turbo",
|
|
|
286 |
max_tokens=2500
|
287 |
)
|
288 |
return response.choices[0].message.content
|
289 |
+
except Exception as e:
|
290 |
return self._professional_code(content)
|
291 |
|
292 |
def _professional_code(self, content):
|
|
|
328 |
return call_ai_api(enriched_prompt)
|
329 |
"""
|
330 |
|
|
|
331 |
class DesignAgent:
|
332 |
def generate_design(self, slide_content):
|
333 |
if not openai_client:
|
334 |
return None
|
335 |
+
|
336 |
try:
|
337 |
response = openai_client.images.generate(
|
338 |
model="dall-e-3",
|
|
|
345 |
size="1024x1024"
|
346 |
)
|
347 |
return response.data[0].url
|
348 |
+
except Exception as e:
|
349 |
return None
|
350 |
|
|
|
351 |
class VoiceoverAgent:
|
352 |
def __init__(self):
|
353 |
self.api_key = ELEVENLABS_API_KEY
|
354 |
self.voice_id = "21m00Tcm4TlvDq8ikWAM" # Default voice ID
|
355 |
self.model = "eleven_monolingual_v1"
|
356 |
+
|
357 |
def generate_voiceover(self, text, voice_id=None):
|
358 |
if not self.api_key:
|
359 |
return None
|
360 |
+
|
361 |
try:
|
362 |
voice = voice_id if voice_id else self.voice_id
|
363 |
+
|
364 |
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice}"
|
365 |
headers = {
|
366 |
"Accept": "audio/mpeg",
|
|
|
378 |
}
|
379 |
}
|
380 |
response = requests.post(url, json=data, headers=headers)
|
381 |
+
|
382 |
if response.status_code == 200:
|
383 |
return response.content
|
384 |
return None
|
385 |
+
except Exception as e:
|
386 |
return None
|
387 |
+
|
388 |
def get_voices(self):
|
389 |
if not self.api_key:
|
390 |
return []
|
391 |
+
|
392 |
try:
|
393 |
url = "https://api.elevenlabs.io/v1/voices"
|
394 |
headers = {"xi-api-key": self.api_key}
|
395 |
response = requests.get(url, headers=headers)
|
396 |
+
|
397 |
if response.status_code == 200:
|
398 |
return response.json().get("voices", [])
|
399 |
return []
|
400 |
+
except Exception as e:
|
401 |
+
return []
|