Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,7 +49,7 @@ analysis_pipe = pipeline(
|
|
| 49 |
temperature=1.0,
|
| 50 |
)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
suggestion_pipe = pipeline(
|
| 54 |
"text2text-generation",
|
| 55 |
model=flan_model,
|
|
@@ -57,7 +57,7 @@ suggestion_pipe = pipeline(
|
|
| 57 |
device=DEVICE,
|
| 58 |
max_new_tokens=256,
|
| 59 |
do_sample=True,
|
| 60 |
-
temperature=1.
|
| 61 |
)
|
| 62 |
|
| 63 |
expansion_pipe = pipeline(
|
|
@@ -70,7 +70,6 @@ expansion_pipe = pipeline(
|
|
| 70 |
)
|
| 71 |
|
| 72 |
def get_recommendations():
|
| 73 |
-
# Returns list of 10 example ad image URLs
|
| 74 |
return [
|
| 75 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 76 |
"https://i.imgur.com/7BHfv4T.png",
|
|
@@ -88,38 +87,29 @@ def process(image: Image):
|
|
| 88 |
if image is None:
|
| 89 |
return "", "", "", get_recommendations()
|
| 90 |
|
| 91 |
-
#
|
| 92 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 93 |
raw_caption = caption_res[0]["generated_text"].strip()
|
|
|
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
|
| 98 |
-
desc = exp[0]["generated_text"].strip()
|
| 99 |
-
else:
|
| 100 |
-
desc = raw_caption
|
| 101 |
-
|
| 102 |
-
# 2. Category
|
| 103 |
-
cat_prompt = (
|
| 104 |
-
f"Description: {desc}\n\n"
|
| 105 |
-
"Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
|
| 106 |
-
)
|
| 107 |
cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
|
| 108 |
|
| 109 |
-
#
|
| 110 |
ana_prompt = (
|
| 111 |
f"Description: {desc}\n\n"
|
| 112 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 113 |
)
|
| 114 |
ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
|
| 115 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 116 |
-
analysis = " ".join(sentences[:5])
|
| 117 |
|
| 118 |
-
#
|
| 119 |
sug_prompt = (
|
| 120 |
-
f"
|
| 121 |
-
"
|
| 122 |
-
"Each suggestion
|
| 123 |
)
|
| 124 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 125 |
all_sugs = [line.strip() for line in sug_raw.splitlines() if line.strip().startswith("-")]
|
|
@@ -133,7 +123,7 @@ def process(image: Image):
|
|
| 133 |
if len(unique_sugs) == 5:
|
| 134 |
break
|
| 135 |
|
| 136 |
-
# Add
|
| 137 |
defaults = [
|
| 138 |
"- Make the main headline more eye-catching.",
|
| 139 |
"- Add a clear and visible call-to-action button.",
|
|
|
|
| 49 |
temperature=1.0,
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# Higher temp for more variety in suggestions
|
| 53 |
suggestion_pipe = pipeline(
|
| 54 |
"text2text-generation",
|
| 55 |
model=flan_model,
|
|
|
|
| 57 |
device=DEVICE,
|
| 58 |
max_new_tokens=256,
|
| 59 |
do_sample=True,
|
| 60 |
+
temperature=1.3,
|
| 61 |
)
|
| 62 |
|
| 63 |
expansion_pipe = pipeline(
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
def get_recommendations():
|
|
|
|
| 73 |
return [
|
| 74 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 75 |
"https://i.imgur.com/7BHfv4T.png",
|
|
|
|
| 87 |
if image is None:
|
| 88 |
return "", "", "", get_recommendations()
|
| 89 |
|
| 90 |
+
# BLIP caption
|
| 91 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 92 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 93 |
+
desc = raw_caption if len(raw_caption.split()) >= 3 else expansion_pipe(f"Expand into a detailed description: {raw_caption}")[0]["generated_text"].strip()
|
| 94 |
|
| 95 |
+
# Category
|
| 96 |
+
cat_prompt = f"Description: {desc}\n\nProvide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
|
| 98 |
|
| 99 |
+
# Five-sentence analysis
|
| 100 |
ana_prompt = (
|
| 101 |
f"Description: {desc}\n\n"
|
| 102 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 103 |
)
|
| 104 |
ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
|
| 105 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 106 |
+
analysis = " ".join(sentences[:5]).strip()
|
| 107 |
|
| 108 |
+
# **KEY CHANGE**: Use analysis in suggestion prompt!
|
| 109 |
sug_prompt = (
|
| 110 |
+
f"Ad description: {desc}\n"
|
| 111 |
+
f"Ad analysis: {analysis}\n\n"
|
| 112 |
+
"Based on this, suggest five unique and specific improvements for this ad. Each suggestion should be one clear sentence starting with '- ' and focus on a different aspect, like message, visuals, call-to-action, color, clarity, layout, targeting, or emotional impact. Do NOT repeat suggestions."
|
| 113 |
)
|
| 114 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 115 |
all_sugs = [line.strip() for line in sug_raw.splitlines() if line.strip().startswith("-")]
|
|
|
|
| 123 |
if len(unique_sugs) == 5:
|
| 124 |
break
|
| 125 |
|
| 126 |
+
# Add defaults if needed
|
| 127 |
defaults = [
|
| 128 |
"- Make the main headline more eye-catching.",
|
| 129 |
"- Add a clear and visible call-to-action button.",
|