Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,29 @@
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
from transformers import pipeline
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
caption_pipe = pipeline(
|
| 8 |
-
|
| 9 |
-
model=
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
do_sample=False,
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# 2) Flan-T5 for 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,
|
|
@@ -26,6 +32,7 @@ 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,
|
|
@@ -34,15 +41,17 @@ 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,
|
| 40 |
)
|
| 41 |
-
#
|
| 42 |
expansion_pipe = pipeline(
|
| 43 |
"text2text-generation",
|
| 44 |
model=FLAN,
|
| 45 |
tokenizer=FLAN,
|
|
|
|
| 46 |
max_new_tokens=128,
|
| 47 |
do_sample=False,
|
| 48 |
)
|
|
@@ -62,81 +71,84 @@ def get_recommendations():
|
|
| 62 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 63 |
]
|
| 64 |
|
| 65 |
-
|
| 66 |
def process(image: Image):
|
| 67 |
# 1) BLIP caption
|
| 68 |
-
caption = caption_pipe(image)[0][
|
| 69 |
|
| 70 |
-
# 1a) Expand if too short
|
| 71 |
if len(caption.split()) < 3:
|
| 72 |
-
|
| 73 |
-
desc = expansion_pipe(exp_prompt)[0]["generated_text"].strip()
|
| 74 |
else:
|
| 75 |
desc = caption
|
| 76 |
|
| 77 |
-
# 2)
|
| 78 |
cat_prompt = (
|
| 79 |
f"Description: {desc}\n\n"
|
| 80 |
-
"Provide a concise category label for this ad (e.g. '
|
| 81 |
)
|
| 82 |
-
category = category_pipe(cat_prompt)[0][
|
| 83 |
|
| 84 |
-
# 3) Five
|
| 85 |
ana_prompt = (
|
| 86 |
f"Description: {desc}\n\n"
|
| 87 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 88 |
)
|
| 89 |
-
raw_ana = analysis_pipe(ana_prompt)[0][
|
| 90 |
-
|
| 91 |
-
analysis = " ".join(
|
| 92 |
|
| 93 |
-
# 4) Five bullet
|
| 94 |
sug_prompt = (
|
| 95 |
f"Description: {desc}\n\n"
|
| 96 |
-
"Suggest five distinct improvements for this ad. "
|
| 97 |
-
"Each suggestion must start with '- ' and be one sentence."
|
| 98 |
)
|
| 99 |
-
raw_sug = suggestion_pipe(sug_prompt)[0][
|
| 100 |
-
bullets = [l for l in raw_sug.splitlines() if l.
|
| 101 |
if len(bullets) < 5:
|
| 102 |
-
|
| 103 |
-
for
|
| 104 |
-
if len(bullets) >= 5:
|
| 105 |
-
|
| 106 |
-
bullets.append(line)
|
| 107 |
-
suggestions =
|
| 108 |
|
| 109 |
return caption, category, analysis, suggestions, get_recommendations()
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
gr.
|
| 114 |
-
"
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
with gr.
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
from PIL import Image
|
| 5 |
+
from transformers import pipeline, AutoProcessor, AutoModelForVision2Seq
|
| 6 |
|
| 7 |
+
# Auto-detect CPU/GPU
|
| 8 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 9 |
+
|
| 10 |
+
# 1) BLIP captioner
|
| 11 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 12 |
+
model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 13 |
caption_pipe = pipeline(
|
| 14 |
+
"image-to-text",
|
| 15 |
+
model=model,
|
| 16 |
+
processor=processor,
|
| 17 |
+
device=device
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# 2) Flan-T5 for text-to-text
|
| 21 |
FLAN = "google/flan-t5-large"
|
| 22 |
category_pipe = pipeline(
|
| 23 |
"text2text-generation",
|
| 24 |
model=FLAN,
|
| 25 |
tokenizer=FLAN,
|
| 26 |
+
device=device,
|
| 27 |
max_new_tokens=32,
|
| 28 |
do_sample=True,
|
| 29 |
temperature=1.0,
|
|
|
|
| 32 |
"text2text-generation",
|
| 33 |
model=FLAN,
|
| 34 |
tokenizer=FLAN,
|
| 35 |
+
device=device,
|
| 36 |
max_new_tokens=256,
|
| 37 |
do_sample=True,
|
| 38 |
temperature=1.0,
|
|
|
|
| 41 |
"text2text-generation",
|
| 42 |
model=FLAN,
|
| 43 |
tokenizer=FLAN,
|
| 44 |
+
device=device,
|
| 45 |
max_new_tokens=256,
|
| 46 |
do_sample=True,
|
| 47 |
temperature=1.0,
|
| 48 |
)
|
| 49 |
+
# Expander when BLIP caption is too short
|
| 50 |
expansion_pipe = pipeline(
|
| 51 |
"text2text-generation",
|
| 52 |
model=FLAN,
|
| 53 |
tokenizer=FLAN,
|
| 54 |
+
device=device,
|
| 55 |
max_new_tokens=128,
|
| 56 |
do_sample=False,
|
| 57 |
)
|
|
|
|
| 71 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 72 |
]
|
| 73 |
|
| 74 |
+
# Main processing function
|
| 75 |
def process(image: Image):
|
| 76 |
# 1) BLIP caption
|
| 77 |
+
caption = caption_pipe(image, max_new_tokens=64, do_sample=False)[0]['generated_text'].strip()
|
| 78 |
|
| 79 |
+
# 1a) Expand caption if too short
|
| 80 |
if len(caption.split()) < 3:
|
| 81 |
+
desc = expansion_pipe(f"Expand into a detailed description: {caption}")[0]['generated_text'].strip()
|
|
|
|
| 82 |
else:
|
| 83 |
desc = caption
|
| 84 |
|
| 85 |
+
# 2) Ad category
|
| 86 |
cat_prompt = (
|
| 87 |
f"Description: {desc}\n\n"
|
| 88 |
+
"Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
|
| 89 |
)
|
| 90 |
+
category = category_pipe(cat_prompt)[0]['generated_text'].splitlines()[0].strip()
|
| 91 |
|
| 92 |
+
# 3) Five-sentence analysis
|
| 93 |
ana_prompt = (
|
| 94 |
f"Description: {desc}\n\n"
|
| 95 |
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 96 |
)
|
| 97 |
+
raw_ana = analysis_pipe(ana_prompt)[0]['generated_text'].strip()
|
| 98 |
+
sentences = re.split(r'(?<=[.!?])\s+', raw_ana)
|
| 99 |
+
analysis = " ".join(sentences[:5])
|
| 100 |
|
| 101 |
+
# 4) Five bullet-point suggestions
|
| 102 |
sug_prompt = (
|
| 103 |
f"Description: {desc}\n\n"
|
| 104 |
+
"Suggest five distinct improvements for this ad. Each must start with '- ' and be one sentence."
|
|
|
|
| 105 |
)
|
| 106 |
+
raw_sug = suggestion_pipe(sug_prompt)[0]['generated_text'].strip()
|
| 107 |
+
bullets = [l for l in raw_sug.splitlines() if l.startswith('-')]
|
| 108 |
if len(bullets) < 5:
|
| 109 |
+
extra_lines = [l for l in raw_sug.splitlines() if l.strip()]
|
| 110 |
+
for line in extra_lines:
|
| 111 |
+
if len(bullets) >= 5:
|
| 112 |
+
break
|
| 113 |
+
bullets.append(line if line.startswith('-') else '- ' + line)
|
| 114 |
+
suggestions = '\n'.join(bullets[:5])
|
| 115 |
|
| 116 |
return caption, category, analysis, suggestions, get_recommendations()
|
| 117 |
|
| 118 |
+
# Gradio UI
|
| 119 |
+
def main():
|
| 120 |
+
with gr.Blocks() as demo:
|
| 121 |
+
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 122 |
+
gr.Markdown(
|
| 123 |
+
"Upload an image ad to get:\n"
|
| 124 |
+
"- **BLIP Caption** (raw)\n"
|
| 125 |
+
"- **Ad Category**\n"
|
| 126 |
+
"- **Five-sentence Analysis**\n"
|
| 127 |
+
"- **Five bullet-point Suggestions**\n"
|
| 128 |
+
"- **Example Ads**"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
with gr.Row():
|
| 132 |
+
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 133 |
+
with gr.Column():
|
| 134 |
+
cap_out = gr.Textbox(label='🔍 BLIP Caption', interactive=False)
|
| 135 |
+
cat_out = gr.Textbox(label='Ad Category', interactive=False)
|
| 136 |
+
ana_out = gr.Textbox(label='Ad Analysis', lines=5, interactive=False)
|
| 137 |
+
sug_out = gr.Textbox(label='Improvement Suggestions', lines=5, interactive=False)
|
| 138 |
+
btn = gr.Button('Analyze Ad', size='sm', variant='primary')
|
| 139 |
+
|
| 140 |
+
gallery = gr.Gallery(label='Example Ads')
|
| 141 |
+
|
| 142 |
+
btn.click(
|
| 143 |
+
fn=process,
|
| 144 |
+
inputs=[inp],
|
| 145 |
+
outputs=[cap_out, cat_out, ana_out, sug_out, gallery],
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
gr.Markdown('Made by Simon Thalmay')
|
| 149 |
+
|
| 150 |
+
demo.launch()
|
| 151 |
+
|
| 152 |
+
if __name__ == '__main__':
|
| 153 |
+
main()
|
| 154 |
+
|