Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,39 @@
|
|
| 1 |
-
import logging
|
| 2 |
import re
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
-
from transformers import
|
| 6 |
-
BlipProcessor,
|
| 7 |
-
BlipForConditionalGeneration,
|
| 8 |
-
pipeline,
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
# Set up logging
|
| 12 |
-
logging.basicConfig(level=logging.INFO)
|
| 13 |
-
|
| 14 |
-
# 1) BLIP captioner (large model for richer captions)
|
| 15 |
-
caption_processor = BlipProcessor.from_pretrained(
|
| 16 |
-
"Salesforce/blip-image-captioning-large",
|
| 17 |
-
use_fast=False
|
| 18 |
-
)
|
| 19 |
-
caption_model = BlipForConditionalGeneration.from_pretrained(
|
| 20 |
-
"Salesforce/blip-image-captioning-large"
|
| 21 |
-
)
|
| 22 |
|
|
|
|
| 23 |
caption_pipe = pipeline(
|
| 24 |
task="image-to-text",
|
| 25 |
-
model=
|
| 26 |
-
|
| 27 |
-
device=-1,
|
| 28 |
max_length=64,
|
| 29 |
do_sample=False,
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# 2) Flan-T5
|
| 33 |
-
|
| 34 |
category_pipe = pipeline(
|
| 35 |
"text2text-generation",
|
| 36 |
-
model=
|
| 37 |
-
tokenizer=
|
| 38 |
max_new_tokens=32,
|
| 39 |
do_sample=True,
|
| 40 |
temperature=1.0,
|
| 41 |
)
|
| 42 |
analysis_pipe = pipeline(
|
| 43 |
"text2text-generation",
|
| 44 |
-
model=
|
| 45 |
-
tokenizer=
|
| 46 |
max_new_tokens=256,
|
| 47 |
do_sample=True,
|
| 48 |
temperature=1.0,
|
| 49 |
)
|
| 50 |
suggestion_pipe = pipeline(
|
| 51 |
"text2text-generation",
|
| 52 |
-
model=
|
| 53 |
-
tokenizer=
|
| 54 |
max_new_tokens=256,
|
| 55 |
do_sample=True,
|
| 56 |
temperature=1.0,
|
|
@@ -63,79 +46,74 @@ def get_recommendations():
|
|
| 63 |
"https://i.imgur.com/wp3Wzc4.jpeg",
|
| 64 |
"https://i.imgur.com/5e2xOA4.jpeg",
|
| 65 |
"https://i.imgur.com/txjRk98.jpeg",
|
| 66 |
-
"https://i.imgur.com/rQ4AYl0.jpeg",
|
| 67 |
-
"https://i.imgur.com/bDzwD04.jpeg",
|
| 68 |
-
"https://i.imgur.com/fLMngXI.jpeg",
|
| 69 |
-
"https://i.imgur.com/nYEJzxt.png",
|
| 70 |
-
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 71 |
]
|
| 72 |
|
| 73 |
def process(image: Image):
|
| 74 |
-
#
|
| 75 |
caption = caption_pipe(image)[0]["generated_text"].strip()
|
| 76 |
-
logging.info(f"RAW CAPTION: {caption}")
|
| 77 |
|
| 78 |
-
#
|
| 79 |
cat_prompt = (
|
| 80 |
-
f"Caption: {caption}\n"
|
| 81 |
-
"
|
| 82 |
)
|
| 83 |
-
|
| 84 |
-
category = raw_cat.splitlines()[0]
|
| 85 |
-
logging.info(f"RAW CATEGORY: {raw_cat}")
|
| 86 |
|
| 87 |
-
#
|
| 88 |
ana_prompt = (
|
| 89 |
-
f"Caption: {caption}\n"
|
| 90 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 91 |
)
|
| 92 |
raw_ana = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
#
|
| 98 |
sug_prompt = (
|
| 99 |
-
f"Caption: {caption}\n"
|
| 100 |
"Suggest five distinct improvements for this ad. "
|
| 101 |
-
"Each suggestion must start with '- ' and be one
|
| 102 |
)
|
| 103 |
raw_sug = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
extras = [l.strip() for l in raw_sug.splitlines() if l.strip()]
|
| 107 |
for ex in extras:
|
| 108 |
-
if len(
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
suggestions = "\n".join(lines[:5])
|
| 113 |
-
logging.info(f"RAW SUGGESTIONS:\n{raw_sug}")
|
| 114 |
|
| 115 |
return caption, category, analysis, suggestions, get_recommendations()
|
| 116 |
|
| 117 |
-
with gr.Blocks(
|
| 118 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 119 |
gr.Markdown(
|
| 120 |
-
"Upload an image ad to get
|
| 121 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
)
|
| 123 |
|
| 124 |
with gr.Row():
|
| 125 |
-
|
| 126 |
with gr.Column():
|
| 127 |
-
|
| 128 |
-
cat_out
|
| 129 |
-
ana_out
|
| 130 |
-
sug_out
|
| 131 |
-
btn
|
| 132 |
|
| 133 |
-
gallery = gr.Gallery(label="Example Ads",
|
| 134 |
|
| 135 |
btn.click(
|
| 136 |
fn=process,
|
| 137 |
-
inputs=[
|
| 138 |
-
outputs=[
|
| 139 |
)
|
| 140 |
|
| 141 |
gr.Markdown("Made by Simon Thalmay")
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# 1) BLIP captioner (rich COCO captions)
|
| 7 |
caption_pipe = pipeline(
|
| 8 |
task="image-to-text",
|
| 9 |
+
model="Salesforce/blip-image-captioning-large",
|
| 10 |
+
device=-1, # force CPU
|
|
|
|
| 11 |
max_length=64,
|
| 12 |
do_sample=False,
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# 2) Flan-T5 for text‐to‐text
|
| 16 |
+
FLAN = "google/flan-t5-large"
|
| 17 |
category_pipe = pipeline(
|
| 18 |
"text2text-generation",
|
| 19 |
+
model=FLAN,
|
| 20 |
+
tokenizer=FLAN,
|
| 21 |
max_new_tokens=32,
|
| 22 |
do_sample=True,
|
| 23 |
temperature=1.0,
|
| 24 |
)
|
| 25 |
analysis_pipe = pipeline(
|
| 26 |
"text2text-generation",
|
| 27 |
+
model=FLAN,
|
| 28 |
+
tokenizer=FLAN,
|
| 29 |
max_new_tokens=256,
|
| 30 |
do_sample=True,
|
| 31 |
temperature=1.0,
|
| 32 |
)
|
| 33 |
suggestion_pipe = pipeline(
|
| 34 |
"text2text-generation",
|
| 35 |
+
model=FLAN,
|
| 36 |
+
tokenizer=FLAN,
|
| 37 |
max_new_tokens=256,
|
| 38 |
do_sample=True,
|
| 39 |
temperature=1.0,
|
|
|
|
| 46 |
"https://i.imgur.com/wp3Wzc4.jpeg",
|
| 47 |
"https://i.imgur.com/5e2xOA4.jpeg",
|
| 48 |
"https://i.imgur.com/txjRk98.jpeg",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
]
|
| 50 |
|
| 51 |
def process(image: Image):
|
| 52 |
+
# 1) BLIP caption
|
| 53 |
caption = caption_pipe(image)[0]["generated_text"].strip()
|
|
|
|
| 54 |
|
| 55 |
+
# 2) Single‐label category
|
| 56 |
cat_prompt = (
|
| 57 |
+
f"Caption: {caption}\n\n"
|
| 58 |
+
"Give me one concise category label for this ad (e.g. 'Fitness', 'Food'):"
|
| 59 |
)
|
| 60 |
+
category = category_pipe(cat_prompt)[0]["generated_text"].strip().splitlines()[0]
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# 3) Five‐sentence analysis
|
| 63 |
ana_prompt = (
|
| 64 |
+
f"Caption: {caption}\n\n"
|
| 65 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 66 |
)
|
| 67 |
raw_ana = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
|
| 68 |
+
# ensure exactly five sentences
|
| 69 |
+
sents = re.split(r'(?<=[.!?])\s+', raw_ana)
|
| 70 |
+
analysis = " ".join(sents[:5])
|
| 71 |
|
| 72 |
+
# 4) Five bullet‐point suggestions
|
| 73 |
sug_prompt = (
|
| 74 |
+
f"Caption: {caption}\n\n"
|
| 75 |
"Suggest five distinct improvements for this ad. "
|
| 76 |
+
"Each suggestion must start with '- ' and be one sentence."
|
| 77 |
)
|
| 78 |
raw_sug = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
|
| 79 |
+
bullets = [l for l in raw_sug.splitlines() if l.strip().startswith("-")]
|
| 80 |
+
# pad/truncate to 5
|
| 81 |
+
if len(bullets) < 5:
|
| 82 |
extras = [l.strip() for l in raw_sug.splitlines() if l.strip()]
|
| 83 |
for ex in extras:
|
| 84 |
+
if len(bullets) >= 5: break
|
| 85 |
+
line = ex if ex.startswith("-") else "- " + ex
|
| 86 |
+
bullets.append(line)
|
| 87 |
+
suggestions = "\n".join(bullets[:5])
|
|
|
|
|
|
|
| 88 |
|
| 89 |
return caption, category, analysis, suggestions, get_recommendations()
|
| 90 |
|
| 91 |
+
with gr.Blocks() as demo:
|
| 92 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 93 |
gr.Markdown(
|
| 94 |
+
"Upload an image ad to get:\n"
|
| 95 |
+
"- **BLIP Caption** (debug)\n"
|
| 96 |
+
"- **Category**\n"
|
| 97 |
+
"- **Five-sentence Analysis**\n"
|
| 98 |
+
"- **Five bullet-point Suggestions**\n"
|
| 99 |
+
"- **Example Ads**"
|
| 100 |
)
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
+
inp = gr.Image(type="pil", label="Upload Ad Image")
|
| 104 |
with gr.Column():
|
| 105 |
+
cap_out = gr.Textbox(label="🔍 BLIP Caption", interactive=False)
|
| 106 |
+
cat_out = gr.Textbox(label="Ad Category", interactive=False)
|
| 107 |
+
ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
|
| 108 |
+
sug_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
|
| 109 |
+
btn = gr.Button("Analyze Ad")
|
| 110 |
|
| 111 |
+
gallery = gr.Gallery(label="Example Ads").style(grid=[5], height="auto")
|
| 112 |
|
| 113 |
btn.click(
|
| 114 |
fn=process,
|
| 115 |
+
inputs=[inp],
|
| 116 |
+
outputs=[cap_out, cat_out, ana_out, sug_out, gallery],
|
| 117 |
)
|
| 118 |
|
| 119 |
gr.Markdown("Made by Simon Thalmay")
|