Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,10 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
from transformers import pipeline
|
| 4 |
-
import tempfile
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def process(image):
|
| 10 |
-
# Save image to temp file and use local path for Gemma
|
| 11 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
| 12 |
-
image.save(tmp, format="PNG")
|
| 13 |
-
tmp.flush()
|
| 14 |
-
img_path = tmp.name
|
| 15 |
-
|
| 16 |
-
# Build multimodal message for Gemma
|
| 17 |
-
messages = [
|
| 18 |
-
{
|
| 19 |
-
"role": "user",
|
| 20 |
-
"content": [
|
| 21 |
-
{"type": "image", "path": img_path},
|
| 22 |
-
{"type": "text", "text": "Analyze this ad image in detail. What is the product or service? Who is the target audience? Suggest five unique improvements to the ad. Output exactly four fields separated by |||: (1) Category (2) Five-sentence analysis (3) Five bullet suggestions (4) Short punchy headline for the ad."}
|
| 23 |
-
]
|
| 24 |
-
}
|
| 25 |
-
]
|
| 26 |
-
|
| 27 |
-
# Get model output
|
| 28 |
-
out = pipe(text=messages, max_new_tokens=512)
|
| 29 |
-
output = out[0]["generated_text"][-1]["content"] if "generated_text" in out[0] else out[0]["content"]
|
| 30 |
-
|
| 31 |
-
# Parse results by "|||"
|
| 32 |
-
if "|||" in output:
|
| 33 |
-
cat, analysis, suggestions, headline = [x.strip() for x in output.split("|||")]
|
| 34 |
-
else:
|
| 35 |
-
cat = "N/A"
|
| 36 |
-
analysis = output.strip()
|
| 37 |
-
suggestions = ""
|
| 38 |
-
headline = ""
|
| 39 |
-
|
| 40 |
-
# Also provide example ads (unchanged)
|
| 41 |
-
gallery = [
|
| 42 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 43 |
"https://i.imgur.com/7BHfv4T.png",
|
| 44 |
"https://i.imgur.com/wp3Wzc4.jpeg",
|
|
@@ -51,32 +17,68 @@ def process(image):
|
|
| 51 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 52 |
]
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
def main():
|
| 57 |
-
with gr.Blocks(title="Smart Ad Analyzer (Gemma
|
| 58 |
-
gr.Markdown("## 📢 Smart Ad Analyzer (Gemma
|
| 59 |
-
gr.Markdown(
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
- **Example ad gallery**
|
| 66 |
-
""")
|
| 67 |
with gr.Row():
|
| 68 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 69 |
with gr.Column():
|
| 70 |
-
cat_out = gr.Textbox(label='Ad Category')
|
| 71 |
-
ana_out = gr.Textbox(label='Analysis', lines=5)
|
| 72 |
-
sug_out = gr.Textbox(label='Improvement Suggestions', lines=5)
|
| 73 |
-
head_out = gr.Textbox(label='Headline Suggestion')
|
| 74 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 75 |
gallery = gr.Gallery(label='Example Ads')
|
| 76 |
btn.click(
|
| 77 |
fn=process,
|
| 78 |
inputs=[inp],
|
| 79 |
-
outputs=[cat_out, ana_out, sug_out,
|
| 80 |
)
|
| 81 |
gr.Markdown('Made by Simon Thalmay')
|
| 82 |
return demo
|
|
|
|
| 1 |
+
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 [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 9 |
"https://i.imgur.com/7BHfv4T.png",
|
| 10 |
"https://i.imgur.com/wp3Wzc4.jpeg",
|
|
|
|
| 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 image
|
| 31 |
+
cap_result = captioner(image)
|
| 32 |
+
caption = cap_result[0]['generated_text'] if cap_result else "No caption generated."
|
| 33 |
+
|
| 34 |
+
# 2. Compose a prompt for Gemma (category, analysis, suggestions)
|
| 35 |
+
prompt = (
|
| 36 |
+
f"Here is a description of an ad image: {caption}\n"
|
| 37 |
+
"1. Assign a concise ad category label (e.g., 'Food', 'Fitness').\n"
|
| 38 |
+
"2. Write exactly five sentences analyzing what this ad communicates and its emotional impact.\n"
|
| 39 |
+
"3. Suggest five specific ways to improve this ad, each as a short, practical sentence.\n"
|
| 40 |
+
"Answer in three parts clearly marked as Category, Analysis, and Suggestions."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
gemma_out = gemma(prompt, max_new_tokens=256)[0]['generated_text']
|
| 44 |
+
# Split results
|
| 45 |
+
lines = gemma_out.split('\n')
|
| 46 |
+
cat, analysis, suggestions = "", "", ""
|
| 47 |
+
for i, line in enumerate(lines):
|
| 48 |
+
if "category" in line.lower():
|
| 49 |
+
cat = line.split(":", 1)[-1].strip()
|
| 50 |
+
elif "analysis" in line.lower():
|
| 51 |
+
analysis = "\n".join(lines[i+1:i+6])
|
| 52 |
+
elif "suggestions" in line.lower():
|
| 53 |
+
suggestions = "\n".join(lines[i+1:i+6])
|
| 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 (BLIP+Gemma Edition)") as demo:
|
| 63 |
+
gr.Markdown("## 📢 Smart Ad Analyzer (BLIP+Gemma Edition)")
|
| 64 |
+
gr.Markdown(
|
| 65 |
+
"""
|
| 66 |
+
Upload your ad image below and instantly get expert feedback.
|
| 67 |
+
Category, analysis, improvement suggestions—and example ads for inspiration.
|
| 68 |
+
"""
|
| 69 |
+
)
|
|
|
|
|
|
|
| 70 |
with gr.Row():
|
| 71 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 72 |
with gr.Column():
|
| 73 |
+
cat_out = gr.Textbox(label='🗂️ Ad Category', interactive=False)
|
| 74 |
+
ana_out = gr.Textbox(label='📊 Ad Analysis', lines=5, interactive=False)
|
| 75 |
+
sug_out = gr.Textbox(label='🛠️ Improvement Suggestions', lines=5, interactive=False)
|
|
|
|
| 76 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 77 |
gallery = gr.Gallery(label='Example Ads')
|
| 78 |
btn.click(
|
| 79 |
fn=process,
|
| 80 |
inputs=[inp],
|
| 81 |
+
outputs=[cat_out, ana_out, sug_out, gallery],
|
| 82 |
)
|
| 83 |
gr.Markdown('Made by Simon Thalmay')
|
| 84 |
return demo
|