faizee07 commited on
Commit
ed628a1
·
verified ·
1 Parent(s): 02fe9c6

Create workflows/content_generator.py

Browse files
Files changed (1) hide show
  1. workflows/content_generator.py +43 -0
workflows/content_generator.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from prefect import task
2
+ from crewai import Crew
3
+ from crews.agents import content_writer_agent, image_prompt_agent, quality_assurance_agent
4
+ from crews.tasks import create_content_tasks
5
+ from config.schemas import InstagramPost
6
+ import json
7
+ import re
8
+
9
+ @task(retries=2)
10
+ def generate_instagram_content(rss_entry: dict) -> dict:
11
+ """Use CrewAI to generate Instagram post content"""
12
+
13
+ # Create tasks
14
+ tasks = create_content_tasks(
15
+ rss_content=rss_entry["content"],
16
+ rss_title=rss_entry["title"]
17
+ )
18
+
19
+ # Create crew
20
+ crew = Crew(
21
+ agents=[content_writer_agent, image_prompt_agent, quality_assurance_agent],
22
+ tasks=tasks,
23
+ verbose=True
24
+ )
25
+
26
+ # Execute crew
27
+ result = crew.kickoff()
28
+
29
+ # Parse result (CrewAI returns string)
30
+ result_str = str(result)
31
+
32
+ # Extract JSON from result
33
+ json_match = re.search(r'\{[\s\S]*\}', result_str)
34
+ if json_match:
35
+ json_str = json_match.group()
36
+ post_data = json.loads(json_str)
37
+ else:
38
+ raise ValueError("No valid JSON found in CrewAI output")
39
+
40
+ # Validate against schema
41
+ validated_post = InstagramPost(**post_data)
42
+
43
+ return validated_post.model_dump()