Skip to content

Commit a888b78

Browse files
committed
feat: Add sample image generation script
- Standalone script to demonstrate DALL-E 3 and gpt-image-1 image generation - Supports command-line arguments for prompt, size, quality, and output path - Includes themed examples function for generating multiple images - Uses the existing image_content_agent module
1 parent 3f039d0 commit a888b78

1 file changed

Lines changed: 253 additions & 0 deletions

File tree

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Sample Image Generation Script
4+
5+
This script demonstrates how to generate marketing images using the
6+
content-gen image generation capabilities (DALL-E 3 or gpt-image-1).
7+
8+
Prerequisites:
9+
1. Set up environment variables (or use a .env file):
10+
- AZURE_OPENAI_ENDPOINT: Your Azure OpenAI endpoint
11+
- AZURE_OPENAI_DALLE_ENDPOINT: (Optional) Dedicated DALL-E endpoint
12+
- AZURE_OPENAI_DALLE_MODEL: Model name (default: dall-e-3)
13+
- AZURE_OPENAI_IMAGE_MODEL: (Optional) Use "gpt-image-1" for GPT Image model
14+
15+
2. Ensure you have RBAC access:
16+
- "Cognitive Services OpenAI User" role on the Azure OpenAI resource
17+
18+
Usage:
19+
python sample_image_generation.py
20+
python sample_image_generation.py --prompt "A modern kitchen with stainless steel appliances"
21+
python sample_image_generation.py --size 1024x1792 --quality hd
22+
"""
23+
24+
import asyncio
25+
import argparse
26+
import base64
27+
import os
28+
import sys
29+
from datetime import datetime
30+
from pathlib import Path
31+
32+
# Add the backend directory to the path
33+
backend_path = Path(__file__).parent.parent / "src" / "backend"
34+
sys.path.insert(0, str(backend_path))
35+
36+
# Now import the image generation function
37+
from agents.image_content_agent import generate_dalle_image
38+
from settings import app_settings
39+
40+
41+
async def generate_sample_image(
42+
prompt: str,
43+
product_description: str = "",
44+
scene_description: str = "",
45+
size: str = None,
46+
quality: str = None,
47+
output_path: str = None
48+
) -> dict:
49+
"""
50+
Generate a sample marketing image.
51+
52+
Args:
53+
prompt: The main image generation prompt
54+
product_description: Optional product context for the image
55+
scene_description: Optional scene/setting description
56+
size: Image size (default from settings)
57+
quality: Image quality (default from settings)
58+
output_path: Path to save the generated image (optional)
59+
60+
Returns:
61+
Dictionary with generation results
62+
"""
63+
print(f"\n{'='*60}")
64+
print("IMAGE GENERATION SAMPLE")
65+
print(f"{'='*60}")
66+
print(f"\nModel: {app_settings.azure_openai.effective_image_model}")
67+
print(f"Endpoint: {app_settings.azure_openai.dalle_endpoint or app_settings.azure_openai.endpoint}")
68+
print(f"Size: {size or app_settings.azure_openai.image_size}")
69+
print(f"Quality: {quality or app_settings.azure_openai.image_quality}")
70+
print(f"\nPrompt: {prompt[:200]}{'...' if len(prompt) > 200 else ''}")
71+
72+
if product_description:
73+
print(f"Product context: {product_description[:100]}...")
74+
if scene_description:
75+
print(f"Scene: {scene_description[:100]}...")
76+
77+
print(f"\n{'='*60}")
78+
print("Generating image...")
79+
print(f"{'='*60}\n")
80+
81+
# Call the image generation function
82+
result = await generate_dalle_image(
83+
prompt=prompt,
84+
product_description=product_description,
85+
scene_description=scene_description,
86+
size=size,
87+
quality=quality
88+
)
89+
90+
if result.get("success"):
91+
print("✅ Image generated successfully!")
92+
print(f" Model used: {result.get('model')}")
93+
94+
if result.get("revised_prompt"):
95+
print(f" Revised prompt: {result['revised_prompt'][:150]}...")
96+
97+
# Save the image if we have base64 data
98+
if result.get("image_base64") and output_path:
99+
# Decode and save the image
100+
image_data = base64.b64decode(result["image_base64"])
101+
102+
# Ensure output directory exists
103+
output_dir = os.path.dirname(output_path)
104+
if output_dir:
105+
os.makedirs(output_dir, exist_ok=True)
106+
107+
with open(output_path, "wb") as f:
108+
f.write(image_data)
109+
110+
print(f" Saved to: {output_path}")
111+
print(f" File size: {len(image_data) / 1024:.1f} KB")
112+
elif result.get("image_base64"):
113+
# Generate default output path
114+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
115+
default_path = f"generated_image_{timestamp}.png"
116+
117+
image_data = base64.b64decode(result["image_base64"])
118+
with open(default_path, "wb") as f:
119+
f.write(image_data)
120+
121+
print(f" Saved to: {default_path}")
122+
print(f" File size: {len(image_data) / 1024:.1f} KB")
123+
else:
124+
print(f"❌ Image generation failed: {result.get('error')}")
125+
126+
return result
127+
128+
129+
async def main():
130+
"""Main entry point for the sample script."""
131+
parser = argparse.ArgumentParser(
132+
description="Generate marketing images using DALL-E 3 or gpt-image-1"
133+
)
134+
parser.add_argument(
135+
"--prompt", "-p",
136+
type=str,
137+
default="A modern, minimalist living room with comfortable furniture, soft natural lighting, and plants. Professional marketing photography style.",
138+
help="The image generation prompt"
139+
)
140+
parser.add_argument(
141+
"--product", "-d",
142+
type=str,
143+
default="",
144+
help="Product description for context"
145+
)
146+
parser.add_argument(
147+
"--scene", "-s",
148+
type=str,
149+
default="",
150+
help="Scene/setting description"
151+
)
152+
parser.add_argument(
153+
"--size",
154+
type=str,
155+
choices=["1024x1024", "1024x1792", "1792x1024", "1536x1024", "1024x1536"],
156+
default=None,
157+
help="Image size (default from settings)"
158+
)
159+
parser.add_argument(
160+
"--quality", "-q",
161+
type=str,
162+
choices=["standard", "hd", "low", "medium", "high"],
163+
default=None,
164+
help="Image quality (default from settings)"
165+
)
166+
parser.add_argument(
167+
"--output", "-o",
168+
type=str,
169+
default=None,
170+
help="Output file path for the generated image"
171+
)
172+
173+
args = parser.parse_args()
174+
175+
# Check if image generation is enabled
176+
if not app_settings.azure_openai.image_generation_enabled:
177+
print("❌ Image generation is not configured.")
178+
print(" Please set AZURE_OPENAI_DALLE_ENDPOINT or AZURE_OPENAI_ENDPOINT")
179+
print(" and ensure you have access to a DALL-E 3 or gpt-image-1 model.")
180+
sys.exit(1)
181+
182+
# Generate the image
183+
result = await generate_sample_image(
184+
prompt=args.prompt,
185+
product_description=args.product,
186+
scene_description=args.scene,
187+
size=args.size,
188+
quality=args.quality,
189+
output_path=args.output
190+
)
191+
192+
# Exit with appropriate code
193+
sys.exit(0 if result.get("success") else 1)
194+
195+
196+
# Example: Generate multiple themed images
197+
async def generate_themed_examples():
198+
"""Generate a set of example marketing images with different themes."""
199+
200+
themes = [
201+
{
202+
"name": "Modern Kitchen",
203+
"prompt": "A sleek modern kitchen with marble countertops, stainless steel appliances, and pendant lighting. Professional real estate photography.",
204+
"scene": "Bright, airy kitchen in a contemporary home",
205+
},
206+
{
207+
"name": "Outdoor Living",
208+
"prompt": "A beautiful outdoor patio with comfortable seating, string lights, and a fire pit at sunset. Lifestyle marketing photography.",
209+
"scene": "Warm evening atmosphere in a backyard setting",
210+
},
211+
{
212+
"name": "Home Office",
213+
"prompt": "A minimalist home office with a clean desk, ergonomic chair, natural wood accents, and large windows. Professional interior design photography.",
214+
"scene": "Productive workspace with natural lighting",
215+
},
216+
]
217+
218+
print("\n" + "="*60)
219+
print("GENERATING THEMED MARKETING IMAGES")
220+
print("="*60)
221+
222+
results = []
223+
for i, theme in enumerate(themes, 1):
224+
print(f"\n[{i}/{len(themes)}] Generating: {theme['name']}")
225+
226+
result = await generate_sample_image(
227+
prompt=theme["prompt"],
228+
scene_description=theme["scene"],
229+
output_path=f"sample_{theme['name'].lower().replace(' ', '_')}.png"
230+
)
231+
results.append({"theme": theme["name"], "result": result})
232+
233+
# Summary
234+
print("\n" + "="*60)
235+
print("GENERATION SUMMARY")
236+
print("="*60)
237+
238+
successful = sum(1 for r in results if r["result"].get("success"))
239+
print(f"\nSuccessfully generated: {successful}/{len(results)} images")
240+
241+
for r in results:
242+
status = "✅" if r["result"].get("success") else "❌"
243+
print(f" {status} {r['theme']}")
244+
245+
return results
246+
247+
248+
if __name__ == "__main__":
249+
# Run the main function
250+
asyncio.run(main())
251+
252+
# Uncomment below to run themed examples instead:
253+
# asyncio.run(generate_themed_examples())

0 commit comments

Comments
 (0)