Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,26 @@
|
|
| 1 |
-
import re
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from transformers import (
|
| 6 |
-
pipeline,
|
| 7 |
-
AutoProcessor,
|
| 8 |
-
AutoModelForVision2Seq,
|
| 9 |
-
AutoTokenizer,
|
| 10 |
-
AutoModelForSeq2SeqLM,
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# Auto-detect CPU/GPU
|
| 14 |
-
DEVICE = 0 if torch.cuda.is_available() else -1
|
| 15 |
-
|
| 16 |
-
# Load BLIP captioning model
|
| 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(
|
| 20 |
-
task="image-to-text",
|
| 21 |
-
model=blip_model,
|
| 22 |
-
tokenizer=processor.tokenizer,
|
| 23 |
-
image_processor=processor.image_processor,
|
| 24 |
-
device=DEVICE,
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Load Flan-T5 for text-to-text
|
| 28 |
-
FLAN_MODEL = "google/flan-t5-large"
|
| 29 |
-
flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
|
| 30 |
-
flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
|
| 31 |
-
|
| 32 |
-
category_pipe = pipeline(
|
| 33 |
-
"text2text-generation",
|
| 34 |
-
model=flan_model,
|
| 35 |
-
tokenizer=flan_tokenizer,
|
| 36 |
-
device=DEVICE,
|
| 37 |
-
max_new_tokens=32,
|
| 38 |
-
do_sample=True,
|
| 39 |
-
temperature=1.0,
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
analysis_pipe = pipeline(
|
| 43 |
-
"text2text-generation",
|
| 44 |
-
model=flan_model,
|
| 45 |
-
tokenizer=flan_tokenizer,
|
| 46 |
-
device=DEVICE,
|
| 47 |
-
max_new_tokens=256,
|
| 48 |
-
do_sample=True,
|
| 49 |
-
temperature=1.0,
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
suggestion_pipe = pipeline(
|
| 53 |
-
"text2text-generation",
|
| 54 |
-
model=flan_model,
|
| 55 |
-
tokenizer=flan_tokenizer,
|
| 56 |
-
device=DEVICE,
|
| 57 |
-
max_new_tokens=256,
|
| 58 |
-
do_sample=True,
|
| 59 |
-
temperature=1.0,
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
expansion_pipe = pipeline(
|
| 63 |
-
"text2text-generation",
|
| 64 |
-
model=flan_model,
|
| 65 |
-
tokenizer=flan_tokenizer,
|
| 66 |
-
device=DEVICE,
|
| 67 |
-
max_new_tokens=128,
|
| 68 |
-
do_sample=False,
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
def get_recommendations():
|
| 72 |
-
# Returns list of 10 example ad image URLs
|
| 73 |
-
return [
|
| 74 |
-
"https://i.imgur.com/InC88PP.jpeg",
|
| 75 |
-
"https://i.imgur.com/7BHfv4T.png",
|
| 76 |
-
"https://i.imgur.com/wp3Wzc4.jpeg",
|
| 77 |
-
"https://i.imgur.com/5e2xOA4.jpeg",
|
| 78 |
-
"https://i.imgur.com/txjRk98.jpeg",
|
| 79 |
-
"https://i.imgur.com/rQ4AYl0.jpeg",
|
| 80 |
-
"https://i.imgur.com/bDzwD04.jpeg",
|
| 81 |
-
"https://i.imgur.com/fLMngXI.jpeg",
|
| 82 |
-
"https://i.imgur.com/nYEJzxt.png",
|
| 83 |
-
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 84 |
-
]
|
| 85 |
-
|
| 86 |
def process(image: Image):
|
| 87 |
if image is None:
|
| 88 |
return "", "", "", get_recommendations()
|
| 89 |
|
| 90 |
-
# 1
|
| 91 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 92 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 93 |
|
| 94 |
-
# 1a
|
| 95 |
if len(raw_caption.split()) < 3:
|
| 96 |
exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
|
| 97 |
desc = exp[0]["generated_text"].strip()
|
| 98 |
else:
|
| 99 |
desc = raw_caption
|
| 100 |
|
| 101 |
-
# 2
|
| 102 |
cat_prompt = (
|
| 103 |
f"Description: {desc}\n\n"
|
| 104 |
"Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
|
| 105 |
)
|
| 106 |
cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
|
| 107 |
|
| 108 |
-
# 3
|
| 109 |
ana_prompt = (
|
| 110 |
f"Description: {desc}\n\n"
|
| 111 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
|
@@ -114,10 +29,10 @@ def process(image: Image):
|
|
| 114 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 115 |
analysis = " ".join(sentences[:5])
|
| 116 |
|
| 117 |
-
# 4
|
| 118 |
sug_prompt = (
|
| 119 |
f"Description: {desc}\n\n"
|
| 120 |
-
"Suggest five unique, practical improvements for this ad. Each must address a different aspect (message, visuals,
|
| 121 |
)
|
| 122 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 123 |
bullets = []
|
|
@@ -136,7 +51,7 @@ def process(image: Image):
|
|
| 136 |
seen.add(suggestion)
|
| 137 |
if len(bullets) == 5:
|
| 138 |
break
|
| 139 |
-
# Add
|
| 140 |
defaults = [
|
| 141 |
"- Make the main headline more eye-catching.",
|
| 142 |
"- Add a clear and visible call-to-action button.",
|
|
@@ -150,39 +65,3 @@ def process(image: Image):
|
|
| 150 |
suggestions = "\n".join(bullets[:5])
|
| 151 |
|
| 152 |
return cat_out, analysis, suggestions, get_recommendations()
|
| 153 |
-
|
| 154 |
-
def main():
|
| 155 |
-
with gr.Blocks(title="Smart Ad Analyzer") as demo:
|
| 156 |
-
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 157 |
-
gr.Markdown(
|
| 158 |
-
"""
|
| 159 |
-
**Upload your ad image below and instantly get expert feedback.**
|
| 160 |
-
|
| 161 |
-
This AI tool will analyze your ad and provide:
|
| 162 |
-
- 📂 **Category** — What type of ad is this?
|
| 163 |
-
- 📊 **In-depth Analysis** — Five detailed sentences covering message, visuals, emotional impact, and more.
|
| 164 |
-
- 🚀 **Improvement Suggestions** — Five actionable, unique ways to make your ad better.
|
| 165 |
-
- 📸 **Inspiration Gallery** — See other effective ads for ideas.
|
| 166 |
-
|
| 167 |
-
Perfect for marketers, founders, designers, and anyone looking to boost ad performance with actionable insights!
|
| 168 |
-
"""
|
| 169 |
-
)
|
| 170 |
-
with gr.Row():
|
| 171 |
-
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 172 |
-
with gr.Column():
|
| 173 |
-
cat_out = gr.Textbox(label='📂 Ad Category', interactive=False)
|
| 174 |
-
ana_out = gr.Textbox(label='📊 Ad Analysis', lines=5, interactive=False)
|
| 175 |
-
sug_out = gr.Textbox(label='🚀 Improvement Suggestions', lines=5, interactive=False)
|
| 176 |
-
btn = gr.Button('Analyze Ad', variant='primary')
|
| 177 |
-
gallery = gr.Gallery(label='Example Ads')
|
| 178 |
-
btn.click(
|
| 179 |
-
fn=process,
|
| 180 |
-
inputs=[inp],
|
| 181 |
-
outputs=[cat_out, ana_out, sug_out, gallery],
|
| 182 |
-
)
|
| 183 |
-
gr.Markdown('Made by Simon Thalmay')
|
| 184 |
-
return demo
|
| 185 |
-
|
| 186 |
-
if __name__ == "__main__":
|
| 187 |
-
demo = main()
|
| 188 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def process(image: Image):
|
| 2 |
if image is None:
|
| 3 |
return "", "", "", get_recommendations()
|
| 4 |
|
| 5 |
+
# 1) BLIP caption
|
| 6 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 7 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 8 |
|
| 9 |
+
# 1a) Expand caption if too short
|
| 10 |
if len(raw_caption.split()) < 3:
|
| 11 |
exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
|
| 12 |
desc = exp[0]["generated_text"].strip()
|
| 13 |
else:
|
| 14 |
desc = raw_caption
|
| 15 |
|
| 16 |
+
# 2) Category
|
| 17 |
cat_prompt = (
|
| 18 |
f"Description: {desc}\n\n"
|
| 19 |
"Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
|
| 20 |
)
|
| 21 |
cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
|
| 22 |
|
| 23 |
+
# 3) Five-sentence analysis
|
| 24 |
ana_prompt = (
|
| 25 |
f"Description: {desc}\n\n"
|
| 26 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
|
|
|
| 29 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 30 |
analysis = " ".join(sentences[:5])
|
| 31 |
|
| 32 |
+
# 4) Five bullet-point suggestions, filter for unique & not empty
|
| 33 |
sug_prompt = (
|
| 34 |
f"Description: {desc}\n\n"
|
| 35 |
+
"Suggest five unique, practical improvements for this ad. Each must address a different aspect (such as message, visuals, CTA, targeting, layout, or design). Each suggestion must be only one sentence and start with '- '. Do NOT repeat suggestions."
|
| 36 |
)
|
| 37 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 38 |
bullets = []
|
|
|
|
| 51 |
seen.add(suggestion)
|
| 52 |
if len(bullets) == 5:
|
| 53 |
break
|
| 54 |
+
# Add defaults if needed
|
| 55 |
defaults = [
|
| 56 |
"- Make the main headline more eye-catching.",
|
| 57 |
"- Add a clear and visible call-to-action button.",
|
|
|
|
| 65 |
suggestions = "\n".join(bullets[:5])
|
| 66 |
|
| 67 |
return cat_out, analysis, suggestions, get_recommendations()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|