Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,44 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"text-generation",
|
| 17 |
-
model=
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
def get_recommendations():
|
| 22 |
-
# Returns list of 10 example ad image URLs
|
| 23 |
return [
|
| 24 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 25 |
"https://i.imgur.com/7BHfv4T.png",
|
|
@@ -33,43 +52,69 @@ def get_recommendations():
|
|
| 33 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 34 |
]
|
| 35 |
|
| 36 |
-
def process(image):
|
| 37 |
if image is None:
|
| 38 |
return "", "", "", get_recommendations()
|
| 39 |
-
|
| 40 |
-
# 1. Caption
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
def main():
|
| 71 |
-
with gr.Blocks(title="Smart Ad Analyzer (
|
| 72 |
-
gr.Markdown("## 📢 Smart Ad Analyzer (
|
| 73 |
gr.Markdown(
|
| 74 |
"""
|
| 75 |
Upload your ad image below and instantly get expert feedback.
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from transformers import pipeline, AutoProcessor, AutoModelForVision2Seq, AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
| 7 |
+
# --- SETUP TOKEN ---
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Set in env or Secrets on Spaces
|
| 9 |
|
| 10 |
+
# --- DEVICE ---
|
| 11 |
+
DEVICE = 0 if torch.cuda.is_available() else -1
|
| 12 |
+
|
| 13 |
+
# --- BLIP: Captioning ---
|
| 14 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large", token=HF_TOKEN)
|
| 15 |
+
blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large", token=HF_TOKEN)
|
| 16 |
+
caption_pipe = pipeline(
|
| 17 |
+
task="image-to-text",
|
| 18 |
+
model=blip_model,
|
| 19 |
+
tokenizer=processor.tokenizer,
|
| 20 |
+
image_processor=processor.image_processor,
|
| 21 |
+
device=DEVICE,
|
| 22 |
+
token=HF_TOKEN,
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# --- GEMMA: Text Generation ---
|
| 26 |
+
# Swap this to your preferred Gemma model ID, e.g. "google/gemma-2b-it"
|
| 27 |
+
GEMMA_MODEL = "google/gemma-2b-it"
|
| 28 |
+
|
| 29 |
+
gemma_tokenizer = AutoTokenizer.from_pretrained(GEMMA_MODEL, token=HF_TOKEN)
|
| 30 |
+
gemma_model = AutoModelForCausalLM.from_pretrained(GEMMA_MODEL, token=HF_TOKEN)
|
| 31 |
+
gemma_pipe = pipeline(
|
| 32 |
"text-generation",
|
| 33 |
+
model=gemma_model,
|
| 34 |
+
tokenizer=gemma_tokenizer,
|
| 35 |
+
device=DEVICE,
|
| 36 |
+
max_new_tokens=384,
|
| 37 |
+
do_sample=False,
|
| 38 |
+
token=HF_TOKEN,
|
| 39 |
)
|
| 40 |
|
| 41 |
def get_recommendations():
|
|
|
|
| 42 |
return [
|
| 43 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 44 |
"https://i.imgur.com/7BHfv4T.png",
|
|
|
|
| 52 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 53 |
]
|
| 54 |
|
| 55 |
+
def process(image: Image):
|
| 56 |
if image is None:
|
| 57 |
return "", "", "", get_recommendations()
|
| 58 |
+
|
| 59 |
+
# 1. BLIP: Caption
|
| 60 |
+
caption_res = caption_pipe(image, max_new_tokens=64)
|
| 61 |
+
description = caption_res[0]["generated_text"].strip()
|
| 62 |
+
|
| 63 |
+
# 2. GEMMA: Category
|
| 64 |
+
prompt_cat = f"This is an ad image. Description: {description}\n\nProvide a concise category label for this ad (e.g. Food, Fitness, Technology):"
|
| 65 |
+
cat_out = gemma_pipe(prompt_cat)[0]['generated_text'].splitlines()[0].strip()
|
| 66 |
+
|
| 67 |
+
# 3. GEMMA: Five-sentence analysis
|
| 68 |
+
prompt_ana = (
|
| 69 |
+
f"This is an ad image. Description: {description}\n\n"
|
| 70 |
+
"Write exactly five sentences explaining what this ad communicates and its emotional impact."
|
| 71 |
)
|
| 72 |
+
ana_raw = gemma_pipe(prompt_ana)[0]['generated_text'].strip()
|
| 73 |
+
# Get only first five sentences.
|
| 74 |
+
import re
|
| 75 |
+
sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
|
| 76 |
+
analysis = " ".join(sentences[:5])
|
| 77 |
+
|
| 78 |
+
# 4. GEMMA: Five suggestions (bullets, unique)
|
| 79 |
+
prompt_sug = (
|
| 80 |
+
f"This is an ad image. Description: {description}\n\n"
|
| 81 |
+
"Suggest five unique, practical improvements for this ad. Each must address a different aspect (message, visuals, call-to-action, targeting, layout, or design). "
|
| 82 |
+
"Each suggestion must be one sentence and start with '- '. Do NOT repeat suggestions."
|
| 83 |
+
)
|
| 84 |
+
sug_raw = gemma_pipe(prompt_sug)[0]['generated_text']
|
| 85 |
+
bullets = []
|
| 86 |
+
seen = set()
|
| 87 |
+
for line in sug_raw.splitlines():
|
| 88 |
+
if line.startswith("-"):
|
| 89 |
+
suggestion = line.strip()
|
| 90 |
+
if suggestion and suggestion not in seen:
|
| 91 |
+
bullets.append(suggestion)
|
| 92 |
+
seen.add(suggestion)
|
| 93 |
+
elif line.strip():
|
| 94 |
+
suggestion = "- " + line.strip()
|
| 95 |
+
if suggestion and suggestion not in seen:
|
| 96 |
+
bullets.append(suggestion)
|
| 97 |
+
seen.add(suggestion)
|
| 98 |
+
if len(bullets) == 5:
|
| 99 |
+
break
|
| 100 |
+
# Defaults if not enough bullets
|
| 101 |
+
defaults = [
|
| 102 |
+
"- Make the main headline more eye-catching.",
|
| 103 |
+
"- Add a clear and visible call-to-action button.",
|
| 104 |
+
"- Use contrasting colors for better readability.",
|
| 105 |
+
"- Highlight the unique selling point of the product.",
|
| 106 |
+
"- Simplify the design to reduce clutter."
|
| 107 |
+
]
|
| 108 |
+
for default in defaults:
|
| 109 |
+
if len(bullets) < 5 and default not in seen:
|
| 110 |
+
bullets.append(default)
|
| 111 |
+
suggestions = "\n".join(bullets[:5])
|
| 112 |
+
|
| 113 |
+
return cat_out, analysis, suggestions, get_recommendations()
|
| 114 |
|
| 115 |
def main():
|
| 116 |
+
with gr.Blocks(title="Smart Ad Analyzer (BLIP+Gemma)") as demo:
|
| 117 |
+
gr.Markdown("## 📢 Smart Ad Analyzer (BLIP + Gemma)")
|
| 118 |
gr.Markdown(
|
| 119 |
"""
|
| 120 |
Upload your ad image below and instantly get expert feedback.
|