Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,10 +10,10 @@ from transformers import (
|
|
| 10 |
AutoModelForSeq2SeqLM,
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
DEVICE = 0 if torch.cuda.is_available() else -1
|
| 15 |
|
| 16 |
-
# BLIP
|
| 17 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 18 |
blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 19 |
caption_pipe = pipeline(
|
|
@@ -68,6 +68,7 @@ expansion_pipe = pipeline(
|
|
| 68 |
do_sample=False,
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
def get_recommendations():
|
| 72 |
return [
|
| 73 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
@@ -82,29 +83,30 @@ def get_recommendations():
|
|
| 82 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 83 |
]
|
| 84 |
|
|
|
|
| 85 |
def process(image: Image):
|
| 86 |
if image is None:
|
| 87 |
-
return "", "", "",
|
| 88 |
|
| 89 |
-
# BLIP caption
|
| 90 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 91 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 92 |
|
| 93 |
-
# Expand if too short
|
| 94 |
if len(raw_caption.split()) < 3:
|
| 95 |
exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
|
| 96 |
desc = exp[0]["generated_text"].strip()
|
| 97 |
else:
|
| 98 |
desc = raw_caption
|
| 99 |
|
| 100 |
-
# Category
|
| 101 |
cat_prompt = (
|
| 102 |
f"Description: {desc}\n\n"
|
| 103 |
"Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
|
| 104 |
)
|
| 105 |
cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
|
| 106 |
|
| 107 |
-
# Five-sentence analysis
|
| 108 |
ana_prompt = (
|
| 109 |
f"Description: {desc}\n\n"
|
| 110 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
|
@@ -113,67 +115,58 @@ def process(image: Image):
|
|
| 113 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 114 |
analysis = " ".join(sentences[:5])
|
| 115 |
|
| 116 |
-
# Five bullet-point suggestions
|
| 117 |
sug_prompt = (
|
| 118 |
f"Description: {desc}\n\n"
|
| 119 |
-
"
|
| 120 |
)
|
| 121 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
|
|
|
| 122 |
bullets = []
|
| 123 |
seen = set()
|
| 124 |
-
for
|
| 125 |
-
line = l.strip()
|
| 126 |
if line.startswith("-"):
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
"-
|
| 139 |
-
"- Highlight product benefits more clearly in the headline."
|
| 140 |
-
]
|
| 141 |
-
for fb in fallback:
|
| 142 |
-
if len(bullets) == 5:
|
| 143 |
-
break
|
| 144 |
-
fb_key = fb[2:].lower()
|
| 145 |
-
if fb_key not in seen:
|
| 146 |
-
bullets.append(fb)
|
| 147 |
-
seen.add(fb_key)
|
| 148 |
suggestions = "\n".join(bullets[:5])
|
| 149 |
|
| 150 |
-
return
|
| 151 |
|
|
|
|
| 152 |
def main():
|
| 153 |
with gr.Blocks(title="Smart Ad Analyzer") as demo:
|
|
|
|
| 154 |
gr.Markdown(
|
| 155 |
-
"
|
| 156 |
-
"
|
| 157 |
-
"- **Ad Category:**
|
| 158 |
-
"- **
|
| 159 |
-
"- **Improvement Suggestions:** Five
|
| 160 |
-
"- **Example
|
| 161 |
-
"
|
| 162 |
)
|
| 163 |
with gr.Row():
|
| 164 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 165 |
with gr.Column():
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
ana_out = gr.Textbox(label=' Ad Analysis', lines=5, interactive=False)
|
| 170 |
-
sug_out = gr.Textbox(label=' Improvement Suggestions', lines=5, interactive=False)
|
| 171 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 172 |
gallery = gr.Gallery(label='Example Ads')
|
| 173 |
btn.click(
|
| 174 |
fn=process,
|
| 175 |
inputs=[inp],
|
| 176 |
-
outputs=[
|
| 177 |
)
|
| 178 |
gr.Markdown('Made by Simon Thalmay')
|
| 179 |
return demo
|
|
|
|
| 10 |
AutoModelForSeq2SeqLM,
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Device config
|
| 14 |
DEVICE = 0 if torch.cuda.is_available() else -1
|
| 15 |
|
| 16 |
+
# BLIP image captioning
|
| 17 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 18 |
blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 19 |
caption_pipe = pipeline(
|
|
|
|
| 68 |
do_sample=False,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Example gallery helper
|
| 72 |
def get_recommendations():
|
| 73 |
return [
|
| 74 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
|
|
| 83 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 84 |
]
|
| 85 |
|
| 86 |
+
# Main processing function
|
| 87 |
def process(image: Image):
|
| 88 |
if image is None:
|
| 89 |
+
return "", "", "", get_recommendations()
|
| 90 |
|
| 91 |
+
# 1) BLIP caption
|
| 92 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 93 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 94 |
|
| 95 |
+
# 1a) Expand caption if too short
|
| 96 |
if len(raw_caption.split()) < 3:
|
| 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 |
+
# 3) Five-sentence analysis
|
| 110 |
ana_prompt = (
|
| 111 |
f"Description: {desc}\n\n"
|
| 112 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
|
|
|
| 115 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 116 |
analysis = " ".join(sentences[:5])
|
| 117 |
|
| 118 |
+
# 4) Five bullet-point suggestions, deduplicate
|
| 119 |
sug_prompt = (
|
| 120 |
f"Description: {desc}\n\n"
|
| 121 |
+
"Suggest five unique, practical improvements for this ad, each starting with '- '. Each must address a different aspect (such as message, visuals, CTA, targeting, layout, or design). Avoid repeating the same suggestion."
|
| 122 |
)
|
| 123 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 124 |
+
# Only keep unique, non-empty suggestions
|
| 125 |
bullets = []
|
| 126 |
seen = set()
|
| 127 |
+
for line in sug_raw.splitlines():
|
|
|
|
| 128 |
if line.startswith("-"):
|
| 129 |
+
item = line.strip()
|
| 130 |
+
# Remove exact duplicates
|
| 131 |
+
if item not in seen and len(bullets) < 5:
|
| 132 |
+
bullets.append(item)
|
| 133 |
+
seen.add(item)
|
| 134 |
+
elif line.strip() and len(bullets) < 5:
|
| 135 |
+
item = "- " + line.strip()
|
| 136 |
+
if item not in seen:
|
| 137 |
+
bullets.append(item)
|
| 138 |
+
seen.add(item)
|
| 139 |
+
while len(bullets) < 5:
|
| 140 |
+
bullets.append(f"- Add a new visual or messaging element for more impact.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
suggestions = "\n".join(bullets[:5])
|
| 142 |
|
| 143 |
+
return cat_out, analysis, suggestions, get_recommendations()
|
| 144 |
|
| 145 |
+
# Gradio UI
|
| 146 |
def main():
|
| 147 |
with gr.Blocks(title="Smart Ad Analyzer") as demo:
|
| 148 |
+
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 149 |
gr.Markdown(
|
| 150 |
+
"Welcome to the Smart Ad Analyzer! Upload any advertisement image to receive instant, AI-powered marketing feedback.\n\n"
|
| 151 |
+
"What you'll get:\n"
|
| 152 |
+
"- **Ad Category:** What type of product or service is advertised?\n"
|
| 153 |
+
"- **Detailed Analysis:** A five-sentence summary explaining the ad's message, design, and emotional effect.\n"
|
| 154 |
+
"- **Improvement Suggestions:** Five unique, actionable tips to boost ad performance (not just generic advice!).\n"
|
| 155 |
+
"- **Example Gallery:** See other effective ad examples for inspiration.\n\n"
|
| 156 |
+
"Ideal for marketers, entrepreneurs, students, or anyone curious about what makes a great ad!"
|
| 157 |
)
|
| 158 |
with gr.Row():
|
| 159 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 160 |
with gr.Column():
|
| 161 |
+
cat_out = gr.Textbox(label='📂 Ad Category', interactive=False)
|
| 162 |
+
ana_out = gr.Textbox(label='📊 Ad Analysis', lines=5, interactive=False)
|
| 163 |
+
sug_out = gr.Textbox(label='🚀 Improvement Suggestions', lines=5, interactive=False)
|
|
|
|
|
|
|
| 164 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 165 |
gallery = gr.Gallery(label='Example Ads')
|
| 166 |
btn.click(
|
| 167 |
fn=process,
|
| 168 |
inputs=[inp],
|
| 169 |
+
outputs=[cat_out, ana_out, sug_out, gallery],
|
| 170 |
)
|
| 171 |
gr.Markdown('Made by Simon Thalmay')
|
| 172 |
return demo
|