|
|
|
|
|
|
|
|
""" |
|
|
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) |
|
|
|
|
|
|
|
|
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)}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
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]}...") |
|
|
|
|
|
|
|
|
generate_tool = toolkit.get_tool("flux_image_generation_edit") |
|
|
|
|
|
|
|
|
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 |
|
|
) |
|
|
|
|
|
|
|
|
if 'error' not in result: |
|
|
print("β Image generation successful") |
|
|
print(f"Generated image path: {result.get('file_path', 'No path')}") |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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 =====") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("\n===== ALL IMAGE TOOL EXAMPLES COMPLETED =====") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|