Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
-
from transformers import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
"
|
| 9 |
-
|
| 10 |
-
)
|
| 11 |
-
blip_model = BlipForConditionalGeneration.from_pretrained(
|
| 12 |
-
"Salesforce/blip-image-captioning-base"
|
| 13 |
)
|
| 14 |
|
| 15 |
# Helper to create Flan-T5 pipelines (temperature=1.0 for diversity)
|
|
@@ -24,12 +23,11 @@ def make_pipeline(model_name, max_tokens):
|
|
| 24 |
)
|
| 25 |
|
| 26 |
# Pipelines: category, analysis, suggestions
|
| 27 |
-
category_generator
|
| 28 |
-
analysis_generator
|
| 29 |
suggestion_generator = make_pipeline("google/flan-t5-small", 500)
|
| 30 |
|
| 31 |
# Example ads URLs for gallery
|
| 32 |
-
|
| 33 |
def get_recommendations():
|
| 34 |
return [
|
| 35 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
@@ -44,11 +42,10 @@ def get_recommendations():
|
|
| 44 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 45 |
]
|
| 46 |
|
| 47 |
-
# Step 1:
|
| 48 |
def generate_caption(image):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
return blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
|
| 53 |
# Step 2: Flan interprets caption into a concise category label
|
| 54 |
def generate_category(caption):
|
|
@@ -74,36 +71,39 @@ def generate_suggestions(caption):
|
|
| 74 |
"Each line must start with '- '."
|
| 75 |
)
|
| 76 |
raw = suggestion_generator(prompt)[0]["generated_text"].strip()
|
| 77 |
-
lines = [
|
| 78 |
if len(lines) < 5:
|
| 79 |
all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
|
| 80 |
-
lines = [
|
|
|
|
|
|
|
|
|
|
| 81 |
return "\n".join(lines[:5])
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
def process(image):
|
| 86 |
-
caption
|
| 87 |
-
category
|
| 88 |
-
analysis
|
| 89 |
suggestions = generate_suggestions(caption)
|
| 90 |
-
recs
|
| 91 |
return category, analysis, suggestions, recs
|
| 92 |
|
| 93 |
-
# Gradio UI
|
| 94 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 95 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 96 |
gr.Markdown(
|
| 97 |
-
"Upload an image ad to see an Ad Category, a five-sentence Analysis,
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
image_input = gr.Image(type="pil", label="Upload Ad Image")
|
| 102 |
with gr.Column():
|
| 103 |
-
category_out
|
| 104 |
-
analysis_out
|
| 105 |
suggestion_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
|
| 106 |
-
btn
|
| 107 |
|
| 108 |
recommendation_gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
|
| 109 |
|
|
@@ -116,4 +116,4 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 116 |
gr.Markdown("Made by Simon Thalmay")
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import re
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
+
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# Use ChatDOC/OCRFlux-3B for image-to-text instead of BLIP
|
| 9 |
+
image_to_text = pipeline(
|
| 10 |
+
"image-to-text",
|
| 11 |
+
model="ChatDOC/OCRFlux-3B"
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
# Helper to create Flan-T5 pipelines (temperature=1.0 for diversity)
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
# Pipelines: category, analysis, suggestions
|
| 26 |
+
category_generator = make_pipeline("google/flan-t5-small", 100)
|
| 27 |
+
analysis_generator = make_pipeline("google/flan-t5-small", 500)
|
| 28 |
suggestion_generator = make_pipeline("google/flan-t5-small", 500)
|
| 29 |
|
| 30 |
# Example ads URLs for gallery
|
|
|
|
| 31 |
def get_recommendations():
|
| 32 |
return [
|
| 33 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
|
|
| 42 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 43 |
]
|
| 44 |
|
| 45 |
+
# Step 1: Use OCRFlux to get a detailed textual description of the image
|
| 46 |
def generate_caption(image):
|
| 47 |
+
result = image_to_text(image)
|
| 48 |
+
return result[0]["generated_text"].strip()
|
|
|
|
| 49 |
|
| 50 |
# Step 2: Flan interprets caption into a concise category label
|
| 51 |
def generate_category(caption):
|
|
|
|
| 71 |
"Each line must start with '- '."
|
| 72 |
)
|
| 73 |
raw = suggestion_generator(prompt)[0]["generated_text"].strip()
|
| 74 |
+
lines = [l for l in raw.splitlines() if l.strip().startswith('- ')]
|
| 75 |
if len(lines) < 5:
|
| 76 |
all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
|
| 77 |
+
lines = [
|
| 78 |
+
('- ' + all_lines[i]) if not all_lines[i].startswith('- ') else all_lines[i]
|
| 79 |
+
for i in range(min(5, len(all_lines)))
|
| 80 |
+
]
|
| 81 |
return "\n".join(lines[:5])
|
| 82 |
|
| 83 |
+
# Full workflow
|
|
|
|
| 84 |
def process(image):
|
| 85 |
+
caption = generate_caption(image)
|
| 86 |
+
category = generate_category(caption)
|
| 87 |
+
analysis = generate_analysis(caption)
|
| 88 |
suggestions = generate_suggestions(caption)
|
| 89 |
+
recs = get_recommendations()
|
| 90 |
return category, analysis, suggestions, recs
|
| 91 |
|
| 92 |
+
# Gradio UI
|
| 93 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 94 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 95 |
gr.Markdown(
|
| 96 |
+
"Upload an image ad to see an Ad Category, a five-sentence Analysis, "
|
| 97 |
+
"five bullet-point Suggestions, and Example Ads."
|
| 98 |
)
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
image_input = gr.Image(type="pil", label="Upload Ad Image")
|
| 102 |
with gr.Column():
|
| 103 |
+
category_out = gr.Textbox(label="Ad Category", interactive=False)
|
| 104 |
+
analysis_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
|
| 105 |
suggestion_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
|
| 106 |
+
btn = gr.Button("Analyze Ad", size="sm", variant="primary")
|
| 107 |
|
| 108 |
recommendation_gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
|
| 109 |
|
|
|
|
| 116 |
gr.Markdown("Made by Simon Thalmay")
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
+
demo.launch()
|