Noo88ear commited on
Commit
8fc4f50
Β·
verified Β·
1 Parent(s): b0b7bfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -325
app.py CHANGED
@@ -1,21 +1,15 @@
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
11
  import json
12
- import time
13
- import base64
14
  from PIL import Image, ImageDraw, ImageFont
15
  import io
16
- import os
17
  import hashlib
18
- import logging
19
 
20
  # Configure logging
21
  logging.basicConfig(level=logging.INFO)
@@ -44,340 +38,180 @@ try:
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__":
383
- demo.launch()
 
1
  """
2
+ Marketing Image Generator with AI Review
3
+ Clean, simple Gradio interface
 
 
 
 
4
  """
5
 
6
  import gradio as gr
7
  import json
8
+ import os
9
+ import logging
10
  from PIL import Image, ImageDraw, ImageFont
11
  import io
 
12
  import hashlib
 
13
 
14
  # Configure logging
15
  logging.basicConfig(level=logging.INFO)
 
38
  logger.info("βœ… Google API configured")
39
  except Exception as e:
40
  logger.error(f"Google setup failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ def create_demo_image(prompt, style, width=512, height=512):
43
+ """Create demo image"""
44
+ colors = {
45
+ "Professional": (59, 130, 246),
46
+ "Creative": (139, 92, 246),
47
+ "Minimalist": (107, 114, 128),
48
+ "Corporate": (30, 64, 175),
49
+ "Modern": (5, 150, 105)
50
+ }
51
+
52
+ color = colors.get(style, (59, 130, 246))
53
+ img = Image.new('RGB', (width, height), color)
54
+ draw = ImageDraw.Draw(img)
55
+
56
+ try:
57
+ font = ImageFont.load_default()
58
+ title = "Marketing Image"
59
+ bbox = draw.textbbox((0, 0), title, font=font)
60
+ text_width = bbox[2] - bbox[0]
61
+ x = (width - text_width) // 2
62
+ y = height // 2 - 20
63
+ draw.text((x, y), title, fill="white", font=font)
64
+
65
+ style_text = f"{style} Style"
66
+ bbox2 = draw.textbbox((0, 0), style_text, font=font)
67
+ text_width2 = bbox2[2] - bbox2[0]
68
+ x2 = (width - text_width2) // 2
69
+ draw.text((x2, y + 30), style_text, fill="white", font=font)
70
+ except:
71
+ pass
72
+
73
+ return img
74
+
75
+ def generate_with_google_ai(prompt, style):
76
+ """Generate with Google Imagen"""
77
+ if not google_auth_configured:
78
+ return None
79
+
80
+ try:
81
+ enhanced_prompt = f"{prompt}, {style.lower()} style, professional marketing image, high quality, 4K"
82
+
83
+ if GOOGLE_SERVICE_ACCOUNT_JSON:
84
+ client = genai_sdk.Client()
85
  else:
86
+ client = genai_sdk.Client(api_key=GOOGLE_API_KEY)
87
+
88
+ result = client.models.generate_images(
89
+ model="imagen-3.0-generate-002",
90
+ prompt=enhanced_prompt,
91
+ config={"number_of_images": 1, "output_mime_type": "image/png"}
92
+ )
93
+
94
+ if result and hasattr(result, 'generated_images') and result.generated_images:
95
+ img_data = result.generated_images[0]
96
+ if hasattr(img_data, 'image') and hasattr(img_data.image, 'image_bytes'):
97
+ return Image.open(io.BytesIO(img_data.image.image_bytes))
98
+ except Exception as e:
99
+ logger.warning(f"Google Imagen failed: {e}")
100
+
101
+ return None
102
+
103
+ def review_with_ai(image, prompt):
104
+ """Review with Gemini"""
105
+ if not google_auth_configured:
106
+ return "Demo mode - Add Google credentials for AI review"
107
+
108
+ try:
109
+ model = genai.GenerativeModel('gemini-2.0-flash-exp')
110
+ review_prompt = f"Review this marketing image from: '{prompt}'. Rate 1-10 and give brief feedback. Keep under 100 words."
111
+ response = model.generate_content([review_prompt, image])
112
+ return response.text[:200]
113
+ except Exception as e:
114
+ logger.warning(f"AI review failed: {e}")
115
+ return "AI review unavailable"
116
+
117
+ def generate_marketing_image(prompt, style):
118
+ """Main function - Agent1 generates, Agent2 reviews"""
119
  if not prompt or not prompt.strip():
120
+ return None, "Please enter a prompt"
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
+ logger.info(f"πŸ€– Agent1: Generating image for '{prompt}' in {style} style")
123
 
124
+ # Agent1: Try Google Imagen
125
+ if google_auth_configured:
126
+ real_image = generate_with_google_ai(prompt, style)
127
+ if real_image:
128
+ logger.info("βœ… Agent1: Real image generated with Google Imagen")
129
+ logger.info("πŸ” Agent2: Starting AI review...")
130
+ review = review_with_ai(real_image, prompt)
131
+ logger.info("βœ… Agent2: Review completed")
132
+ return real_image, f"βœ… Google Imagen + AI Review\n\n{review}"
133
 
134
+ # Agent1: Fallback to demo
135
+ logger.info("πŸ€– Agent1: Creating demo image (fallback)")
136
+ demo_image = create_demo_image(prompt, style)
 
 
 
 
 
 
 
 
 
137
 
138
+ # Agent2: Basic review
139
+ logger.info("πŸ” Agent2: Basic review (fallback)")
140
+ word_count = len(prompt.split())
141
+ quality = "Good" if word_count > 10 else "Basic"
142
 
143
+ review = f"Demo Mode Review:\n\nPrompt Quality: {quality} ({word_count} words)\nStyle: {style}\nRecommendation: Add Google credentials for full AI generation and review."
 
144
 
145
+ return demo_image, review
146
 
147
+ # Interface configuration
148
+ title = "🎨 Marketing Image Generator with AI Review"
149
 
150
+ description = """
151
+ <p style='text-align: center'>
152
+ <b>Agent-Based Marketing Image Generation</b><br>
153
+ Agent1 creates images with Google Imagen3 β†’ Agent2 reviews with Gemini Vision
154
+ </p>
 
 
 
155
  """
 
 
156
 
157
+ article = """
158
+ <p style='text-align: center'>
159
+ <b>How it Works:</b><br>
160
+ 1. Agent1 (Generator) enhances your prompt and creates professional marketing images<br>
161
+ 2. Agent2 (Reviewer) analyzes the image for quality and marketing effectiveness<br>
162
+ 3. Get instant feedback and download high-quality results
163
+ </p>
164
+ """
165
 
166
+ examples = [
167
+ ["Professional team collaboration in modern office", "Professional"],
168
+ ["Clean product showcase on white background", "Minimalist"],
169
+ ["Customer service representative with headset", "Corporate"],
170
+ ["Modern workspace with laptop and plants", "Modern"],
171
+ ["Creative marketing team brainstorming session", "Creative"]
172
+ ]
173
+
174
+ def main():
175
+ # Status message
176
+ if google_auth_configured:
177
+ status_msg = "βœ… Google AI Connected - Full Agent Pipeline Active"
178
+ else:
179
+ status_msg = "⚠️ Demo Mode - Add GOOGLE_SERVICE_ACCOUNT_JSON for full AI capabilities"
180
+
181
+ full_description = f"""
182
+ {description}
183
+ <p style='text-align: center; color: {"green" if google_auth_configured else "orange"}'>
184
+ <b>Status:</b> {status_msg}
185
+ </p>
186
+ """
187
+
188
+ iface = gr.Interface(
189
+ fn=generate_marketing_image,
190
+ inputs=[
191
+ gr.Textbox(
192
  label="Marketing Image Description",
193
+ placeholder="A professional team meeting in a modern office space...",
194
+ lines=3
195
+ ),
196
+ gr.Dropdown(
197
+ choices=["Professional", "Creative", "Minimalist", "Corporate", "Modern"],
 
198
  value="Professional",
199
  label="Style"
200
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  ],
202
+ outputs=[
203
+ gr.Image(label="Generated Marketing Image", height=400),
204
+ gr.Textbox(label="AI Agent Review", lines=5)
205
+ ],
206
+ title=title,
207
+ description=full_description,
208
+ article=article,
209
+ examples=examples,
210
+ theme="soft",
211
+ allow_flagging="never"
212
  )
213
 
214
+ iface.launch()
 
 
 
 
 
 
215
 
216
+ if __name__ == '__main__':
217
+ main()