sushilideaclan01 commited on
Commit
4928a87
·
1 Parent(s): 29066e0

variation in gallery

Browse files
Files changed (1) hide show
  1. services/generator.py +134 -112
services/generator.py CHANGED
@@ -1536,7 +1536,7 @@ CRITICAL REQUIREMENTS:
1536
  "visual_styles": visual_styles,
1537
  }
1538
 
1539
- # Save to database (for first image)
1540
  # Ensure database connection is initialized
1541
  if db_service and db_service.collection is None and settings.mongodb_url:
1542
  try:
@@ -1544,42 +1544,50 @@ CRITICAL REQUIREMENTS:
1544
  except Exception as e:
1545
  print(f"Warning: Could not connect to database: {e}")
1546
 
1547
- first_image = generated_images[0] if generated_images else {}
1548
- if db_service and db_service.collection is not None and not first_image.get("error") and username:
1549
- try:
1550
- db_id = await db_service.save_ad_creative(
1551
- niche=niche,
1552
- title=None, # No title for standard flow
1553
- headline=ad_copy.get("headline", ""),
1554
- primary_text=ad_copy.get("primary_text", ""),
1555
- description=ad_copy.get("description", ""),
1556
- body_story=ad_copy.get("body_story", ""),
1557
- cta=ad_copy.get("cta", ""),
1558
- psychological_angle=ad_copy.get("psychological_angle", ""),
1559
- why_it_works=ad_copy.get("why_it_works", ""),
1560
- username=username, # Pass username
1561
- image_url=first_image.get("image_url"),
1562
- image_filename=first_image.get("filename"),
1563
- image_model=first_image.get("model_used"),
1564
- image_seed=first_image.get("seed"),
1565
- image_prompt=first_image.get("image_prompt"), # Save the final refined prompt
1566
- generation_method="standard",
1567
- angle_key=angle.get("key"),
1568
- angle_name=angle.get("name"),
1569
- angle_trigger=angle.get("trigger"),
1570
- angle_category=angle.get("category"),
1571
- concept_key=concept.get("key"),
1572
- concept_name=concept.get("name"),
1573
- concept_structure=concept.get("structure"),
1574
- concept_visual=concept.get("visual"),
1575
- concept_category=concept.get("category"),
1576
- metadata=metadata,
1577
- )
1578
- if db_id:
1579
- ad_id = db_id
1580
- print(f" Saved ad creative to database: {ad_id}")
1581
- except Exception as e:
1582
- print(f"Warning: Failed to save to database: {e}")
 
 
 
 
 
 
 
 
1583
 
1584
  # Build response
