forked from MervinPraison/PraisonAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_agent.py
More file actions
377 lines (339 loc) Β· 14.8 KB
/
image_agent.py
File metadata and controls
377 lines (339 loc) Β· 14.8 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
ImageAgent - A specialized agent class for generating images using AI models.
This class extends the base Agent class to provide specific functionality for image generation,
including support for different image models, sizes, and quality settings.
"""
from typing import Optional, Any, Dict, Union, List
from ..agent.agent import Agent
from pydantic import BaseModel, Field
import logging
from praisonaiagents._logging import get_logger
import warnings
# Filter out Pydantic warning about fields
warnings.filterwarnings("ignore", "Valid config keys have changed in V2", UserWarning)
class ImageGenerationConfig(BaseModel):
"""Configuration for image generation settings."""
style: str = Field(default="natural", description="Style of the generated image")
response_format: str = Field(default="url", description="Format of the response (url or b64_json)")
timeout: int = Field(default=600, description="Timeout in seconds for the API call")
api_base: Optional[str] = Field(default=None, description="Optional API base URL")
api_key: Optional[str] = Field(default=None, description="Optional API key")
api_version: Optional[str] = Field(default=None, description="Optional API version (required for Azure dall-e-3)")
class ImageAgent(Agent):
"""
A specialized agent for generating images using AI models.
This agent extends the base Agent class with specific functionality for image generation,
including support for different models, sizes, and quality settings.
"""
def __init__(
self,
name: Optional[str] = None,
role: Optional[str] = None,
goal: Optional[str] = None,
backstory: Optional[str] = None,
instructions: Optional[str] = None,
llm: Optional[Union[str, Any]] = None,
style: str = "natural",
response_format: str = "url",
timeout: int = 600,
api_base: Optional[str] = None,
api_key: Optional[str] = None,
api_version: Optional[str] = None,
verbose: Union[bool, int] = True,
**kwargs
):
"""Initialize ImageAgent with parameters."""
# Set default role and goal if not provided
role = role or "Image Generation Assistant"
goal = goal or "Generate high-quality images based on text descriptions"
backstory = backstory or "I am an AI assistant specialized in generating images from textual descriptions"
# Initialize the base agent
super().__init__(
name=name,
role=role,
goal=goal,
backstory=backstory,
instructions=instructions,
llm=llm,
**kwargs
)
# Store image generation configuration
self.image_config = ImageGenerationConfig(
style=style,
response_format=response_format,
timeout=timeout,
api_base=api_base,
api_key=api_key,
api_version=api_version
)
# Lazy load litellm
self._litellm = None
# Configure logging based on verbose level
self._configure_logging(verbose)
def _configure_logging(self, verbose: Union[bool, int]) -> None:
"""Configure logging levels based on verbose setting."""
# Only suppress logs if not in debug mode
if not isinstance(verbose, bool) and verbose >= 10:
# Enable detailed debug logging
get_logger("asyncio").setLevel(logging.DEBUG)
get_logger("selector_events").setLevel(logging.DEBUG)
get_logger("litellm.utils").setLevel(logging.DEBUG)
get_logger("litellm.main").setLevel(logging.DEBUG)
if hasattr(self, 'litellm'):
self.litellm.suppress_debug_messages = False
self.litellm.set_verbose = True
# Don't filter warnings in debug mode
warnings.resetwarnings()
else:
# Suppress debug logging for normal operation
get_logger("asyncio").setLevel(logging.WARNING)
get_logger("selector_events").setLevel(logging.WARNING)
get_logger("litellm.utils").setLevel(logging.WARNING)
get_logger("litellm.main").setLevel(logging.WARNING)
get_logger("httpx").setLevel(logging.WARNING)
get_logger("httpcore").setLevel(logging.WARNING)
if hasattr(self, 'litellm'):
self.litellm.suppress_debug_messages = True
self.litellm._logging._disable_debugging()
# Suppress all warnings including Pydantic's
warnings.filterwarnings("ignore", category=RuntimeWarning)
warnings.filterwarnings("ignore", category=UserWarning)
warnings.filterwarnings("ignore", category=DeprecationWarning)
@property
def litellm(self):
"""Lazy load litellm module when needed."""
if self._litellm is None:
try:
import litellm
from litellm import image_generation
# Configure litellm to disable success handler logs
litellm.success_callback = []
litellm._logging._disable_debugging()
self._litellm = image_generation
# Configure logging after litellm is loaded
self._configure_logging(self.verbose)
except ImportError:
raise ImportError(
"litellm is required for image generation. "
"Please install it with: pip install litellm"
)
return self._litellm
def generate_image(self, prompt: str, **kwargs) -> Dict[str, Any]:
"""Generate an image based on the provided prompt."""
# Merge default config with any provided kwargs
config = self.image_config.dict(exclude_none=True)
config.update(kwargs)
# Get the model name robustly from the parent Agent's property
model_info = self.llm_model
model_name = model_info.model if hasattr(model_info, 'model') else str(model_info)
# Use the model name in config
config['model'] = model_name
# Filter parameters based on the provider to avoid unsupported parameter errors
custom_llm_provider = None
try:
import litellm
_, custom_llm_provider, _, _ = litellm.get_llm_provider(model=model_name)
except (ImportError, AttributeError, ValueError, TypeError, Exception) as e:
# Log the specific error for debugging but continue with string-based fallback
# Include generic Exception to catch provider-specific errors like BadRequestError
logging.debug(f"Provider detection failed for model '{model_name}': {e}")
if custom_llm_provider == "vertex_ai":
# Vertex AI only supports 'n' and 'size' parameters for image generation
supported_params = ['n', 'size', 'model']
config = {k: v for k, v in config.items() if k in supported_params}
elif custom_llm_provider == "gemini" or (custom_llm_provider is None and 'gemini' in model_name.lower()):
# Gemini provider doesn't support response_format parameter
# Apply this filter if provider is explicitly 'gemini' or as fallback for gemini models
config.pop('response_format', None)
from rich.progress import Progress, SpinnerColumn, TextColumn
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True
) as progress:
try:
# Add a task for image generation
task = progress.add_task(f"[cyan]Generating image with {model_name}...", total=None)
# Use litellm's image generation with parameter dropping enabled as safety net
response = self.litellm(
prompt=prompt,
drop_params=True,
**config
)
# Mark task as complete
progress.update(task, completed=True)
return response
except Exception as e:
error_msg = f"Error generating image: {str(e)}"
if self.verbose:
self.console.print(f"[red]{error_msg}[/red]")
logging.error(error_msg)
raise
async def agenerate_image(self, prompt: str, **kwargs) -> Dict[str, Any]:
"""Async wrapper for generate_image."""
return self.generate_image(prompt, **kwargs)
# Aliases for consistency with other agents
def generate(self, prompt: str, **kwargs) -> Dict[str, Any]:
"""Alias for generate_image() - for consistency with VideoAgent/AudioAgent."""
return self.generate_image(prompt, **kwargs)
async def agenerate(self, prompt: str, **kwargs) -> Dict[str, Any]:
"""Async alias for generate_image()."""
return self.generate_image(prompt, **kwargs)
def chat(self, prompt: str, **kwargs) -> Dict[str, Any]:
"""Generate an image from the prompt."""
try:
result = self.generate_image(prompt, **kwargs)
if self.verbose:
self.console.print(f"[green]Successfully generated image from prompt[/green]")
return result
except Exception as e:
error_msg = f"Failed to generate image: {str(e)}"
if self.verbose:
self.console.print(f"[red]{error_msg}[/red]")
return {"error": str(e)}
async def achat(
self,
prompt: str,
temperature: float = 0.2,
tools: Optional[List[Any]] = None,
output_json: Optional[str] = None,
output_pydantic: Optional[Any] = None,
reasoning_steps: bool = False,
**kwargs
) -> Union[str, Dict[str, Any]]:
"""Async chat method for image generation."""
try:
image_result = await self.agenerate_image(prompt, **kwargs)
if self.verbose:
self.console.print(f"[green]Successfully generated image from prompt[/green]")
return image_result
except Exception as e:
error_msg = f"Failed to generate image: {str(e)}"
if self.verbose:
self.console.print(f"[red]{error_msg}[/red]")
return {"error": str(e)}
def edit(
self,
image: str,
prompt: str,
mask: Optional[str] = None,
n: int = 1,
size: Optional[str] = None,
**kwargs
) -> Dict[str, Any]:
"""
Edit an existing image with a prompt.
Args:
image: Path or URL to the image to edit
prompt: Description of the desired edits
mask: Optional mask image (transparent areas will be edited)
n: Number of images to generate
size: Output image size
**kwargs: Additional provider-specific parameters
Returns:
ImageResponse with edited image(s)
Example:
```python
agent = ImageAgent(llm="openai/dall-e-2")
result = agent.edit("photo.png", "Add a sunset in the background")
```
"""
try:
import litellm
litellm.telemetry = False
model_name = self.llm or self.model or "dall-e-2"
config = {
"model": model_name,
"image": image,
"prompt": prompt,
"n": n,
}
if mask:
config["mask"] = mask
if size:
config["size"] = size
if self.image_config.api_key:
config["api_key"] = self.image_config.api_key
if self.image_config.api_base:
config["api_base"] = self.image_config.api_base
config.update(kwargs)
if self.verbose:
self.console.print(f"[cyan]Editing image with {model_name}...[/cyan]")
response = litellm.image_edit(**config)
if self.verbose:
self.console.print(f"[green]β Image edited successfully[/green]")
return response
except Exception as e:
error_msg = f"Error editing image: {str(e)}"
if self.verbose:
self.console.print(f"[red]{error_msg}[/red]")
raise
async def aedit(
self,
image: str,
prompt: str,
mask: Optional[str] = None,
n: int = 1,
size: Optional[str] = None,
**kwargs
) -> Dict[str, Any]:
"""Async version of edit()."""
return self.edit(image, prompt, mask, n, size, **kwargs)
def variation(
self,
image: str,
n: int = 1,
size: Optional[str] = None,
**kwargs
) -> Dict[str, Any]:
"""
Generate variations of an existing image.
Args:
image: Path or URL to the source image
n: Number of variations to generate
size: Output image size
**kwargs: Additional provider-specific parameters
Returns:
ImageResponse with image variations
Example:
```python
agent = ImageAgent(llm="openai/dall-e-2")
result = agent.variation("original.png", n=3)
```
"""
try:
import litellm
litellm.telemetry = False
model_name = self.llm or self.model or "dall-e-2"
config = {
"model": model_name,
"image": image,
"n": n,
}
if size:
config["size"] = size
if self.image_config.api_key:
config["api_key"] = self.image_config.api_key
if self.image_config.api_base:
config["api_base"] = self.image_config.api_base
config.update(kwargs)
if self.verbose:
self.console.print(f"[cyan]Generating variations with {model_name}...[/cyan]")
response = litellm.image_variation(**config)
if self.verbose:
self.console.print(f"[green]β Variations generated successfully[/green]")
return response
except Exception as e:
error_msg = f"Error generating variations: {str(e)}"
if self.verbose:
self.console.print(f"[red]{error_msg}[/red]")
raise
async def avariation(
self,
image: str,
n: int = 1,
size: Optional[str] = None,
**kwargs
) -> Dict[str, Any]:
"""Async version of variation()."""
return self.variation(image, n, size, **kwargs)