File size: 11,682 Bytes
5374a2d |
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 |
#!/usr/bin/env python3
"""
Example demonstrating how to use image handling toolkits from EvoAgentX.
This script provides comprehensive examples for:
- ImageAnalysisToolkit for analyzing images using AI
- OpenAI Image Generation for creating images from text prompts
- OpenAI Image Editing for editing existing images
- Flux Image Generation for creating images using Flux Kontext Max
"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
load_dotenv(override=True)
# Add the parent directory to sys.path to import from evoagentx
sys.path.append(str(Path(__file__).parent.parent))
from evoagentx.tools import (
OpenAIImageToolkit,
FluxImageGenerationToolkit,
OpenRouterImageToolkit
)
def run_image_analysis_example():
"""Simple example using OpenRouter image analysis to analyze images."""
print("\n===== IMAGE ANALYSIS TOOL EXAMPLE =====\n")
openrouter_api_key = os.getenv("OPENROUTER_API_KEY")
if not openrouter_api_key:
print("β OPENROUTER_API_KEY not found in environment variables")
return
try:
ortk = OpenRouterImageToolkit(name="DemoORImageToolkit", api_key=openrouter_api_key)
analyze_tool = ortk.get_tool("image_analysis")
test_image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
print(f"Analyzing image: {test_image_url}")
result = analyze_tool(prompt="Describe this image in detail.", image_url=test_image_url)
if 'error' in result:
print(f"β Image analysis failed: {result['error']}")
else:
print("β Analysis:")
print(result.get('content', ''))
except Exception as e:
print(f"Error: {str(e)}")
## (Removed) standalone OpenAI image generation example
## (Removed) standalone OpenAI image editing example
def run_openai_image_toolkit_pipeline():
"""Pipeline: generate β edit β analyze using OpenAIImageToolkit."""
print("\n===== OPENAI IMAGE TOOLKIT PIPELINE (GEN β EDIT β ANALYZE) =====\n")
openai_api_key = os.getenv("OPENAI_API_KEY")
openai_org_id = os.getenv("OPENAI_ORGANIZATION_ID")
if not openai_api_key:
print("β OPENAI_API_KEY not found in environment variables")
return
toolkit = OpenAIImageToolkit(
name="DemoOpenAIImageToolkit",
api_key=openai_api_key,
organization_id=openai_org_id,
generation_model="gpt-image-1",
save_path="./generated_images"
)
gen = toolkit.get_tool("openai_image_generation")
edit = toolkit.get_tool("openai_image_edit")
analyze = toolkit.get_tool("openai_image_analysis")
# 1) Generate
gen_prompt = "A cute baby owl sitting on a tree branch at sunset, digital art"
print(f"Generating: {gen_prompt}")
gen_result = gen(prompt=gen_prompt, model="gpt-image-1", size="1024x1024")
if 'error' in gen_result:
print(f"β Generation failed: {gen_result['error']}")
return
gen_paths = gen_result.get('results', [])
if not gen_paths:
print("β No generated images returned")
return
src_path = gen_paths[0]
print(f"Generated image: {src_path}")
# 2) Edit
print("Editing the generated image...")
edit_result = edit(
prompt="Add a red scarf around the owl's neck",
images=src_path,
size="1024x1024",
background="opaque",
quality="high",
n=1,
image_name="edited_minimal"
)
if 'error' in edit_result:
print(f"β Edit failed: {edit_result['error']}")
return
edited_paths = edit_result.get('results', [])
if not edited_paths:
print("β No edited images returned")
return
edited_path = edited_paths[0]
print(f"Edited image: {edited_path}")
# 3) Analyze (convert local file β data URL)
print("Analyzing the edited image...")
try:
analysis = analyze(
prompt="Summarize what's in this image in one sentence.",
image_path=edited_path,
model="gpt-4o-mini"
)
if 'error' in analysis:
print(f"β Analyze failed: {analysis['error']}")
else:
print("β Analysis:")
print(analysis.get('content', ''))
except Exception as e:
print(f"β Failed to analyze edited image: {e}")
def run_flux_image_generation_example():
"""Simple example using Flux Image Generation Toolkit."""
print("\n===== IMAGE GENERATION TOOL EXAMPLE =====\n")
# Check for BFL API key
bfl_api_key = os.getenv("BFL_API_KEY")
if not bfl_api_key:
print("β BFL_API_KEY not found in environment variables")
print("To test Flux image generation, set your BFL API key:")
print("export BFL_API_KEY='your-bfl-api-key-here'")
print("Get your key from: https://flux.ai/")
return
try:
# Initialize the Flux image generation toolkit
toolkit = FluxImageGenerationToolkit(
name="DemoFluxImageToolkit",
api_key=bfl_api_key,
save_path="./flux_generated_images"
)
print("β Image Generation Toolkit initialized")
print(f"β Using BFL API key: {bfl_api_key[:8]}...")
# Get the generation tool - the actual tool name is "flux_image_generation_edit"
generate_tool = toolkit.get_tool("flux_image_generation_edit")
# Test image generation
test_prompt = "A futuristic cyberpunk city with neon lights and flying cars, digital art style"
print(f"Generating image with prompt: '{test_prompt}'")
result = generate_tool(
prompt=test_prompt,
seed=42,
output_format="jpeg",
prompt_upsampling=False,
safety_tolerance=2
)
# The tool returns file_path directly, not in a success wrapper
if 'error' not in result:
print("β Image generation successful")
print(f"Generated image path: {result.get('file_path', 'No path')}")
# Check if file exists
file_path = result.get('file_path', '')
if file_path and os.path.exists(file_path):
file_size = os.path.getsize(file_path)
print(f"File size: {file_size} bytes")
print("β Generated image file saved successfully")
else:
print("β Generated image file not found")
else:
print(f"β Image generation failed: {result.get('error', 'Unknown error')}")
print("\nβ Image Generation Toolkit test completed")
except Exception as e:
print(f"Error: {str(e)}")
def run_flux_image_toolkit_pipeline():
"""Pipeline: generate β edit β analyze using Flux backend (input_image editing)."""
print("\n===== IMAGE TOOLKIT PIPELINE (GEN β EDIT β ANALYZE) =====\n")
bfl_api_key = os.getenv("BFL_API_KEY")
if not bfl_api_key:
print("β BFL_API_KEY not found in environment variables")
return
# Initialize toolkit
flux = FluxImageGenerationToolkit(
name="DemoFluxImageToolkitPipeline",
api_key=bfl_api_key,
save_path="./flux_generated_images"
)
gen = flux.get_tool("flux_image_generation_edit")
analyze = flux.get_tool("image_analysis") if flux.get_tool("image_analysis") else None
# 1) Generate base image
gen_prompt = "A neon-lit cyberpunk alley with rain reflections, cinematic"
print(f"Generating: {gen_prompt}")
gen_res = gen(
prompt=gen_prompt,
seed=42,
output_format="jpeg",
prompt_upsampling=False,
safety_tolerance=2
)
if 'error' in gen_res:
print(f"β Generation failed: {gen_res['error']}")
return
base_path = gen_res.get('file_path')
if not base_path or not os.path.exists(base_path):
print("β Generation did not return a valid file path")
return
print(f"Generated: {base_path}")
# 2) Edit by sending input_image (base64)
try:
import base64
with open(base_path, 'rb') as f:
b64_img = base64.b64encode(f.read()).decode('utf-8')
edit_prompt = "Add a glowing red umbrella held by a person in the foreground"
print("Editing the generated image...")
edit_res = gen(
prompt=edit_prompt,
input_image=b64_img,
seed=43,
output_format="jpeg",
prompt_upsampling=False,
safety_tolerance=2
)
if 'error' in edit_res:
print(f"β Edit failed: {edit_res['error']}")
return
edited_path = edit_res.get('file_path')
if not edited_path or not os.path.exists(edited_path):
print("β Edit did not return a valid file path")
return
print(f"Edited: {edited_path}")
except Exception as e:
print(f"β Failed to edit: {e}")
# 3) Analyze
if analyze and edited_path and os.path.exists(edited_path):
try:
import base64, mimetypes
with open(edited_path, 'rb') as f:
b64 = base64.b64encode(f.read()).decode('utf-8')
mime, _ = mimetypes.guess_type(edited_path)
mime = mime or 'image/jpeg'
data_url = f"data:{mime};base64,{b64}"
analysis = analyze(
prompt="Summarize what's in this image in one sentence.",
image_url=data_url,
)
if 'error' in analysis:
print(f"β Analyze failed: {analysis['error']}")
else:
print("β Analysis:")
print(analysis.get('content', ''))
except Exception as e:
print(f"β Failed to analyze: {e}")
def run_openrouter_edit_pipeline():
"""OpenRouter: generate β edit (with generated image as input) β save."""
print("\n===== OPENROUTER EDIT PIPELINE (GEN β EDIT) =====\n")
or_key = os.getenv("OPENROUTER_API_KEY")
if not or_key:
print("β OPENROUTER_API_KEY not found")
return
ortk = OpenRouterImageToolkit(name="DemoORImageToolkit", api_key=or_key)
gen = ortk.get_tool("openrouter_image_generation_edit")
# 1) generate
res = gen(
prompt="A minimalist poster of a mountain at sunrise",
model="google/gemini-2.5-flash-image-preview",
save_path="./openrouter_images",
output_basename="base"
)
bases = res.get('saved_paths', [])
if not bases:
print("β No base image saved; cannot proceed to edit")
return
base_path = bases[0]
print(f"Base image: {base_path}")
# 2) edit
edit_prompt = "Add a bold 'GEMINI' text at the top"
edit_res = gen(
prompt=edit_prompt,
image_paths=[base_path],
model="google/gemini-2.5-flash-image-preview",
save_path="./openrouter_images",
output_basename="edited"
)
edited = edit_res.get('saved_paths', [])
if not edited:
print("β No edited image saved")
return
print(f"Edited image: {edited[0]}")
def main():
"""Main function to run all image tool examples"""
print("===== IMAGE TOOL EXAMPLES =====")
# run_image_analysis_example()
# run_openai_image_toolkit_pipeline()
# run_flux_image_toolkit_pipeline()
# run_openrouter_edit_pipeline()
print("\n===== ALL IMAGE TOOL EXAMPLES COMPLETED =====")
if __name__ == "__main__":
main()
|