1585
  result = {
@@ -1791,7 +1799,7 @@ CONCEPT: {concept['name']}
1791
  # Generate unique ID
1792
  ad_id = str(uuid.uuid4())
1793
 
1794
- # Save to database (for first image)
1795
  # Ensure database connection is initialized
1796
  if db_service and db_service.collection is None and settings.mongodb_url:
1797
  try:
@@ -1799,42 +1807,50 @@ CONCEPT: {concept['name']}
1799
  except Exception as e:
1800
  print(f"Warning: Could not connect to database: {e}")
1801
 
1802
- first_image = images[0] if images else {}
1803
- if db_service and db_service.collection is not None and not first_image.get("error") and username:
1804
- try:
1805
- db_id = await db_service.save_ad_creative(
1806
- niche=niche,
1807
- title=None, # No title for matrix flow
1808
- headline=ad_copy.get("headline", ""),
1809
- primary_text=ad_copy.get("primary_text", ""),
1810
- description=ad_copy.get("description", ""),
1811
- body_story=ad_copy.get("body_story", ""),
1812
- cta=ad_copy.get("cta", ""),
1813
- psychological_angle=ad_copy.get("psychological_angle", angle.get("name", "")),
1814
- why_it_works=ad_copy.get("why_it_works", ""),
1815
- username=username, # Pass username
1816
- image_url=first_image.get("image_url"),
1817
- image_filename=first_image.get("filename"),
1818
- image_model=first_image.get("model_used"),
1819
- image_seed=first_image.get("seed"),
1820
- image_prompt=first_image.get("image_prompt"), # Save the final refined prompt
1821
- angle_key=angle.get("key"),
1822
- angle_name=angle.get("name"),
1823
- angle_trigger=angle.get("trigger"),
1824
- angle_category=angle.get("category"),
1825
- concept_key=concept.get("key"),
1826
- concept_name=concept.get("name"),
1827
- concept_structure=concept.get("structure"),
1828
- concept_visual=concept.get("visual"),
1829
- concept_category=concept.get("category"),
1830
- generation_method="angle_concept_matrix",
1831
- metadata={"generation_method": "angle_concept_matrix"},
1832
- )
1833
- if db_id:
1834
- ad_id = db_id
1835
- print(f" Saved matrix ad creative to database: {ad_id}")
1836
- except Exception as e:
1837
- print(f"Warning: Failed to save to database: {e}")
 
 
 
 
 
 
 
 
1838
 
1839
  return {
1840
  "id": ad_id,
@@ -2089,9 +2105,6 @@ CONCEPT: {concept['name']}
2089
  print(f"Warning: No images generated for strategy {idx + 1}, skipping...")
2090
  continue
2091
 
2092
- # Get first image for database
2093
- first_image = generated_images[0]
2094
-
2095
  # Build ad copy
2096
  strategy = creative_strategies[idx]
2097
  headline = title or strategy.titleIdeas or "Check this out"
@@ -2099,44 +2112,53 @@ CONCEPT: {concept['name']}
2099
  description_text = description or strategy.captionIdeas or ""
2100
  cta = strategy.cta or "Learn More"
2101
 
2102
- # Save to database
2103
  ad_id = str(uuid.uuid4())
2104
- db_id = None
2105
 
2106
  if db_service and username:
2107
- try:
2108
- db_id = await db_service.save_ad_creative(
2109
- niche=niche,
2110
- title=title or "",
2111
- headline=headline,
2112
- primary_text=primary_text,
2113
- description=description_text,
2114
- body_story=primary_text, # Use primary_text as body_story
2115
- cta=cta,
2116
- psychological_angle=strategy.phsychologyTrigger or "",
2117
- why_it_works=f"Angle: {strategy.angle}, Concept: {strategy.concept}",
2118
- username=username, # Pass username
2119
- image_url=first_image.get("image_url"),
2120
- image_filename=first_image.get("filename"),
2121
- image_model=first_image.get("model_used"),
2122
- image_prompt=first_image.get("image_prompt"),
2123
- generation_method="extensive",
2124
- metadata={
2125
- "generation_method": "extensive",
2126
- "target_audience": target_audience,
2127
- "offer": offer,
2128
- "strategy_index": idx,
2129
- "psychology_trigger": strategy.phsychologyTrigger,
2130
- "angle": strategy.angle,
2131
- "concept": strategy.concept,
2132
- "visual_direction": strategy.visualDirection,
2133
- },
2134
- )
2135
- if db_id:
2136
- ad_id = db_id
2137
- print(f"✓ Saved extensive ad creative to database: {ad_id}")
2138
- except Exception as e:
2139
- print(f"Warning: Failed to save to database: {e}")
 
 
 
 
 
 
 
 
 
2140
 
2141
  all_results.append({
2142
  "id": ad_id,
 
1536
  "visual_styles": visual_styles,
1537
  }
1538
 
1539
+ # Save to database (save EACH image as a separate record for gallery visibility)
1540
  # Ensure database connection is initialized
1541
  if db_service and db_service.collection is None and settings.mongodb_url:
1542
  try:
 
1544
  except Exception as e:
1545
  print(f"Warning: Could not connect to database: {e}")
1546
 
1547
+ # Save each image variation as a separate database record
1548
+ saved_ad_ids = []
1549
+ if db_service and db_service.collection is not None and username:
1550
+ for img_idx, image in enumerate(generated_images):
1551
+ if image.get("error"):
1552
+ continue # Skip failed images
1553
+ try:
1554
+ db_id = await db_service.save_ad_creative(
1555
+ niche=niche,
1556
+ title=None, # No title for standard flow
1557
+ headline=ad_copy.get("headline", ""),
1558
+ primary_text=ad_copy.get("primary_text", ""),
1559
+ description=ad_copy.get("description", ""),
1560
+ body_story=ad_copy.get("body_story", ""),
1561
+ cta=ad_copy.get("cta", ""),
1562
+ psychological_angle=ad_copy.get("psychological_angle", ""),
1563
+ why_it_works=ad_copy.get("why_it_works", ""),
1564
+ username=username, # Pass username
1565
+ image_url=image.get("image_url"),
1566
+ image_filename=image.get("filename"),
1567
+ image_model=image.get("model_used"),
1568
+ image_seed=image.get("seed"),
1569
+ image_prompt=image.get("image_prompt"), # Save the final refined prompt
1570
+ generation_method="standard",
1571
+ angle_key=angle.get("key"),
1572
+ angle_name=angle.get("name"),
1573
+ angle_trigger=angle.get("trigger"),
1574
+ angle_category=angle.get("category"),
1575
+ concept_key=concept.get("key"),
1576
+ concept_name=concept.get("name"),
1577
+ concept_structure=concept.get("structure"),
1578
+ concept_visual=concept.get("visual"),
1579
+ concept_category=concept.get("category"),
1580
+ metadata={**metadata, "variation_index": img_idx, "total_variations": len(generated_images)},
1581
+ )
1582
+ if db_id:
1583
+ saved_ad_ids.append(db_id)
1584
+ print(f"✓ Saved ad creative variation {img_idx + 1}/{len(generated_images)} to database: {db_id}")
1585
+ except Exception as e:
1586
+ print(f"Warning: Failed to save variation {img_idx + 1} to database: {e}")
1587
+
1588
+ # Use first saved ID as the main ad_id
1589
+ if saved_ad_ids:
1590
+ ad_id = saved_ad_ids[0]
1591
 
1592
  # Build response
1593
  result = {
 
1799
  # Generate unique ID
1800
  ad_id = str(uuid.uuid4())
1801
 
1802
+ # Save to database (save EACH image as a separate record for gallery visibility)
1803
  # Ensure database connection is initialized
1804
  if db_service and db_service.collection is None and settings.mongodb_url:
1805
  try:
 
1807
  except Exception as e:
1808
  print(f"Warning: Could not connect to database: {e}")
1809
 
1810
+ # Save each image variation as a separate database record
1811
+ saved_ad_ids = []
1812
+ if db_service and db_service.collection is not None and username:
1813
+ for img_idx, image in enumerate(images):
1814
+ if image.get("error"):
1815
+ continue # Skip failed images
1816
+ try:
1817
+ db_id = await db_service.save_ad_creative(
1818
+ niche=niche,
1819
+ title=None, # No title for matrix flow
1820
+ headline=ad_copy.get("headline", ""),
1821
+ primary_text=ad_copy.get("primary_text", ""),
1822
+ description=ad_copy.get("description", ""),
1823
+ body_story=ad_copy.get("body_story", ""),
1824
+ cta=ad_copy.get("cta", ""),
1825
+ psychological_angle=ad_copy.get("psychological_angle", angle.get("name", "")),
1826
+ why_it_works=ad_copy.get("why_it_works", ""),
1827
+ username=username, # Pass username
1828
+ image_url=image.get("image_url"),
1829
+ image_filename=image.get("filename"),
1830
+ image_model=image.get("model_used"),
1831
+ image_seed=image.get("seed"),
1832
+ image_prompt=image.get("image_prompt"), # Save the final refined prompt
1833
+ angle_key=angle.get("key"),
1834
+ angle_name=angle.get("name"),
1835
+ angle_trigger=angle.get("trigger"),
1836
+ angle_category=angle.get("category"),
1837
+ concept_key=concept.get("key"),
1838
+ concept_name=concept.get("name"),
1839
+ concept_structure=concept.get("structure"),
1840
+ concept_visual=concept.get("visual"),
1841
+ concept_category=concept.get("category"),
1842
+ generation_method="angle_concept_matrix",
1843
+ metadata={"generation_method": "angle_concept_matrix", "variation_index": img_idx, "total_variations": len(images)},
1844
+ )
1845
+ if db_id:
1846
+ saved_ad_ids.append(db_id)
1847
+ print(f"✓ Saved matrix ad creative variation {img_idx + 1}/{len(images)} to database: {db_id}")
1848
+ except Exception as e:
1849
+ print(f"Warning: Failed to save matrix variation {img_idx + 1} to database: {e}")
1850
+
1851
+ # Use first saved ID as the main ad_id
1852
+ if saved_ad_ids:
1853
+ ad_id = saved_ad_ids[0]
1854
 
1855
  return {
1856
  "id": ad_id,
 
2105
  print(f"Warning: No images generated for strategy {idx + 1}, skipping...")
2106
  continue
2107
 
 
 
 
2108
  # Build ad copy
2109
  strategy = creative_strategies[idx]
2110
  headline = title or strategy.titleIdeas or "Check this out"
 
2112
  description_text = description or strategy.captionIdeas or ""
2113
  cta = strategy.cta or "Learn More"
2114
 
2115
+ # Save to database (save EACH image as a separate record for gallery visibility)
2116
  ad_id = str(uuid.uuid4())
2117
+ saved_ad_ids = []
2118
 
2119
  if db_service and username:
2120
+ for img_idx, image in enumerate(generated_images):
2121
+ if image.get("error"):
2122
+ continue # Skip failed images
2123
+ try:
2124
+ db_id = await db_service.save_ad_creative(
2125
+ niche=niche,
2126
+ title=title or "",
2127
+ headline=headline,
2128
+ primary_text=primary_text,
2129
+ description=description_text,
2130
+ body_story=primary_text, # Use primary_text as body_story
2131
+ cta=cta,
2132
+ psychological_angle=strategy.phsychologyTrigger or "",
2133
+ why_it_works=f"Angle: {strategy.angle}, Concept: {strategy.concept}",
2134
+ username=username, # Pass username
2135
+ image_url=image.get("image_url"),
2136
+ image_filename=image.get("filename"),
2137
+ image_model=image.get("model_used"),
2138
+ image_prompt=image.get("image_prompt"),
2139
+ generation_method="extensive",
2140
+ metadata={
2141
+ "generation_method": "extensive",
2142
+ "target_audience": target_audience,
2143
+ "offer": offer,
2144
+ "strategy_index": idx,
2145
+ "psychology_trigger": strategy.phsychologyTrigger,
2146
+ "angle": strategy.angle,
2147
+ "concept": strategy.concept,
2148
+ "visual_direction": strategy.visualDirection,
2149
+ "variation_index": img_idx,
2150
+ "total_variations": len(generated_images),
2151
+ },
2152
+ )
2153
+ if db_id:
2154
+ saved_ad_ids.append(db_id)
2155
+ print(f"✓ Saved extensive ad creative variation {img_idx + 1}/{len(generated_images)} to database: {db_id}")
2156
+ except Exception as e:
2157
+ print(f"Warning: Failed to save extensive variation {img_idx + 1} to database: {e}")
2158
+
2159
+ # Use first saved ID as the main ad_id
2160
+ if saved_ad_ids:
2161
+ ad_id = saved_ad_ids[0]
2162
 
2163
  all_results.append({
2164
  "id": ad_id,