Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from transformers import (
|
|
| 12 |
|
| 13 |
DEVICE = 0 if torch.cuda.is_available() else -1
|
| 14 |
|
| 15 |
-
# BLIP captioner
|
| 16 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 17 |
blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 18 |
caption_pipe = pipeline(
|
|
@@ -23,7 +23,7 @@ caption_pipe = pipeline(
|
|
| 23 |
device=DEVICE,
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# FLAN-T5
|
| 27 |
FLAN_MODEL = "google/flan-t5-large"
|
| 28 |
flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
|
| 29 |
flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
|
|
@@ -37,6 +37,7 @@ category_pipe = pipeline(
|
|
| 37 |
do_sample=True,
|
| 38 |
temperature=1.0,
|
| 39 |
)
|
|
|
|
| 40 |
analysis_pipe = pipeline(
|
| 41 |
"text2text-generation",
|
| 42 |
model=flan_model,
|
|
@@ -46,6 +47,7 @@ analysis_pipe = pipeline(
|
|
| 46 |
do_sample=True,
|
| 47 |
temperature=1.0,
|
| 48 |
)
|
|
|
|
| 49 |
suggestion_pipe = pipeline(
|
| 50 |
"text2text-generation",
|
| 51 |
model=flan_model,
|
|
@@ -55,6 +57,7 @@ suggestion_pipe = pipeline(
|
|
| 55 |
do_sample=True,
|
| 56 |
temperature=1.0,
|
| 57 |
)
|
|
|
|
| 58 |
expansion_pipe = pipeline(
|
| 59 |
"text2text-generation",
|
| 60 |
model=flan_model,
|
|
@@ -82,70 +85,86 @@ def process(image: Image):
|
|
| 82 |
if image is None:
|
| 83 |
return "", "", "", get_recommendations()
|
| 84 |
|
| 85 |
-
#
|
| 86 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 87 |
raw_caption = caption_res[0]["generated_text"].strip()
|
|
|
|
|
|
|
| 88 |
if len(raw_caption.split()) < 3:
|
| 89 |
-
|
|
|
|
| 90 |
else:
|
| 91 |
desc = raw_caption
|
| 92 |
|
| 93 |
-
#
|
| 94 |
cat_prompt = (
|
| 95 |
f"Description: {desc}\n\n"
|
| 96 |
-
"Provide a concise
|
| 97 |
)
|
| 98 |
cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
|
| 99 |
|
| 100 |
-
#
|
| 101 |
ana_prompt = (
|
| 102 |
f"Description: {desc}\n\n"
|
| 103 |
-
"Write exactly five
|
| 104 |
)
|
| 105 |
ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
|
| 106 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 107 |
analysis = " ".join(sentences[:5])
|
| 108 |
|
| 109 |
-
#
|
| 110 |
sug_prompt = (
|
| 111 |
f"Description: {desc}\n\n"
|
| 112 |
-
"
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
for
|
| 119 |
-
|
| 120 |
-
if
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
return cat_out, analysis, suggestions, get_recommendations()
|
| 129 |
|
| 130 |
def main():
|
| 131 |
with gr.Blocks(title="Smart Ad Analyzer") as demo:
|
| 132 |
-
gr.Markdown("
|
| 133 |
gr.Markdown(
|
| 134 |
-
"
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
)
|
| 142 |
with gr.Row():
|
| 143 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 144 |
with gr.Column():
|
| 145 |
-
# BLIP caption hidden from UI
|
| 146 |
cat_out = gr.Textbox(label='π Ad Category', interactive=False)
|
| 147 |
ana_out = gr.Textbox(label='π Ad Analysis', lines=5, interactive=False)
|
| 148 |
-
sug_out = gr.Textbox(label='π Improvement Suggestions', lines=
|
| 149 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 150 |
gallery = gr.Gallery(label='Example Ads')
|
| 151 |
btn.click(
|
|
|
|
| 12 |
|
| 13 |
DEVICE = 0 if torch.cuda.is_available() else -1
|
| 14 |
|
| 15 |
+
# BLIP captioner
|
| 16 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 17 |
blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 18 |
caption_pipe = pipeline(
|
|
|
|
| 23 |
device=DEVICE,
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# FLAN-T5
|
| 27 |
FLAN_MODEL = "google/flan-t5-large"
|
| 28 |
flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
|
| 29 |
flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
|
|
|
|
| 37 |
do_sample=True,
|
| 38 |
temperature=1.0,
|
| 39 |
)
|
| 40 |
+
|
| 41 |
analysis_pipe = pipeline(
|
| 42 |
"text2text-generation",
|
| 43 |
model=flan_model,
|
|
|
|
| 47 |
do_sample=True,
|
| 48 |
temperature=1.0,
|
| 49 |
)
|
| 50 |
+
|
| 51 |
suggestion_pipe = pipeline(
|
| 52 |
"text2text-generation",
|
| 53 |
model=flan_model,
|
|
|
|
| 57 |
do_sample=True,
|
| 58 |
temperature=1.0,
|
| 59 |
)
|
| 60 |
+
|
| 61 |
expansion_pipe = pipeline(
|
| 62 |
"text2text-generation",
|
| 63 |
model=flan_model,
|
|
|
|
| 85 |
if image is None:
|
| 86 |
return "", "", "", get_recommendations()
|
| 87 |
|
| 88 |
+
# BLIP caption
|
| 89 |
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 90 |
raw_caption = caption_res[0]["generated_text"].strip()
|
| 91 |
+
|
| 92 |
+
# Expand if too short
|
| 93 |
if len(raw_caption.split()) < 3:
|
| 94 |
+
exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
|
| 95 |
+
desc = exp[0]["generated_text"].strip()
|
| 96 |
else:
|
| 97 |
desc = raw_caption
|
| 98 |
|
| 99 |
+
# Category
|
| 100 |
cat_prompt = (
|
| 101 |
f"Description: {desc}\n\n"
|
| 102 |
+
"Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
|
| 103 |
)
|
| 104 |
cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
|
| 105 |
|
| 106 |
+
# Five-sentence analysis
|
| 107 |
ana_prompt = (
|
| 108 |
f"Description: {desc}\n\n"
|
| 109 |
+
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 110 |
)
|
| 111 |
ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
|
| 112 |
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 113 |
analysis = " ".join(sentences[:5])
|
| 114 |
|
| 115 |
+
# Five bullet-point suggestions, improved filtering
|
| 116 |
sug_prompt = (
|
| 117 |
f"Description: {desc}\n\n"
|
| 118 |
+
"Provide five unique, actionable improvement suggestions for this ad. "
|
| 119 |
+
"Each suggestion must begin with '- ' and each one should cover a different topic, such as clarity, visual design, call-to-action, audience targeting, or emotional impact. "
|
| 120 |
+
"Do not repeat ideas or use generic language. Each suggestion should be specific and creative."
|
| 121 |
)
|
| 122 |
sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 123 |
+
lines = [l.strip() for l in sug_raw.splitlines() if l.strip().startswith("-")]
|
| 124 |
+
used = set()
|
| 125 |
+
unique_lines = []
|
| 126 |
+
for l in lines:
|
| 127 |
+
key = l.lower().strip("- .")
|
| 128 |
+
if key not in used and len(unique_lines) < 5:
|
| 129 |
+
unique_lines.append(l)
|
| 130 |
+
used.add(key)
|
| 131 |
+
fallbacks = [
|
| 132 |
+
"- Use a bolder headline or color contrast to capture attention.",
|
| 133 |
+
"- Make the product or offer more visually prominent.",
|
| 134 |
+
"- Add a clearer call to action, such as 'Order Now' or 'Learn More'.",
|
| 135 |
+
"- Tailor the messaging to a more specific audience segment.",
|
| 136 |
+
"- Add emotional or humorous elements to boost engagement."
|
| 137 |
+
]
|
| 138 |
+
for fb in fallbacks:
|
| 139 |
+
if len(unique_lines) >= 5:
|
| 140 |
+
break
|
| 141 |
+
if fb.lower() not in used:
|
| 142 |
+
unique_lines.append(fb)
|
| 143 |
+
used.add(fb.lower())
|
| 144 |
+
suggestions = "\n".join(unique_lines[:5])
|
| 145 |
|
| 146 |
return cat_out, analysis, suggestions, get_recommendations()
|
| 147 |
|
| 148 |
def main():
|
| 149 |
with gr.Blocks(title="Smart Ad Analyzer") as demo:
|
| 150 |
+
gr.Markdown("# π’ Smart Ad Analyzer")
|
| 151 |
gr.Markdown(
|
| 152 |
+
"""
|
| 153 |
+
Upload an ad image to instantly get:
|
| 154 |
+
- **Ad Category**: A clear and relevant label for the type of ad.
|
| 155 |
+
- **Five-sentence Analysis**: A comprehensive explanation of the ad's message, design, and emotional impact.
|
| 156 |
+
- **Five Improvement Suggestions**: Specific, unique tips to boost ad performance.
|
| 157 |
+
- **Example Ads**: Inspiring ad visuals for creative reference.
|
| 158 |
+
|
| 159 |
+
This tool helps marketers, designers, and founders strengthen any ad campaign in seconds.
|
| 160 |
+
"""
|
| 161 |
)
|
| 162 |
with gr.Row():
|
| 163 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 164 |
with gr.Column():
|
|
|
|
| 165 |
cat_out = gr.Textbox(label='π Ad Category', interactive=False)
|
| 166 |
ana_out = gr.Textbox(label='π Ad Analysis', lines=5, interactive=False)
|
| 167 |
+
sug_out = gr.Textbox(label='π Improvement Suggestions', lines=8, interactive=False)
|
| 168 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 169 |
gallery = gr.Gallery(label='Example Ads')
|
| 170 |
btn.click(
|