Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
"""
|
| 2 |
-
Marketing Image Generator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import gradio as gr
|
|
@@ -11,240 +16,367 @@ import io
|
|
| 11 |
import os
|
| 12 |
import hashlib
|
| 13 |
import logging
|
| 14 |
-
import google.generativeai as genai
|
| 15 |
-
from google import genai as genai_sdk
|
| 16 |
|
| 17 |
# Configure logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
-
#
|
|
|
|
| 22 |
GOOGLE_SERVICE_ACCOUNT_JSON = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON", "")
|
| 23 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
credentials_dict = json.loads(GOOGLE_SERVICE_ACCOUNT_JSON)
|
| 31 |
from google.oauth2 import service_account
|
| 32 |
credentials = service_account.Credentials.from_service_account_info(credentials_dict)
|
| 33 |
genai.configure(credentials=credentials)
|
| 34 |
google_auth_configured = True
|
| 35 |
logger.info("β
Google Cloud configured")
|
| 36 |
-
|
| 37 |
-
logger.error(f"Google Cloud setup failed: {e}")
|
| 38 |
-
|
| 39 |
-
if not google_auth_configured and GOOGLE_API_KEY:
|
| 40 |
-
try:
|
| 41 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 42 |
google_auth_configured = True
|
| 43 |
-
logger.info("β
Google API
|
| 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 |
-
x = (width - text_width) // 2
|
| 89 |
-
y = (height - text_height) // 2 - 20
|
| 90 |
-
|
| 91 |
-
draw.text((x, y), text, fill="white", font=font)
|
| 92 |
-
|
| 93 |
-
# Style text
|
| 94 |
-
style_text = f"Style: {style}"
|
| 95 |
-
bbox2 = draw.textbbox((0, 0), style_text, font=font)
|
| 96 |
-
text_width2 = bbox2[2] - bbox2[0]
|
| 97 |
-
x2 = (width - text_width2) // 2
|
| 98 |
-
draw.text((x2, y + 30), style_text, fill="white", font=font)
|
| 99 |
-
|
| 100 |
-
except:
|
| 101 |
-
pass
|
| 102 |
-
|
| 103 |
-
return img
|
| 104 |
-
|
| 105 |
-
def generate_with_google_imagen(prompt, style):
|
| 106 |
-
"""Try to generate with real Google Imagen"""
|
| 107 |
-
if not google_auth_configured:
|
| 108 |
-
return None
|
| 109 |
-
|
| 110 |
-
try:
|
| 111 |
-
# Enhance prompt
|
| 112 |
-
enhanced_prompt = f"{prompt}, {style.lower()} style, high quality, professional marketing image, 4K"
|
| 113 |
-
|
| 114 |
-
if GOOGLE_SERVICE_ACCOUNT_JSON:
|
| 115 |
-
client = genai_sdk.Client()
|
| 116 |
-
else:
|
| 117 |
-
client = genai_sdk.Client(api_key=GOOGLE_API_KEY)
|
| 118 |
-
|
| 119 |
-
result = client.models.generate_images(
|
| 120 |
-
model="imagen-3.0-generate-002",
|
| 121 |
-
prompt=enhanced_prompt,
|
| 122 |
-
config={
|
| 123 |
-
"number_of_images": 1,
|
| 124 |
-
"output_mime_type": "image/png"
|
| 125 |
-
}
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
if result and hasattr(result, 'generated_images') and len(result.generated_images) > 0:
|
| 129 |
-
generated_image = result.generated_images[0]
|
| 130 |
-
if hasattr(generated_image, 'image') and hasattr(generated_image.image, 'image_bytes'):
|
| 131 |
-
image_bytes = generated_image.image.image_bytes
|
| 132 |
-
return Image.open(io.BytesIO(image_bytes))
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
- Marketing effectiveness
|
| 153 |
-
- How well it matches the prompt
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
if real_image:
|
| 174 |
-
review = review_with_gemini(real_image, prompt)
|
| 175 |
-
return real_image, f"β
Generated with Google Imagen\n\nAI Review: {review}"
|
| 176 |
|
| 177 |
-
|
| 178 |
-
placeholder = create_placeholder_image(prompt, style, aspect_ratio)
|
| 179 |
|
| 180 |
-
#
|
| 181 |
-
|
| 182 |
-
|
| 183 |
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# Status
|
| 197 |
-
if google_auth_configured
|
| 198 |
-
|
| 199 |
-
else:
|
| 200 |
-
gr.Markdown("β οΈ **Status**: Demo Mode - Configure Google Cloud for real AI generation")
|
| 201 |
|
| 202 |
with gr.Row():
|
| 203 |
-
with gr.Column():
|
| 204 |
-
# Inputs
|
| 205 |
prompt = gr.Textbox(
|
| 206 |
-
label="
|
| 207 |
-
placeholder="A professional team
|
| 208 |
-
lines=
|
| 209 |
)
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
)
|
| 223 |
|
| 224 |
-
generate_btn = gr.Button("
|
| 225 |
|
| 226 |
-
with gr.Column():
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
| 230 |
|
| 231 |
# Examples
|
| 232 |
-
gr.
|
| 233 |
-
examples = gr.Examples(
|
| 234 |
examples=[
|
| 235 |
-
["
|
| 236 |
-
["
|
| 237 |
-
["
|
| 238 |
-
["An inspiring workspace with laptop and coffee", "Modern", "Landscape (16:9)"]
|
| 239 |
],
|
| 240 |
inputs=[prompt, style, aspect_ratio]
|
| 241 |
)
|
| 242 |
|
| 243 |
-
# Connect
|
| 244 |
generate_btn.click(
|
| 245 |
-
fn=
|
| 246 |
inputs=[prompt, style, aspect_ratio],
|
| 247 |
-
outputs=[image_output,
|
|
|
|
| 248 |
)
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
|
|
|
| 1 |
"""
|
| 2 |
+
Marketing Image Generator - Agent Workflow Test Version
|
| 3 |
+
|
| 4 |
+
This version clearly shows the Agent1 -> Agent2 workflow:
|
| 5 |
+
- Agent1: Image Generator (calls Google Imagen)
|
| 6 |
+
- Agent2: Image Reviewer (calls Google Gemini Vision)
|
| 7 |
+
- Clear logging of each step
|
| 8 |
"""
|
| 9 |
|
| 10 |
import gradio as gr
|
|
|
|
| 16 |
import os
|
| 17 |
import hashlib
|
| 18 |
import logging
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Configure logging
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
+
# Google AI setup
|
| 25 |
+
google_auth_configured = False
|
| 26 |
GOOGLE_SERVICE_ACCOUNT_JSON = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON", "")
|
| 27 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
| 28 |
|
| 29 |
+
# Try to configure Google AI
|
| 30 |
+
try:
|
| 31 |
+
import google.generativeai as genai
|
| 32 |
+
from google import genai as genai_sdk
|
| 33 |
+
|
| 34 |
+
if GOOGLE_SERVICE_ACCOUNT_JSON:
|
| 35 |
credentials_dict = json.loads(GOOGLE_SERVICE_ACCOUNT_JSON)
|
| 36 |
from google.oauth2 import service_account
|
| 37 |
credentials = service_account.Credentials.from_service_account_info(credentials_dict)
|
| 38 |
genai.configure(credentials=credentials)
|
| 39 |
google_auth_configured = True
|
| 40 |
logger.info("β
Google Cloud configured")
|
| 41 |
+
elif GOOGLE_API_KEY:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 43 |
google_auth_configured = True
|
| 44 |
+
logger.info("β
Google API configured")
|
| 45 |
+
except Exception as e:
|
| 46 |
+
logger.error(f"Google setup failed: {e}")
|
| 47 |
+
genai = None
|
| 48 |
+
|
| 49 |
+
# ====== AGENT 1: IMAGE GENERATOR ======
|
| 50 |
+
|
| 51 |
+
class ImageGeneratorAgent:
|
| 52 |
+
"""Agent 1: Responsible for generating images using Google Imagen"""
|
| 53 |
+
|
| 54 |
+
def __init__(self):
|
| 55 |
+
self.name = "ImageGeneratorAgent"
|
| 56 |
+
self.status = "Ready"
|
| 57 |
+
|
| 58 |
+
def enhance_prompt(self, prompt, style):
|
| 59 |
+
"""Enhance user prompt for better results"""
|
| 60 |
+
logger.info(f"π€ Agent1: Enhancing prompt for {style} style")
|
| 61 |
+
|
| 62 |
+
style_enhancers = {
|
| 63 |
+
"Professional": "professional photography, corporate setting, high quality, clean composition, marketing ready",
|
| 64 |
+
"Creative": "creative composition, artistic flair, innovative design, vibrant colors, eye-catching",
|
| 65 |
+
"Minimalist": "clean minimal design, simple composition, white space, elegant, professional",
|
| 66 |
+
"Corporate": "business professional, corporate branding, trustworthy, authoritative, polished",
|
| 67 |
+
"Modern": "contemporary design, sleek, cutting-edge, stylish, trendy"
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
enhancer = style_enhancers.get(style, "high quality, professional")
|
| 71 |
+
enhanced = f"{prompt}, {enhancer}, 4K resolution, sharp focus, marketing quality"
|
| 72 |
+
|
| 73 |
+
logger.info(f"π€ Agent1: Enhanced prompt: {enhanced[:100]}...")
|
| 74 |
+
return enhanced
|
| 75 |
+
|
| 76 |
+
def generate_image(self, prompt, style, aspect_ratio):
|
| 77 |
+
"""Generate image using Google Imagen"""
|
| 78 |
+
logger.info(f"π€ Agent1: Starting image generation")
|
| 79 |
+
logger.info(f"π€ Agent1: Original prompt: {prompt}")
|
| 80 |
+
logger.info(f"π€ Agent1: Style: {style}, Format: {aspect_ratio}")
|
| 81 |
+
|
| 82 |
+
# Step 1: Enhance prompt
|
| 83 |
+
enhanced_prompt = self.enhance_prompt(prompt, style)
|
| 84 |
+
|
| 85 |
+
# Step 2: Try Google Imagen
|
| 86 |
+
if google_auth_configured and genai:
|
| 87 |
+
try:
|
| 88 |
+
logger.info(f"π€ Agent1: Calling Google Imagen API...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
if GOOGLE_SERVICE_ACCOUNT_JSON:
|
| 91 |
+
client = genai_sdk.Client()
|
| 92 |
+
else:
|
| 93 |
+
client = genai_sdk.Client(api_key=GOOGLE_API_KEY)
|
| 94 |
+
|
| 95 |
+
result = client.models.generate_images(
|
| 96 |
+
model="imagen-3.0-generate-002",
|
| 97 |
+
prompt=enhanced_prompt,
|
| 98 |
+
config={
|
| 99 |
+
"number_of_images": 1,
|
| 100 |
+
"output_mime_type": "image/png"
|
| 101 |
+
}
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if result and hasattr(result, 'generated_images') and result.generated_images:
|
| 105 |
+
img_data = result.generated_images[0]
|
| 106 |
+
if hasattr(img_data, 'image') and hasattr(img_data.image, 'image_bytes'):
|
| 107 |
+
image = Image.open(io.BytesIO(img_data.image.image_bytes))
|
| 108 |
+
logger.info(f"β
Agent1: Successfully generated real image with Google Imagen!")
|
| 109 |
+
return {
|
| 110 |
+
"success": True,
|
| 111 |
+
"image": image,
|
| 112 |
+
"method": "Google Imagen",
|
| 113 |
+
"enhanced_prompt": enhanced_prompt,
|
| 114 |
+
"agent": "ImageGeneratorAgent"
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
logger.warning(f"β οΈ Agent1: Google Imagen returned no images")
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
logger.error(f"β Agent1: Google Imagen failed: {e}")
|
| 121 |
+
|
| 122 |
+
# Step 3: Fallback to demo image
|
| 123 |
+
logger.info(f"π€ Agent1: Creating demo image (fallback)")
|
| 124 |
+
demo_image = self.create_demo_image(prompt, style, aspect_ratio)
|
| 125 |
+
|
| 126 |
+
return {
|
| 127 |
+
"success": True,
|
| 128 |
+
"image": demo_image,
|
| 129 |
+
"method": "Demo Mode",
|
| 130 |
+
"enhanced_prompt": enhanced_prompt,
|
| 131 |
+
"agent": "ImageGeneratorAgent"
|
| 132 |
+
}
|
| 133 |
|
| 134 |
+
def create_demo_image(self, prompt, style, aspect_ratio):
|
| 135 |
+
"""Create demo image when API isn't available"""
|
| 136 |
+
# Set dimensions
|
| 137 |
+
if "Square" in aspect_ratio:
|
| 138 |
+
width, height = 400, 400
|
| 139 |
+
elif "Portrait" in aspect_ratio:
|
| 140 |
+
width, height = 400, 500
|
| 141 |
+
else:
|
| 142 |
+
width, height = 500, 300
|
| 143 |
+
|
| 144 |
+
# Style colors
|
| 145 |
+
colors = {
|
| 146 |
+
"Professional": (59, 130, 246),
|
| 147 |
+
"Creative": (139, 92, 246),
|
| 148 |
+
"Minimalist": (107, 114, 128),
|
| 149 |
+
"Corporate": (30, 64, 175),
|
| 150 |
+
"Modern": (5, 150, 105)
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
color = colors.get(style, (59, 130, 246))
|
| 154 |
+
img = Image.new('RGB', (width, height), color)
|
| 155 |
+
draw = ImageDraw.Draw(img)
|
| 156 |
|
| 157 |
+
try:
|
| 158 |
+
font = ImageFont.load_default()
|
| 159 |
+
|
| 160 |
+
# Add text overlay
|
| 161 |
+
title = "Marketing Image"
|
| 162 |
+
bbox = draw.textbbox((0, 0), title, font=font)
|
| 163 |
+
text_width = bbox[2] - bbox[0]
|
| 164 |
+
x = (width - text_width) // 2
|
| 165 |
+
y = height // 2 - 30
|
| 166 |
+
draw.text((x, y), title, fill="white", font=font)
|
| 167 |
+
|
| 168 |
+
style_text = f"{style} Style"
|
| 169 |
+
bbox2 = draw.textbbox((0, 0), style_text, font=font)
|
| 170 |
+
text_width2 = bbox2[2] - bbox2[0]
|
| 171 |
+
x2 = (width - text_width2) // 2
|
| 172 |
+
draw.text((x2, y + 25), style_text, fill="white", font=font)
|
| 173 |
+
|
| 174 |
+
except:
|
| 175 |
+
pass
|
| 176 |
+
|
| 177 |
+
return img
|
| 178 |
+
|
| 179 |
+
# ====== AGENT 2: IMAGE REVIEWER ======
|
| 180 |
+
|
| 181 |
+
class ImageReviewerAgent:
|
| 182 |
+
"""Agent 2: Responsible for reviewing images using Google Gemini Vision"""
|
| 183 |
+
|
| 184 |
+
def __init__(self):
|
| 185 |
+
self.name = "ImageReviewerAgent"
|
| 186 |
+
self.status = "Ready"
|
| 187 |
+
|
| 188 |
+
def review_image(self, image, original_prompt, enhanced_prompt, generation_method):
|
| 189 |
+
"""Review generated image for quality and marketing effectiveness"""
|
| 190 |
+
logger.info(f"π Agent2: Starting image review")
|
| 191 |
+
logger.info(f"π Agent2: Reviewing image generated by: {generation_method}")
|
| 192 |
+
logger.info(f"π Agent2: Original prompt: {original_prompt}")
|
| 193 |
|
| 194 |
+
if google_auth_configured and genai and generation_method == "Google Imagen":
|
| 195 |
+
try:
|
| 196 |
+
logger.info(f"π Agent2: Calling Google Gemini Vision for AI review...")
|
| 197 |
+
|
| 198 |
+
model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 199 |
+
|
| 200 |
+
review_prompt = f"""You are an expert marketing image reviewer. Analyze this image that was generated from the prompt: "{original_prompt}"
|
| 201 |
+
|
| 202 |
+
Rate the image on a scale of 1-10 and provide specific feedback on:
|
| 203 |
+
1. Technical Quality (clarity, composition, lighting)
|
| 204 |
+
2. Marketing Effectiveness (professional appeal, brand suitability)
|
| 205 |
+
3. Prompt Matching (how well it matches the original request)
|
| 206 |
+
|
| 207 |
+
Provide your response in this format:
|
| 208 |
+
SCORE: X/10
|
| 209 |
+
QUALITY: [brief assessment]
|
| 210 |
+
MARKETING: [marketing effectiveness]
|
| 211 |
+
MATCHING: [how well it matches prompt]
|
| 212 |
+
RECOMMENDATIONS: [specific improvements]
|
| 213 |
+
|
| 214 |
+
Keep total response under 200 words."""
|
| 215 |
+
|
| 216 |
+
response = model.generate_content([review_prompt, image])
|
| 217 |
+
review_text = response.text
|
| 218 |
+
|
| 219 |
+
logger.info(f"β
Agent2: AI review completed successfully")
|
| 220 |
+
logger.info(f"π Agent2: Review preview: {review_text[:100]}...")
|
| 221 |
+
|
| 222 |
+
return {
|
| 223 |
+
"success": True,
|
| 224 |
+
"review": review_text,
|
| 225 |
+
"method": "Gemini Vision AI",
|
| 226 |
+
"agent": "ImageReviewerAgent"
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
except Exception as e:
|
| 230 |
+
logger.error(f"β Agent2: Gemini Vision review failed: {e}")
|
| 231 |
|
| 232 |
+
# Fallback review for demo mode
|
| 233 |
+
logger.info(f"π Agent2: Performing basic review (fallback)")
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
word_count = len(original_prompt.split())
|
| 236 |
+
if word_count > 15:
|
| 237 |
+
quality = "Excellent"
|
| 238 |
+
score = "8/10"
|
| 239 |
+
elif word_count > 8:
|
| 240 |
+
quality = "Good"
|
| 241 |
+
score = "7/10"
|
| 242 |
+
else:
|
| 243 |
+
quality = "Basic"
|
| 244 |
+
score = "6/10"
|
| 245 |
|
| 246 |
+
basic_review = f"""SCORE: {score}
|
| 247 |
+
QUALITY: {quality} prompt with {word_count} descriptive words
|
| 248 |
+
MARKETING: Suitable for {generation_method.lower()} marketing use
|
| 249 |
+
MATCHING: Generated image reflects the requested style and content
|
| 250 |
+
RECOMMENDATIONS: Add more specific details for enhanced results
|
| 251 |
+
|
| 252 |
+
Note: This is a basic review. Connect Google Cloud for full AI-powered analysis."""
|
| 253 |
|
| 254 |
+
return {
|
| 255 |
+
"success": True,
|
| 256 |
+
"review": basic_review,
|
| 257 |
+
"method": "Basic Analysis",
|
| 258 |
+
"agent": "ImageReviewerAgent"
|
| 259 |
+
}
|
| 260 |
|
| 261 |
+
# ====== ORCHESTRATOR: MANAGES AGENT WORKFLOW ======
|
| 262 |
+
|
| 263 |
+
def orchestrate_workflow(prompt, style, aspect_ratio):
|
| 264 |
+
"""Main workflow orchestrator - manages Agent1 -> Agent2 pipeline"""
|
| 265 |
+
logger.info(f"π ORCHESTRATOR: Starting agent workflow")
|
| 266 |
+
logger.info(f"π ORCHESTRATOR: Input - Prompt: {prompt}, Style: {style}, Format: {aspect_ratio}")
|
| 267 |
+
|
| 268 |
+
if not prompt or not prompt.strip():
|
| 269 |
+
return None, "β ORCHESTRATOR: No prompt provided", "Workflow failed"
|
| 270 |
+
|
| 271 |
+
workflow_log = []
|
| 272 |
+
|
| 273 |
+
# Step 1: Initialize agents
|
| 274 |
+
logger.info(f"π ORCHESTRATOR: Initializing agents...")
|
| 275 |
+
agent1 = ImageGeneratorAgent()
|
| 276 |
+
agent2 = ImageReviewerAgent()
|
| 277 |
+
workflow_log.append("β
Agents initialized")
|
| 278 |
+
|
| 279 |
+
# Step 2: Agent1 generates image
|
| 280 |
+
logger.info(f"π ORCHESTRATOR: Calling Agent1 (ImageGenerator)...")
|
| 281 |
+
workflow_log.append("π€ Agent1: Starting image generation...")
|
| 282 |
+
|
| 283 |
+
generation_result = agent1.generate_image(prompt, style, aspect_ratio)
|
| 284 |
|
| 285 |
+
if not generation_result["success"]:
|
| 286 |
+
logger.error(f"π ORCHESTRATOR: Agent1 failed")
|
| 287 |
+
return None, "β Image generation failed", "Workflow failed"
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
+
workflow_log.append(f"β
Agent1: Generated image using {generation_result['method']}")
|
|
|
|
| 290 |
|
| 291 |
+
# Step 3: Agent2 reviews image
|
| 292 |
+
logger.info(f"π ORCHESTRATOR: Calling Agent2 (ImageReviewer)...")
|
| 293 |
+
workflow_log.append("π Agent2: Starting image review...")
|
| 294 |
|
| 295 |
+
review_result = agent2.review_image(
|
| 296 |
+
generation_result["image"],
|
| 297 |
+
prompt,
|
| 298 |
+
generation_result["enhanced_prompt"],
|
| 299 |
+
generation_result["method"]
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
workflow_log.append(f"β
Agent2: Review completed using {review_result['method']}")
|
| 303 |
+
|
| 304 |
+
# Step 4: Compile final results
|
| 305 |
+
logger.info(f"π ORCHESTRATOR: Workflow completed successfully")
|
| 306 |
|
| 307 |
+
final_review = f"""π AGENT WORKFLOW COMPLETED
|
| 308 |
+
|
| 309 |
+
π WORKFLOW LOG:
|
| 310 |
+
{chr(10).join(workflow_log)}
|
| 311 |
+
|
| 312 |
+
π€ AGENT1 RESULT:
|
| 313 |
+
Generated using: {generation_result['method']}
|
| 314 |
+
Enhanced prompt: {generation_result['enhanced_prompt'][:100]}...
|
| 315 |
|
| 316 |
+
π AGENT2 REVIEW:
|
| 317 |
+
{review_result['review']}
|
| 318 |
+
|
| 319 |
+
π― SYSTEM STATUS: {"β
Full AI Pipeline" if google_auth_configured else "β οΈ Demo Mode"}
|
| 320 |
+
"""
|
| 321 |
|
| 322 |
+
return generation_result["image"], final_review, "β
Workflow Success"
|
| 323 |
+
|
| 324 |
+
# ====== GRADIO INTERFACE ======
|
| 325 |
+
|
| 326 |
+
with gr.Blocks(title="Marketing Image Generator - Agent Workflow") as demo:
|
| 327 |
+
|
| 328 |
+
gr.Markdown("# π¨ Marketing Image Generator - Agent Workflow")
|
| 329 |
+
gr.Markdown("**Agent1** (ImageGenerator) β **Agent2** (ImageReviewer) β **Results**")
|
| 330 |
|
| 331 |
# Status
|
| 332 |
+
status_msg = "β
Google AI Connected - Full Agent Pipeline" if google_auth_configured else "β οΈ Demo Mode - Simulated Agent Workflow"
|
| 333 |
+
gr.Markdown(f"**Status:** {status_msg}")
|
|
|
|
|
|
|
| 334 |
|
| 335 |
with gr.Row():
|
| 336 |
+
with gr.Column(scale=1):
|
|
|
|
| 337 |
prompt = gr.Textbox(
|
| 338 |
+
label="Marketing Image Description",
|
| 339 |
+
placeholder="A professional team meeting in a modern office...",
|
| 340 |
+
lines=2
|
| 341 |
)
|
| 342 |
|
| 343 |
+
style = gr.Dropdown(
|
| 344 |
+
["Professional", "Creative", "Minimalist", "Corporate", "Modern"],
|
| 345 |
+
value="Professional",
|
| 346 |
+
label="Style"
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
aspect_ratio = gr.Dropdown(
|
| 350 |
+
["Landscape (16:9)", "Square (1:1)", "Portrait (3:4)"],
|
| 351 |
+
value="Landscape (16:9)",
|
| 352 |
+
label="Format"
|
| 353 |
+
)
|
|
|
|
| 354 |
|
| 355 |
+
generate_btn = gr.Button("π Start Agent Workflow", variant="primary")
|
| 356 |
|
| 357 |
+
with gr.Column(scale=1):
|
| 358 |
+
image_output = gr.Image(label="Generated Image", height=300)
|
| 359 |
+
workflow_status = gr.Textbox(label="Workflow Status", lines=1, max_lines=1)
|
| 360 |
+
|
| 361 |
+
# Detailed workflow log
|
| 362 |
+
workflow_output = gr.Textbox(label="Agent Workflow Log", lines=8, max_lines=12)
|
| 363 |
|
| 364 |
# Examples
|
| 365 |
+
gr.Examples(
|
|
|
|
| 366 |
examples=[
|
| 367 |
+
["Professional team collaboration in modern office", "Professional", "Landscape (16:9)"],
|
| 368 |
+
["Clean product showcase on white background", "Minimalist", "Square (1:1)"],
|
| 369 |
+
["Customer service representative with headset", "Corporate", "Portrait (3:4)"]
|
|
|
|
| 370 |
],
|
| 371 |
inputs=[prompt, style, aspect_ratio]
|
| 372 |
)
|
| 373 |
|
| 374 |
+
# Connect workflow
|
| 375 |
generate_btn.click(
|
| 376 |
+
fn=orchestrate_workflow,
|
| 377 |
inputs=[prompt, style, aspect_ratio],
|
| 378 |
+
outputs=[image_output, workflow_output, workflow_status],
|
| 379 |
+
show_progress=True
|
| 380 |
)
|
| 381 |
|
| 382 |
if __name__ == "__main__":
|