Noo88ear commited on
Commit
b240d03
Β·
verified Β·
1 Parent(s): ac520ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -62,7 +62,7 @@ except ImportError:
62
  logging.basicConfig(level=logging.INFO)
63
  logger = logging.getLogger(__name__)
64
 
65
- # Get API keys - prioritize HuggingFace Secrets
66
  GCP_KEYS = [
67
  # Hugging Face Secrets (these are the primary ones for HF deployment)
68
  os.getenv("GOOGLE_API_KEY"),
@@ -96,14 +96,14 @@ def enhance_prompt_with_gemini(prompt: str, style: str) -> str:
96
  style (str): The desired image style
97
 
98
  Returns:
99
- str: Enhanced prompt optimized for image generation
100
  """
101
  if not GEMINI_AVAILABLE or not GOOGLE_API_KEY:
102
  # Basic enhancement without Gemini
103
  style_enhancers = {
104
  "realistic": "photorealistic, high detail, professional photography, sharp focus",
105
  "artistic": "artistic masterpiece, creative composition, painterly style",
106
- "cartoon": "cartoon style, vibrant colors, playful, animated character design",
107
  "photographic": "professional photograph, high quality, detailed, commercial photography",
108
  "illustration": "digital illustration, clean vector art, modern design"
109
  }
@@ -139,7 +139,7 @@ def enhance_prompt_with_gemini(prompt: str, style: str) -> str:
139
  style_enhancers = {
140
  "realistic": "photorealistic, high detail, professional photography",
141
  "artistic": "artistic masterpiece, creative composition",
142
- "cartoon": "cartoon style, vibrant colors, playful",
143
  "photographic": "professional photograph, high quality, detailed",
144
  "illustration": "digital illustration, clean design"
145
  }
@@ -169,18 +169,22 @@ def generate_marketing_image(prompt: str, style: str = "realistic") -> str:
169
  logger.info("🎨 Using Google Genai SDK for image generation")
170
  logger.info(f"API Key available: {GOOGLE_API_KEY[:10]}...")
171
 
172
- # Initialize the genai SDK client
173
  client = genai_sdk.Client(api_key=GOOGLE_API_KEY)
174
 
175
- # Generate image using Imagen 4.0 with reduced safety filtering
 
 
 
 
176
  result = client.models.generate_images(
177
  model="imagen-4.0-generate-preview-06-06",
178
  prompt=enhanced_prompt,
179
  config={
180
  "number_of_images": 1,
181
  "output_mime_type": "image/png",
182
- "safety_filter_level": "block_low_and_above",
183
- "include_safety_attributes": False
184
  }
185
  )
186
 
@@ -244,9 +248,9 @@ def generate_marketing_image(prompt: str, style: str = "realistic") -> str:
244
  "style": style
245
  })
246
 
247
- def analyze_marketing_image_with_gemini(image_url: str, prompt: str, review_guidelines: str = "") -> str:
248
  """
249
- Analyze a generated marketing image using Gemini Vision for quality, relevance, and compliance.
250
 
251
  Args:
252
  image_url (str): URL or base64 data of the generated image
@@ -272,7 +276,7 @@ def analyze_marketing_image_with_gemini(image_url: str, prompt: str, review_guid
272
 
273
  CRITICAL MARKETING CHECKS:
274
  1. **Language/Text Requirements**: If guidelines mention "English" or specific language requirements, verify ALL visible text matches
275
- 2. **Brand Compliance**: Check professional appearance, color consistency, readability
276
  3. **Marketing Effectiveness**: Assess visual appeal and message clarity
277
  4. **Target Audience**: Evaluate cultural appropriateness and accessibility
278
 
@@ -502,9 +506,9 @@ def generate_and_review_marketing_image(prompt: str, style: str = "realistic", r
502
  })
503
  continue
504
 
505
- # Step 2: Analyze the generated image with Gemini Vision
506
  image_url = generation_data.get("image_url", "")
507
- analysis_response = analyze_marketing_image_with_gemini(image_url, prompt, review_guidelines)
508
  analysis_data = json.loads(analysis_response)
509
 
510
  current_score = analysis_data.get("overall_score", 0.0)
@@ -612,8 +616,10 @@ def process_generated_image_and_results(api_response_str: str) -> Tuple[Image.Im
612
 
613
  # Add specific documentation links for common errors
614
  doc_link = ""
615
- if any(brand in error_msg.lower() for brand in ['hsbc', 'bank', 'political', 'timeout', 'stall']):
616
- doc_link = "\n\nπŸ“– See content restrictions guide: https://huggingface.co/spaces/CognizantAI/marketing-image-generator/blob/main/README.md#content-policy--brand-restrictions"
 
 
617
  elif 'api' in error_msg.lower() or 'key' in error_msg.lower():
618
  doc_link = "\n\nπŸ“– See API troubleshooting: https://huggingface.co/spaces/CognizantAI/marketing-image-generator/blob/main/README.md#common-issues"
619
 
@@ -731,7 +737,7 @@ def gradio_generate_marketing_image(prompt: str, style: str, max_retries: int, r
731
  )
732
  return process_generated_image_and_results(result_json)
733
  except Exception as e:
734
- error_message = f"❌ Error: {str(e)}\n\nπŸ“– For troubleshooting help, see: https://huggingface.co/spaces/CognizantAI/marketing-image-generator/blob/main/README.md#content-policy--brand-restrictions"
735
  logger.error(error_message)
736
  return None, error_message
737
 
@@ -739,10 +745,10 @@ def gradio_generate_marketing_image(prompt: str, style: str, max_retries: int, r
739
  SUGGESTED_PROMPTS = {
740
  "Modern office team collaboration": ("A modern office space with diverse professionals collaborating around a sleek conference table, natural lighting, professional attire, English signage visible", "realistic"),
741
  "Executive boardroom meeting": ("Professional executive boardroom with polished conference table, city skyline view, business documents, English presentations on screens", "realistic"),
742
- "Customer service excellence": ("Professional customer service representative with headset in modern call center, English signage, clean corporate environment", "realistic"),
743
  "Product showcase display": ("Clean product showcase on white background with professional lighting, English product labels, minimalist marketing aesthetic", "realistic"),
744
- "Creative workspace design": ("Creative workspace with colorful design elements, inspirational English quotes on walls, modern furniture, artistic marketing materials", "artistic"),
745
- "Brand presentation setup": ("Professional brand presentation setup with English branded materials, corporate colors, marketing displays, conference room setting", "realistic")
746
  }
747
 
748
  # Create Gradio interface
 
62
  logging.basicConfig(level=logging.INFO)
63
  logger = logging.getLogger(__name__)
64
 
65
+ # Get API keys - prioritise HuggingFace Secrets
66
  GCP_KEYS = [
67
  # Hugging Face Secrets (these are the primary ones for HF deployment)
68
  os.getenv("GOOGLE_API_KEY"),
 
96
  style (str): The desired image style
97
 
98
  Returns:
99
+ str: Enhanced prompt optimised for image generation
100
  """
101
  if not GEMINI_AVAILABLE or not GOOGLE_API_KEY:
102
  # Basic enhancement without Gemini
103
  style_enhancers = {
104
  "realistic": "photorealistic, high detail, professional photography, sharp focus",
105
  "artistic": "artistic masterpiece, creative composition, painterly style",
106
+ "cartoon": "cartoon style, vibrant colours, playful, animated character design",
107
  "photographic": "professional photograph, high quality, detailed, commercial photography",
108
  "illustration": "digital illustration, clean vector art, modern design"
109
  }
 
139
  style_enhancers = {
140
  "realistic": "photorealistic, high detail, professional photography",
141
  "artistic": "artistic masterpiece, creative composition",
142
+ "cartoon": "cartoon style, vibrant colours, playful",
143
  "photographic": "professional photograph, high quality, detailed",
144
  "illustration": "digital illustration, clean design"
145
  }
 
169
  logger.info("🎨 Using Google Genai SDK for image generation")
170
  logger.info(f"API Key available: {GOOGLE_API_KEY[:10]}...")
171
 
172
+ # Initialise the genai SDK client
173
  client = genai_sdk.Client(api_key=GOOGLE_API_KEY)
174
 
175
+ # Generate image using Imagen 4.0 with optimised safety filtering
176
+ # Safety configuration: "block_low_and_above" - allows corporate/business content
177
+ # while maintaining essential safety guardrails. This setting significantly
178
+ # improves generation success for financial institutions, corporate brands,
179
+ # and marketing content while blocking genuinely harmful content.
180
  result = client.models.generate_images(
181
  model="imagen-4.0-generate-preview-06-06",
182
  prompt=enhanced_prompt,
183
  config={
184
  "number_of_images": 1,
185
  "output_mime_type": "image/png",
186
+ "safety_filter_level": "block_low_and_above", # Reduced from default strict filtering
187
+ "include_safety_attributes": False # Cleaner response without safety metadata
188
  }
189
  )
190
 
 
248
  "style": style
249
  })
250
 
251
+ def analyse_marketing_image_with_gemini(image_url: str, prompt: str, review_guidelines: str = "") -> str:
252
  """
253
+ Analyse a generated marketing image using Gemini Vision for quality, relevance, and compliance.
254
 
255
  Args:
256
  image_url (str): URL or base64 data of the generated image
 
276
 
277
  CRITICAL MARKETING CHECKS:
278
  1. **Language/Text Requirements**: If guidelines mention "English" or specific language requirements, verify ALL visible text matches
279
+ 2. **Brand Compliance**: Check professional appearance, colour consistency, readability
280
  3. **Marketing Effectiveness**: Assess visual appeal and message clarity
281
  4. **Target Audience**: Evaluate cultural appropriateness and accessibility
282
 
 
506
  })
507
  continue
508
 
509
+ # Step 2: Analyse the generated image with Gemini Vision
510
  image_url = generation_data.get("image_url", "")
511
+ analysis_response = analyse_marketing_image_with_gemini(image_url, prompt, review_guidelines)
512
  analysis_data = json.loads(analysis_response)
513
 
514
  current_score = analysis_data.get("overall_score", 0.0)
 
616
 
617
  # Add specific documentation links for common errors
618
  doc_link = ""
619
+ if any(keyword in error_msg.lower() for keyword in ['political', 'timeout', 'stall']):
620
+ doc_link = "\n\nπŸ“– See updated safety configuration: https://huggingface.co/spaces/CognizantAI/marketing-image-generator/blob/main/README.md#content-policy--safety-configuration"
621
+ elif any(keyword in error_msg.lower() for keyword in ['hsbc', 'bank', 'corporate']):
622
+ doc_link = "\n\nπŸ’‘ Note: Financial brands now work better with reduced safety filtering. See: https://huggingface.co/spaces/CognizantAI/marketing-image-generator/blob/main/README.md#improved-content-support"
623
  elif 'api' in error_msg.lower() or 'key' in error_msg.lower():
624
  doc_link = "\n\nπŸ“– See API troubleshooting: https://huggingface.co/spaces/CognizantAI/marketing-image-generator/blob/main/README.md#common-issues"
625
 
 
737
  )
738
  return process_generated_image_and_results(result_json)
739
  except Exception as e:
740
+ error_message = f"❌ Error: {str(e)}\n\nπŸ“– For troubleshooting help, see: https://huggingface.co/spaces/CognizantAI/marketing-image-generator/blob/main/README.md#content-policy--safety-configuration"
741
  logger.error(error_message)
742
  return None, error_message
743
 
 
745
  SUGGESTED_PROMPTS = {
746
  "Modern office team collaboration": ("A modern office space with diverse professionals collaborating around a sleek conference table, natural lighting, professional attire, English signage visible", "realistic"),
747
  "Executive boardroom meeting": ("Professional executive boardroom with polished conference table, city skyline view, business documents, English presentations on screens", "realistic"),
748
+ "Customer service excellence": ("Professional customer service representative with headset in modern call centre, English signage, clean corporate environment", "realistic"),
749
  "Product showcase display": ("Clean product showcase on white background with professional lighting, English product labels, minimalist marketing aesthetic", "realistic"),
750
+ "Creative workspace design": ("Creative workspace with colourful design elements, inspirational English quotes on walls, modern furniture, artistic marketing materials", "artistic"),
751
+ "Brand presentation setup": ("Professional brand presentation setup with English branded materials, corporate colours, marketing displays, conference room setting", "realistic")
752
  }
753
 
754
  # Create Gradio interface