Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,22 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def get_recommendations():
|
| 6 |
# Returns list of 10 example ad image URLs
|
| 7 |
return [
|
|
@@ -17,50 +33,43 @@ def get_recommendations():
|
|
| 17 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 18 |
]
|
| 19 |
|
| 20 |
-
# BLIP for image captioning (always on CPU, runs fast)
|
| 21 |
-
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device="cpu")
|
| 22 |
-
|
| 23 |
-
# Gemma 1B for text generation
|
| 24 |
-
gemma = pipeline("text-generation", model="google/gemma-1.1-1b-it", device="cpu")
|
| 25 |
-
|
| 26 |
def process(image):
|
| 27 |
if image is None:
|
| 28 |
return "", "", "", get_recommendations()
|
| 29 |
-
|
| 30 |
-
# 1. Caption
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# 2. Compose a prompt for Gemma (category, analysis, suggestions)
|
| 35 |
prompt = (
|
| 36 |
-
f"
|
| 37 |
-
"
|
|
|
|
| 38 |
"2. Write exactly five sentences analyzing what this ad communicates and its emotional impact.\n"
|
| 39 |
-
"3. Suggest five
|
| 40 |
-
"Answer in three parts clearly marked as Category, Analysis, and Suggestions."
|
| 41 |
)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
cat, analysis, suggestions = "", "", ""
|
| 47 |
-
for
|
| 48 |
if "category" in line.lower():
|
| 49 |
cat = line.split(":", 1)[-1].strip()
|
| 50 |
-
elif "analysis" in line.lower():
|
| 51 |
-
analysis
|
| 52 |
-
elif "
|
| 53 |
-
suggestions
|
| 54 |
-
# Fallback if Gemma output is not perfectly formatted
|
| 55 |
if not cat: cat = lines[0][:80]
|
| 56 |
if not analysis: analysis = "\n".join(lines[1:6])
|
| 57 |
if not suggestions: suggestions = "\n".join(lines[6:11])
|
| 58 |
-
|
| 59 |
return cat.strip(), analysis.strip(), suggestions.strip(), get_recommendations()
|
| 60 |
|
| 61 |
def main():
|
| 62 |
-
with gr.Blocks(title="Smart Ad Analyzer (
|
| 63 |
-
gr.Markdown("## 📢 Smart Ad Analyzer (
|
| 64 |
gr.Markdown(
|
| 65 |
"""
|
| 66 |
Upload your ad image below and instantly get expert feedback.
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
+
|
| 7 |
+
# 1. Image Captioning (fast lightweight)
|
| 8 |
+
captioner = pipeline(
|
| 9 |
+
"image-to-text",
|
| 10 |
+
model="google/paligemma-3b-pt-224",
|
| 11 |
+
token=HF_TOKEN
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# 2. Text Feedback/Analysis (fast lightweight)
|
| 15 |
+
reviewer = pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
model="google/gemma-1.1-2b-it",
|
| 18 |
+
token=HF_TOKEN
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
def get_recommendations():
|
| 22 |
# Returns list of 10 example ad image URLs
|
| 23 |
return [
|
|
|
|
| 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/ad description
|
| 41 |
+
cap = captioner(image)[0]["generated_text"].strip()
|
| 42 |
+
|
| 43 |
+
# 2. Build feedback prompt
|
|
|
|
| 44 |
prompt = (
|
| 45 |
+
f"Ad description: {cap}\n"
|
| 46 |
+
"Your task: \n"
|
| 47 |
+
"1. Give a concise category label for this ad (e.g., 'Food', 'Fitness').\n"
|
| 48 |
"2. Write exactly five sentences analyzing what this ad communicates and its emotional impact.\n"
|
| 49 |
+
"3. Suggest five ways to improve this ad. Each suggestion should be a short, practical sentence."
|
|
|
|
| 50 |
)
|
| 51 |
+
# 3. Run through Gemma
|
| 52 |
+
resp = reviewer(prompt, max_new_tokens=256)[0]["generated_text"]
|
| 53 |
+
|
| 54 |
+
# 4. Simple parsing logic
|
| 55 |
+
lines = resp.split('\n')
|
| 56 |
cat, analysis, suggestions = "", "", ""
|
| 57 |
+
for line in lines:
|
| 58 |
if "category" in line.lower():
|
| 59 |
cat = line.split(":", 1)[-1].strip()
|
| 60 |
+
elif "analysis" in line.lower() or "sentence" in line.lower():
|
| 61 |
+
analysis += line + " "
|
| 62 |
+
elif "suggestion" in line.lower() or line.strip().startswith("-"):
|
| 63 |
+
suggestions += line + "\n"
|
|
|
|
| 64 |
if not cat: cat = lines[0][:80]
|
| 65 |
if not analysis: analysis = "\n".join(lines[1:6])
|
| 66 |
if not suggestions: suggestions = "\n".join(lines[6:11])
|
| 67 |
+
|
| 68 |
return cat.strip(), analysis.strip(), suggestions.strip(), get_recommendations()
|
| 69 |
|
| 70 |
def main():
|
| 71 |
+
with gr.Blocks(title="Smart Ad Analyzer (Fast Edition)") as demo:
|
| 72 |
+
gr.Markdown("## 📢 Smart Ad Analyzer (Fast Edition)")
|
| 73 |
gr.Markdown(
|
| 74 |
"""
|
| 75 |
Upload your ad image below and instantly get expert feedback.
|