-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
Copy pathcreate_agent.py
291 lines (231 loc) · 10.1 KB
/
create_agent.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import concurrent.futures
from typing import Dict, Optional
import secrets
import string
import uuid
from dotenv import load_dotenv
from swarms import Agent
import replicate
from swarms.utils.str_to_dict import str_to_dict
load_dotenv()
def generate_key(prefix: str = "run") -> str:
"""
Generates an API key similar to OpenAI's format (sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX).
Args:
prefix (str): The prefix for the API key. Defaults to "sk".
Returns:
str: An API key string in format: prefix-<48 random characters>
"""
# Create random string of letters and numbers
alphabet = string.ascii_letters + string.digits
random_part = "".join(secrets.choice(alphabet) for _ in range(28))
return f"{prefix}-{random_part}"
def _generate_media(
prompt: str = None, modalities: list = None
) -> Dict[str, str]:
"""
Generate media content (images or videos) based on text prompts using AI models.
Args:
prompt (str): Text description of the content to be generated
modalities (list): List of media types to generate (e.g., ["image", "video"])
Returns:
Dict[str, str]: Dictionary containing file paths of generated media
"""
if not prompt or not modalities:
raise ValueError("Prompt and modalities must be provided")
input = {"prompt": prompt}
results = {}
def _generate_image(input: Dict) -> str:
"""Generate an image and return the file path."""
output = replicate.run(
"black-forest-labs/flux-dev", input=input
)
file_paths = []
for index, item in enumerate(output):
unique_id = str(uuid.uuid4())
artifact = item.read()
file_path = f"output_{unique_id}_{index}.webp"
with open(file_path, "wb") as file:
file.write(artifact)
file_paths.append(file_path)
return file_paths
def _generate_video(input: Dict) -> str:
"""Generate a video and return the file path."""
output = replicate.run("luma/ray", input=input)
unique_id = str(uuid.uuid4())
artifact = output.read()
file_path = f"output_{unique_id}.mp4"
with open(file_path, "wb") as file:
file.write(artifact)
return file_path
for modality in modalities:
if modality == "image":
results["images"] = _generate_image(input)
elif modality == "video":
results["video"] = _generate_video(input)
else:
raise ValueError(f"Unsupported modality: {modality}")
print(results)
return results
def generate_media(
modalities: list,
prompt: Optional[str] = None,
count: int = 1,
) -> Dict:
with concurrent.futures.ThreadPoolExecutor(
max_workers=count
) as executor:
# Create list of identical tasks to run concurrently
futures = [
executor.submit(
_generate_media,
prompt=prompt, # Fix: Pass as keyword arguments
modalities=modalities,
)
for _ in range(count)
]
# Wait for all tasks to complete and collect results
results = [
future.result()
for future in concurrent.futures.as_completed(futures)
]
return {"results": results}
tools = [
{
"type": "function",
"function": {
"name": "generate_media",
"description": "Generate different types of media content (image, video, or music) based on text prompts using AI models.",
"parameters": {
"type": "object",
"properties": {
"modality": {
"type": "array",
"items": {
"type": "string",
"enum": ["image", "video", "music"],
},
"description": "The type of media content to generate",
},
"prompt": {
"type": "string",
"description": "Text description of the content to be generated. Specialize it for the modality at hand. For example, if you are generating an image, the prompt should be a description of the image you want to see. If you are generating a video, the prompt should be a description of the video you want to see. If you are generating music, the prompt should be a description of the music you want to hear.",
},
"count": {
"type": "integer",
"description": "Number of outputs to generate (1-4)",
},
},
"required": [
"modality",
"prompt",
"count",
],
},
},
}
]
MEDIA_GENERATION_SYSTEM_PROMPT = """
You are an expert AI Media Generation Assistant, specialized in crafting precise and effective prompts for generating images, videos, and music. Your role is to help users create high-quality media content by understanding their requests and translating them into optimal prompts.
GENERAL GUIDELINES:
- Always analyze the user's request carefully to determine the appropriate modality (image, video, or music)
- Maintain a balanced level of detail in prompts - specific enough to capture the desired outcome but not overly verbose
- Consider the technical limitations and capabilities of AI generation systems
- When unclear, ask for clarification about specific details or preferences
MODALITY-SPECIFIC GUIDELINES:
1. IMAGE GENERATION:
- Structure prompts with primary subject first, followed by style, mood, and technical specifications
- Include relevant art styles when specified (e.g., "digital art", "oil painting", "watercolor", "photorealistic")
- Consider composition elements (foreground, background, lighting, perspective)
- Use specific adjectives for clarity (instead of "beautiful", specify "vibrant", "ethereal", "gritty", etc.)
Example image prompts:
- "A serene Japanese garden at sunset, with cherry blossoms falling, painted in traditional ukiyo-e style, soft pastel colors"
- "Cyberpunk cityscape at night, neon lights reflecting in rain puddles, hyper-realistic digital art style"
2. VIDEO GENERATION:
- Describe the sequence of events clearly
- Specify camera movements if relevant (pan, zoom, tracking shot)
- Include timing and transitions when necessary
- Focus on dynamic elements and motion
Example video prompts:
- "Timelapse of a flower blooming in a garden, close-up shot, soft natural lighting, 10-second duration"
- "Drone shot flying through autumn forest, camera slowly rising above the canopy, revealing mountains in the distance"
3. MUSIC GENERATION:
- Specify genre, tempo, and mood
- Mention key instruments or sounds
- Include emotional qualities and intensity
- Reference similar artists or styles if relevant
Example music prompts:
- "Calm ambient electronic music with soft synthesizer pads, gentle piano melodies, 80 BPM, suitable for meditation"
- "Upbeat jazz fusion track with prominent bass line, dynamic drums, and horn section, inspired by Weather Report"
COUNT HANDLING:
- When multiple outputs are requested (1-4), maintain consistency while introducing subtle variations
- For images: Vary composition or perspective while maintaining style
- For videos: Adjust camera angles or timing while keeping the core concept
- For music: Modify instrument arrangements or tempo while preserving the genre and mood
PROMPT OPTIMIZATION PROCESS:
1. Identify core requirements from user input
2. Determine appropriate modality
3. Add necessary style and technical specifications
4. Adjust detail level based on complexity
5. Consider count and create variations if needed
EXAMPLES OF HANDLING USER REQUESTS:
User: "I want a fantasy landscape"
Assistant response: {
"modality": "image",
"prompt": "Majestic fantasy landscape with floating islands, crystal waterfalls, and ancient magical ruins, ethereal lighting, digital art style with rich colors",
"count": 1
}
User: "Create 3 variations of a peaceful nature scene"
Assistant response: {
"modality": "image",
"prompt": "Tranquil forest clearing with morning mist, sunbeams filtering through ancient trees, photorealistic style with soft natural lighting",
"count": 1
}
IMPORTANT CONSIDERATIONS:
- Avoid harmful, unethical, or inappropriate content
- Respect copyright and intellectual property guidelines
- Maintain consistency with brand guidelines when specified
- Consider technical limitations of current AI generation systems
"""
# Initialize the agent with the new system prompt
agent = Agent(
agent_name="Media-Generation-Agent",
agent_description="AI Media Generation Assistant",
system_prompt=MEDIA_GENERATION_SYSTEM_PROMPT,
max_loops=1,
tools_list_dictionary=tools,
output_type="final",
)
def create_agent(task: str):
output = str_to_dict(agent.run(task))
print(output)
print(type(output))
prompt = output["prompt"]
count = output["count"]
modalities = output["modality"]
output = generate_media(
modalities=modalities,
prompt=prompt,
count=count,
)
run_id = generate_key()
total_cost = 0
for modality in modalities:
if modality == "image":
total_cost += 0.1
elif modality == "video":
total_cost += 1
result = {
"id": run_id,
"success": True,
"prompt": prompt,
"count": count,
"modality": modalities,
"total_cost": total_cost,
}
return result
if __name__ == "__main__":
task = "Create 3 super kawaii variations of a magical Chinese mountain garden scene in anime style! 🌸✨ Include adorable elements like: cute koi fish swimming in crystal ponds, fluffy clouds floating around misty peaks, tiny pagodas with twinkling lights, and playful pandas hiding in bamboo groves. Make it extra magical with sparkles and soft pastel colors! Create both a video and an image for each variation. Just 1."
output = create_agent(task)
print("✨ Yay! Here's your super cute creation! ✨")
print(output)