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(
|
|
@@ -24,7 +24,7 @@ caption_pipe = pipeline(
|
|
| 24 |
device=DEVICE,
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
FLAN_MODEL = "google/flan-t5-large"
|
| 29 |
flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
|
| 30 |
flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
|
|
@@ -68,8 +68,8 @@ expansion_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",
|
| 75 |
"https://i.imgur.com/7BHfv4T.png",
|
|
@@ -83,30 +83,29 @@ def get_recommendations():
|
|
| 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
|
| 92 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 93 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 94 |
|
| 95 |
-
# 1a
|
| 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
|
| 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
|
| 110 |
ana_prompt = (
|
| 111 |
f"Description: {desc}\n\n"
|
| 112 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
|
@@ -115,45 +114,58 @@ def process(image: Image):
|
|
| 115 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 116 |
analysis = " ".join(sentences[:5])
|
| 117 |
|
| 118 |
-
# 4
|
| 119 |
sug_prompt = (
|
| 120 |
f"Description: {desc}\n\n"
|
| 121 |
-
"Suggest five unique, practical improvements for this ad
|
| 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 |
-
|
| 130 |
-
# Remove
|
| 131 |
-
if
|
| 132 |
-
bullets.append(
|
| 133 |
-
seen.add(
|
| 134 |
-
elif line.strip()
|
| 135 |
-
|
| 136 |
-
if
|
| 137 |
-
bullets.append(
|
| 138 |
-
seen.add(
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
| 158 |
with gr.Row():
|
| 159 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
|
|
|
| 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(
|
|
|
|
| 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)
|
|
|
|
| 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",
|
|
|
|
| 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. BLIP caption
|
| 91 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 92 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 93 |
|
| 94 |
+
# 1a. Expand caption if too short
|
| 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. Category
|
| 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. Five-sentence analysis
|
| 109 |
ana_prompt = (
|
| 110 |
f"Description: {desc}\n\n"
|
| 111 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
|
|
|
| 114 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 115 |
analysis = " ".join(sentences[:5])
|
| 116 |
|
| 117 |
+
# 4. Five bullet-point suggestions (unique only)
|
| 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, call-to-action, targeting, layout, or design). Each suggestion must be one sentence and start with '- '. Do NOT repeat suggestions."
|
| 121 |
)
|
| 122 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
|
|
|
| 123 |
bullets = []
|
| 124 |
seen = set()
|
| 125 |
for line in sug_raw.splitlines():
|
| 126 |
if line.startswith("-"):
|
| 127 |
+
suggestion = line.strip()
|
| 128 |
+
# Remove duplicates and ignore empty lines
|
| 129 |
+
if suggestion and suggestion not in seen:
|
| 130 |
+
bullets.append(suggestion)
|
| 131 |
+
seen.add(suggestion)
|
| 132 |
+
elif line.strip():
|
| 133 |
+
suggestion = "- " + line.strip()
|
| 134 |
+
if suggestion and suggestion not in seen:
|
| 135 |
+
bullets.append(suggestion)
|
| 136 |
+
seen.add(suggestion)
|
| 137 |
+
if len(bullets) == 5:
|
| 138 |
+
break
|
| 139 |
+
# Add non-repetitive defaults if needed
|
| 140 |
+
defaults = [
|
| 141 |
+
"- Make the main headline more eye-catching.",
|
| 142 |
+
"- Add a clear and visible call-to-action button.",
|
| 143 |
+
"- Use contrasting colors for better readability.",
|
| 144 |
+
"- Highlight the unique selling point of the product.",
|
| 145 |
+
"- Simplify the design to reduce clutter."
|
| 146 |
+
]
|
| 147 |
+
for default in defaults:
|
| 148 |
+
if len(bullets) < 5 and default not in seen:
|
| 149 |
+
bullets.append(default)
|
| 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')
|