Commit ·
4928a87
1
Parent(s): 29066e0
variation in gallery
Browse files- 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 (
|
| 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 |
-
|
| 1548 |
-
|
| 1549 |
-
|
| 1550 |
-
|
| 1551 |
-
|
| 1552 |
-
|
| 1553 |
-
|
| 1554 |
-
|
| 1555 |
-
|
| 1556 |
-
|
| 1557 |
-
|
| 1558 |
-
|
| 1559 |
-
|
| 1560 |
-
|
| 1561 |
-
|
| 1562 |
-
|
| 1563 |
-
|
| 1564 |
-
|
| 1565 |
-
|
| 1566 |
-
|
| 1567 |
-
|
| 1568 |
-
|
| 1569 |
-
|
| 1570 |
-
|
| 1571 |
-
|
| 1572 |
-
|
| 1573 |
-
|
| 1574 |
-
|
| 1575 |
-
|
| 1576 |
-
|
| 1577 |
-
|
| 1578 |
-
|
| 1579 |
-
|
| 1580 |
-
|
| 1581 |
-
|
| 1582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 (
|
| 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 |
-
|
| 1803 |
-
|
| 1804 |
-
|
| 1805 |
-
|
| 1806 |
-
|
| 1807 |
-
|
| 1808 |
-
|
| 1809 |
-
|
| 1810 |
-
|
| 1811 |
-
|
| 1812 |
-
|
| 1813 |
-
|
| 1814 |
-
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
|
| 1820 |
-
|
| 1821 |
-
|
| 1822 |
-
|
| 1823 |
-
|
| 1824 |
-
|
| 1825 |
-
|
| 1826 |
-
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
-
|
| 1831 |
-
|
| 1832 |
-
|
| 1833 |
-
|
| 1834 |
-
|
| 1835 |
-
|
| 1836 |
-
|
| 1837 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 2105 |
|
| 2106 |
if db_service and username:
|
| 2107 |
-
|
| 2108 |
-
|
| 2109 |
-
|
| 2110 |
-
|
| 2111 |
-
|
| 2112 |
-
|
| 2113 |
-
|
| 2114 |
-
|
| 2115 |
-
|
| 2116 |
-
|
| 2117 |
-
|
| 2118 |
-
|
| 2119 |
-
|
| 2120 |
-
|
| 2121 |
-
|
| 2122 |
-
|
| 2123 |
-
|
| 2124 |
-
|
| 2125 |
-
"
|
| 2126 |
-
"
|
| 2127 |
-
|
| 2128 |
-
|
| 2129 |
-
|
| 2130 |
-
|
| 2131 |
-
|
| 2132 |
-
|
| 2133 |
-
|
| 2134 |
-
|
| 2135 |
-
|
| 2136 |
-
|
| 2137 |
-
|
| 2138 |
-
|
| 2139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|