Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,22 @@ from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
|
| 4 |
|
| 5 |
# Initialize BLIP for image captioning
|
| 6 |
blip_processor = BlipProcessor.from_pretrained(
|
| 7 |
-
"Salesforce/blip-image-captioning-base"
|
|
|
|
| 8 |
)
|
| 9 |
blip_model = BlipForConditionalGeneration.from_pretrained(
|
| 10 |
"Salesforce/blip-image-captioning-base"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
"text2text-generation",
|
| 16 |
-
model="google/flan-
|
| 17 |
-
tokenizer="google/flan-
|
| 18 |
-
max_new_tokens=
|
| 19 |
do_sample=True,
|
| 20 |
temperature=0.7,
|
| 21 |
-
|
| 22 |
-
top_p=0.95
|
| 23 |
)
|
| 24 |
|
| 25 |
|
|
@@ -28,73 +28,60 @@ def get_recommendations():
|
|
| 28 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 29 |
"https://i.imgur.com/7BHfv4T.png",
|
| 30 |
"https://i.imgur.com/wp3Wzc4.jpeg",
|
| 31 |
-
#
|
| 32 |
]
|
| 33 |
|
| 34 |
|
| 35 |
def generate_caption(image):
|
| 36 |
inputs = blip_processor(images=image, return_tensors="pt")
|
| 37 |
-
|
| 38 |
-
caption = blip_processor.decode(
|
| 39 |
return caption
|
| 40 |
|
| 41 |
|
| 42 |
def generate_advice_from_caption(caption):
|
| 43 |
-
"""
|
| 44 |
-
Uses a Hugging Face instruction‑tuned model (Flan‑UL2) to turn the BLIP caption
|
| 45 |
-
into targeted, bullet‑pointed ad improvement suggestions.
|
| 46 |
-
"""
|
| 47 |
prompt = (
|
| 48 |
f"Ad description: {caption}\n"
|
| 49 |
-
"
|
| 50 |
)
|
| 51 |
-
result =
|
| 52 |
-
# The model output may include the prompt, so extract after prompt
|
| 53 |
text = result[0]["generated_text"].replace(prompt, "").strip()
|
| 54 |
return text
|
| 55 |
|
| 56 |
|
| 57 |
def analyze_caption(caption):
|
| 58 |
-
keywords = ["product", "offer", "
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
def process(image):
|
| 65 |
caption = generate_caption(image)
|
| 66 |
-
|
| 67 |
analysis = analyze_caption(caption)
|
| 68 |
-
|
| 69 |
-
return caption,
|
| 70 |
|
| 71 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 72 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 73 |
gr.Markdown(
|
| 74 |
-
"Upload an image ad.
|
| 75 |
)
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
image_input = gr.Image(type="pil", label="Upload Ad Image")
|
| 79 |
with gr.Column():
|
| 80 |
-
caption_out = gr.Textbox(
|
| 81 |
-
|
| 82 |
-
)
|
| 83 |
-
suggestion_out = gr.Textbox(
|
| 84 |
-
label="Improvement Suggestions", interactive=False
|
| 85 |
-
)
|
| 86 |
-
analysis_out = gr.Textbox(
|
| 87 |
-
label="Ad Analysis", interactive=False
|
| 88 |
-
)
|
| 89 |
|
| 90 |
btn = gr.Button("Analyze Ad")
|
| 91 |
-
|
| 92 |
|
| 93 |
-
btn.click(
|
| 94 |
-
fn=process,
|
| 95 |
-
inputs=[image_input],
|
| 96 |
-
outputs=[caption_out, suggestion_out, analysis_out, recommendation_gallery]
|
| 97 |
-
)
|
| 98 |
|
| 99 |
gr.Markdown("Made by Simon Thalmay")
|
| 100 |
|
|
|
|
| 4 |
|
| 5 |
# Initialize BLIP for image captioning
|
| 6 |
blip_processor = BlipProcessor.from_pretrained(
|
| 7 |
+
"Salesforce/blip-image-captioning-base",
|
| 8 |
+
use_fast=True
|
| 9 |
)
|
| 10 |
blip_model = BlipForConditionalGeneration.from_pretrained(
|
| 11 |
"Salesforce/blip-image-captioning-base"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Lightweight instruction‑tuned HF model for suggestions
|
| 15 |
+
advisor = pipeline(
|
| 16 |
"text2text-generation",
|
| 17 |
+
model="google/flan-t5-base",
|
| 18 |
+
tokenizer="google/flan-t5-base",
|
| 19 |
+
max_new_tokens=80,
|
| 20 |
do_sample=True,
|
| 21 |
temperature=0.7,
|
| 22 |
+
top_p=0.9
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
|
|
|
|
| 28 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 29 |
"https://i.imgur.com/7BHfv4T.png",
|
| 30 |
"https://i.imgur.com/wp3Wzc4.jpeg",
|
| 31 |
+
# add more examples if desired
|
| 32 |
]
|
| 33 |
|
| 34 |
|
| 35 |
def generate_caption(image):
|
| 36 |
inputs = blip_processor(images=image, return_tensors="pt")
|
| 37 |
+
outputs = blip_model.generate(**inputs)
|
| 38 |
+
caption = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 39 |
return caption
|
| 40 |
|
| 41 |
|
| 42 |
def generate_advice_from_caption(caption):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
prompt = (
|
| 44 |
f"Ad description: {caption}\n"
|
| 45 |
+
"Suggest three concise bullet‑point improvements: clear call to action, highlight key benefits, boost visual appeal."
|
| 46 |
)
|
| 47 |
+
result = advisor(prompt)
|
|
|
|
| 48 |
text = result[0]["generated_text"].replace(prompt, "").strip()
|
| 49 |
return text
|
| 50 |
|
| 51 |
|
| 52 |
def analyze_caption(caption):
|
| 53 |
+
keywords = ["product", "offer", "call to action", "brand"]
|
| 54 |
+
return (
|
| 55 |
+
"👍 Likely effective for advertising."
|
| 56 |
+
if any(k in caption.lower() for k in keywords)
|
| 57 |
+
else "👎 Consider clearer focus or stronger messaging."
|
| 58 |
+
)
|
| 59 |
|
| 60 |
|
| 61 |
def process(image):
|
| 62 |
caption = generate_caption(image)
|
| 63 |
+
advice = generate_advice_from_caption(caption)
|
| 64 |
analysis = analyze_caption(caption)
|
| 65 |
+
recs = get_recommendations()
|
| 66 |
+
return caption, advice, analysis, recs
|
| 67 |
|
| 68 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 69 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 70 |
gr.Markdown(
|
| 71 |
+
"Upload an image ad. BLIP captions it. Flan‑T5‑Base suggests improvements. Then you get analysis and example ads."
|
| 72 |
)
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
image_input = gr.Image(type="pil", label="Upload Ad Image")
|
| 76 |
with gr.Column():
|
| 77 |
+
caption_out = gr.Textbox(label="Generated Caption", interactive=False)
|
| 78 |
+
suggestion_out = gr.Textbox(label="Improvement Suggestions", interactive=False)
|
| 79 |
+
analysis_out = gr.Textbox(label="Ad Analysis", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
btn = gr.Button("Analyze Ad")
|
| 82 |
+
gallery = gr.Gallery(label="Recommended Example Ads")
|
| 83 |
|
| 84 |
+
btn.click(fn=process, inputs=[image_input], outputs=[caption_out, suggestion_out, analysis_out, gallery])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
gr.Markdown("Made by Simon Thalmay")
|
| 87 |
|