Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ blip_model = BlipForConditionalGeneration.from_pretrained(
|
|
| 12 |
"Salesforce/blip-image-captioning-base"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# Hugging Face pipelines (
|
| 16 |
category_generator = pipeline(
|
| 17 |
"text2text-generation",
|
| 18 |
model="google/flan-t5-small",
|
|
@@ -20,29 +20,25 @@ category_generator = pipeline(
|
|
| 20 |
max_new_tokens=50,
|
| 21 |
do_sample=True,
|
| 22 |
temperature=1.0
|
| 23 |
-
)
|
|
|
|
| 24 |
analysis_generator = pipeline(
|
| 25 |
"text2text-generation",
|
| 26 |
model="google/flan-t5-small",
|
| 27 |
tokenizer="google/flan-t5-small",
|
| 28 |
-
max_new_tokens=500,
|
| 29 |
-
do_sample=True,
|
| 30 |
-
temperature=1.0
|
| 31 |
-
) # Switched to Flan-T5-small for faster analysis # reduced tokens for speed
|
| 32 |
do_sample=True,
|
| 33 |
temperature=1.0
|
| 34 |
-
)
|
|
|
|
| 35 |
suggestion_generator = pipeline(
|
| 36 |
"text2text-generation",
|
| 37 |
model="google/flan-t5-small",
|
| 38 |
tokenizer="google/flan-t5-small",
|
| 39 |
-
max_new_tokens=500,
|
| 40 |
do_sample=True,
|
| 41 |
temperature=1.0
|
| 42 |
-
)
|
| 43 |
-
do_sample=True,
|
| 44 |
-
temperature=1.0
|
| 45 |
-
) # Using Flan-T5-small for quicker suggestions
|
| 46 |
|
| 47 |
# Example URLs for gallery
|
| 48 |
def get_recommendations():
|
|
@@ -59,14 +55,13 @@ def get_recommendations():
|
|
| 59 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 60 |
]
|
| 61 |
|
| 62 |
-
# Generate
|
| 63 |
def generate_caption(image):
|
| 64 |
inputs = blip_processor(images=image, return_tensors="pt")
|
| 65 |
outputs = blip_model.generate(**inputs)
|
| 66 |
return blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
def generate_category(caption):
|
| 71 |
prompt = (
|
| 72 |
f"Caption: {caption}\n"
|
|
@@ -76,7 +71,6 @@ def generate_category(caption):
|
|
| 76 |
return raw.splitlines()[0]
|
| 77 |
|
| 78 |
# Produce 5-sentence analysis via Flan
|
| 79 |
-
|
| 80 |
def generate_analysis(caption):
|
| 81 |
prompt = (
|
| 82 |
f"Caption: {caption}\n"
|
|
@@ -87,7 +81,6 @@ def generate_analysis(caption):
|
|
| 87 |
return " ".join(sentences[:5])
|
| 88 |
|
| 89 |
# Suggest 5 bullet-point improvements via Flan
|
| 90 |
-
|
| 91 |
def generate_suggestions(caption):
|
| 92 |
prompt = (
|
| 93 |
f"Caption: {caption}\n"
|
|
@@ -97,7 +90,7 @@ def generate_suggestions(caption):
|
|
| 97 |
lines = [l for l in raw.splitlines() if l.strip().startswith('-')]
|
| 98 |
return "\n".join(lines[:5]) if lines else "\n".join(raw.splitlines()[:5])
|
| 99 |
|
| 100 |
-
# Full pipeline
|
| 101 |
def process(image):
|
| 102 |
caption = generate_caption(image)
|
| 103 |
category = generate_category(caption)
|
|
@@ -106,11 +99,11 @@ def process(image):
|
|
| 106 |
recs = get_recommendations()
|
| 107 |
return category, analysis, suggestions, recs
|
| 108 |
|
| 109 |
-
# UI Layout
|
| 110 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 111 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 112 |
gr.Markdown(
|
| 113 |
-
"Upload an image ad
|
| 114 |
)
|
| 115 |
|
| 116 |
with gr.Row():
|
|
@@ -121,7 +114,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 121 |
suggestion_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
|
| 122 |
btn = gr.Button("Analyze Ad", size="sm", variant="primary")
|
| 123 |
|
| 124 |
-
# Full-width gallery below
|
| 125 |
recommendation_gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
|
| 126 |
|
| 127 |
btn.click(
|
|
|
|
| 12 |
"Salesforce/blip-image-captioning-base"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Hugging Face pipelines (all using Flan-T5-small for speed, temperature=1.0)
|
| 16 |
category_generator = pipeline(
|
| 17 |
"text2text-generation",
|
| 18 |
model="google/flan-t5-small",
|
|
|
|
| 20 |
max_new_tokens=50,
|
| 21 |
do_sample=True,
|
| 22 |
temperature=1.0
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
analysis_generator = pipeline(
|
| 26 |
"text2text-generation",
|
| 27 |
model="google/flan-t5-small",
|
| 28 |
tokenizer="google/flan-t5-small",
|
| 29 |
+
max_new_tokens=500,
|
|
|
|
|
|
|
|
|
|
| 30 |
do_sample=True,
|
| 31 |
temperature=1.0
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
suggestion_generator = pipeline(
|
| 35 |
"text2text-generation",
|
| 36 |
model="google/flan-t5-small",
|
| 37 |
tokenizer="google/flan-t5-small",
|
| 38 |
+
max_new_tokens=500,
|
| 39 |
do_sample=True,
|
| 40 |
temperature=1.0
|
| 41 |
+
)
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Example URLs for gallery
|
| 44 |
def get_recommendations():
|
|
|
|
| 55 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 56 |
]
|
| 57 |
|
| 58 |
+
# Generate BLIP caption from image
|
| 59 |
def generate_caption(image):
|
| 60 |
inputs = blip_processor(images=image, return_tensors="pt")
|
| 61 |
outputs = blip_model.generate(**inputs)
|
| 62 |
return blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 63 |
|
| 64 |
+
# Generate concise category via Flan
|
|
|
|
| 65 |
def generate_category(caption):
|
| 66 |
prompt = (
|
| 67 |
f"Caption: {caption}\n"
|
|
|
|
| 71 |
return raw.splitlines()[0]
|
| 72 |
|
| 73 |
# Produce 5-sentence analysis via Flan
|
|
|
|
| 74 |
def generate_analysis(caption):
|
| 75 |
prompt = (
|
| 76 |
f"Caption: {caption}\n"
|
|
|
|
| 81 |
return " ".join(sentences[:5])
|
| 82 |
|
| 83 |
# Suggest 5 bullet-point improvements via Flan
|
|
|
|
| 84 |
def generate_suggestions(caption):
|
| 85 |
prompt = (
|
| 86 |
f"Caption: {caption}\n"
|
|
|
|
| 90 |
lines = [l for l in raw.splitlines() if l.strip().startswith('-')]
|
| 91 |
return "\n".join(lines[:5]) if lines else "\n".join(raw.splitlines()[:5])
|
| 92 |
|
| 93 |
+
# Full pipeline combining all steps
|
| 94 |
def process(image):
|
| 95 |
caption = generate_caption(image)
|
| 96 |
category = generate_category(caption)
|
|
|
|
| 99 |
recs = get_recommendations()
|
| 100 |
return category, analysis, suggestions, recs
|
| 101 |
|
| 102 |
+
# UI Layout using Gradio
|
| 103 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 104 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 105 |
gr.Markdown(
|
| 106 |
+
"Upload an image ad to see: an Ad Category label, a five-sentence analysis, five bullet-point improvements, and example ads."
|
| 107 |
)
|
| 108 |
|
| 109 |
with gr.Row():
|
|
|
|
| 114 |
suggestion_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
|
| 115 |
btn = gr.Button("Analyze Ad", size="sm", variant="primary")
|
| 116 |
|
|
|
|
| 117 |
recommendation_gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
|
| 118 |
|
| 119 |
btn.click(
|