Skip to content

Commit e6ce244

Browse files
fix: Updates for text-context updation after regenerating image
2 parents 6389049 + b6ed0f4 commit e6ce244

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

content-gen/src/app/frontend/src/App.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,21 @@ function App() {
360360

361361
// Update generatedContent with new image
362362
if (parsedContent.image_url || parsedContent.image_base64) {
363+
// Replace old color/product name in text_content when switching products
364+
const oldName = selectedProducts[0]?.product_name;
365+
const newName = mentionedProduct?.product_name;
366+
const nameRegex = oldName
367+
? new RegExp(oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi')
368+
: undefined;
369+
const swapName = (s?: string) => {
370+
if (!s || !oldName || !newName || oldName === newName || !nameRegex) return s;
371+
return s.replace(nameRegex, () => newName);
372+
};
373+
const tc = generatedContent.text_content;
374+
363375
responseData = {
364376
...generatedContent,
377+
text_content: mentionedProduct ? { ...tc, headline: swapName(tc?.headline), body: swapName(tc?.body), tagline: swapName(tc?.tagline), cta_text: swapName(tc?.cta_text) } : tc,
365378
image_content: {
366379
...generatedContent.image_content,
367380
image_url: parsedContent.image_url || generatedContent.image_content?.image_url,

content-gen/src/backend/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import logging
1111
import os
12+
import re
1213
import uuid
1314
from datetime import datetime, timezone
1415
from typing import Dict, Any
@@ -992,12 +993,28 @@ async def generate():
992993
existing_content = raw_content if isinstance(raw_content, dict) else {}
993994
old_image_url = existing_content.get("image_url")
994995

996+
# Replace old color/product name in text_content when product changes
997+
old_products = existing_content.get("selected_products", [])
998+
old_name = old_products[0].get("product_name", "") if old_products else ""
999+
new_name = products_data[0].get("product_name", "") if products_data else ""
1000+
existing_text = existing_content.get("text_content")
1001+
if existing_text and old_name and new_name and old_name != new_name:
1002+
pat = re.compile(re.escape(old_name), re.IGNORECASE)
1003+
if isinstance(existing_text, dict):
1004+
existing_text = {
1005+
k: pat.sub(lambda _m: new_name, v) if isinstance(v, str) else v
1006+
for k, v in existing_text.items()
1007+
}
1008+
elif isinstance(existing_text, str):
1009+
existing_text = pat.sub(lambda _m: new_name, existing_text)
1010+
9951011
updated_content = {
9961012
**existing_content,
9971013
"image_url": new_image_url if new_image_url else old_image_url,
9981014
"image_prompt": new_image_prompt if new_image_prompt else existing_content.get("image_prompt"),
9991015
"image_revised_prompt": new_image_revised_prompt if new_image_revised_prompt else existing_content.get("image_revised_prompt"),
10001016
"selected_products": products_data if products_data else existing_content.get("selected_products", []),
1017+
**(({"text_content": existing_text} if existing_text is not None else {})),
10011018
}
10021019

10031020
await cosmos_service.save_generated_content(

0 commit comments

Comments
 (0)