Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
|
@@ -28,7 +30,6 @@ analysis_generator = make_pipeline("google/flan-t5-small", 500)
|
|
| 28 |
suggestion_generator = make_pipeline("google/flan-t5-small", 500)
|
| 29 |
|
| 30 |
# Hardcoded example ads for gallery
|
| 31 |
-
|
| 32 |
def get_recommendations():
|
| 33 |
return [
|
| 34 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
@@ -44,21 +45,17 @@ def get_recommendations():
|
|
| 44 |
]
|
| 45 |
|
| 46 |
# Step A: Use OCRFlux to generate a detailed caption
|
| 47 |
-
|
| 48 |
def generate_caption(image):
|
| 49 |
result = image_to_text(image)
|
| 50 |
-
|
| 51 |
-
return text
|
| 52 |
|
| 53 |
# Step B: Flan interprets caption into concise category
|
| 54 |
-
|
| 55 |
def generate_category(caption):
|
| 56 |
prompt = f"Caption: {caption}\nProvide a concise category label for this ad."
|
| 57 |
raw = category_generator(prompt)[0]["generated_text"].strip()
|
| 58 |
return raw.splitlines()[0]
|
| 59 |
|
| 60 |
# Step C: Flan produces exactly five-sentence analysis
|
| 61 |
-
|
| 62 |
def generate_analysis(caption):
|
| 63 |
prompt = (
|
| 64 |
f"Caption: {caption}\n"
|
|
@@ -69,23 +66,22 @@ def generate_analysis(caption):
|
|
| 69 |
return " ".join(sentences[:5])
|
| 70 |
|
| 71 |
# Step D: Flan suggests five actionable bullet-point improvements
|
| 72 |
-
|
| 73 |
def generate_suggestions(caption):
|
| 74 |
prompt = (
|
| 75 |
f"Caption: {caption}\n"
|
| 76 |
-
"Suggest five distinct improvements as bullet points. "
|
| 77 |
-
"Each line must start with '- '."
|
| 78 |
)
|
| 79 |
raw = suggestion_generator(prompt)[0]["generated_text"].strip()
|
| 80 |
lines = [l for l in raw.splitlines() if l.strip().startswith('- ')]
|
| 81 |
if len(lines) < 5:
|
| 82 |
all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
|
| 83 |
-
lines = [
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
return "\n".join(lines[:5])
|
| 86 |
|
| 87 |
# Orchestrator: process image through all steps
|
| 88 |
-
|
| 89 |
def process(image):
|
| 90 |
caption = generate_caption(image)
|
| 91 |
category = generate_category(caption)
|
|
@@ -98,8 +94,7 @@ def process(image):
|
|
| 98 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 99 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 100 |
gr.Markdown(
|
| 101 |
-
"Upload an image ad to see: an Ad Category, a five-sentence Analysis, "
|
| 102 |
-
"five bullet-point Suggestions, and Example Ads."
|
| 103 |
)
|
| 104 |
|
| 105 |
with gr.Row():
|
|
@@ -121,4 +116,4 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 121 |
gr.Markdown("Made by Simon Thalmay")
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import re
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
|
|
|
| 30 |
suggestion_generator = make_pipeline("google/flan-t5-small", 500)
|
| 31 |
|
| 32 |
# Hardcoded example ads for gallery
|
|
|
|
| 33 |
def get_recommendations():
|
| 34 |
return [
|
| 35 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
|
|
| 45 |
]
|
| 46 |
|
| 47 |
# Step A: Use OCRFlux to generate a detailed caption
|
|
|
|
| 48 |
def generate_caption(image):
|
| 49 |
result = image_to_text(image)
|
| 50 |
+
return result[0]["generated_text"].strip()
|
|
|
|
| 51 |
|
| 52 |
# Step B: Flan interprets caption into concise category
|
|
|
|
| 53 |
def generate_category(caption):
|
| 54 |
prompt = f"Caption: {caption}\nProvide a concise category label for this ad."
|
| 55 |
raw = category_generator(prompt)[0]["generated_text"].strip()
|
| 56 |
return raw.splitlines()[0]
|
| 57 |
|
| 58 |
# Step C: Flan produces exactly five-sentence analysis
|
|
|
|
| 59 |
def generate_analysis(caption):
|
| 60 |
prompt = (
|
| 61 |
f"Caption: {caption}\n"
|
|
|
|
| 66 |
return " ".join(sentences[:5])
|
| 67 |
|
| 68 |
# Step D: Flan suggests five actionable bullet-point improvements
|
|
|
|
| 69 |
def generate_suggestions(caption):
|
| 70 |
prompt = (
|
| 71 |
f"Caption: {caption}\n"
|
| 72 |
+
"Suggest five distinct improvements as bullet points. Each line must start with '- '."
|
|
|
|
| 73 |
)
|
| 74 |
raw = suggestion_generator(prompt)[0]["generated_text"].strip()
|
| 75 |
lines = [l for l in raw.splitlines() if l.strip().startswith('- ')]
|
| 76 |
if len(lines) < 5:
|
| 77 |
all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
|
| 78 |
+
lines = [
|
| 79 |
+
('- ' + all_lines[i]) if not all_lines[i].startswith('- ') else all_lines[i]
|
| 80 |
+
for i in range(min(5, len(all_lines)))
|
| 81 |
+
]
|
| 82 |
return "\n".join(lines[:5])
|
| 83 |
|
| 84 |
# Orchestrator: process image through all steps
|
|
|
|
| 85 |
def process(image):
|
| 86 |
caption = generate_caption(image)
|
| 87 |
category = generate_category(caption)
|
|
|
|
| 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, five bullet-point Suggestions, and Example Ads."
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
with gr.Row():
|
|
|
|
| 116 |
gr.Markdown("Made by Simon Thalmay")
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
+
demo.launch()
|