Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,22 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import re
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
pipe = pipeline(
|
| 10 |
task="image-text-to-text",
|
| 11 |
-
model=
|
| 12 |
-
|
|
|
|
| 13 |
max_new_tokens=500,
|
| 14 |
do_sample=True,
|
| 15 |
temperature=1.0,
|
|
@@ -17,7 +24,6 @@ pipe = pipeline(
|
|
| 17 |
top_p=0.9,
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Hard-coded gallery URLs
|
| 21 |
def get_recommendations():
|
| 22 |
return [
|
| 23 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
@@ -45,27 +51,25 @@ def process(image: Image):
|
|
| 45 |
"- <bullet 5>\n"
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
result = pipe(image, prompt=prompt)[0]["generated_text"]
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
cat = re.search(r"Category:(.*?)Analysis:",
|
| 53 |
-
ana = re.search(r"Analysis:(.*?)Suggestions:",
|
| 54 |
-
sug = re.search(r"Suggestions:(.*)",
|
| 55 |
|
| 56 |
category = cat.group(1).strip() if cat else ""
|
| 57 |
analysis = ana.group(1).strip() if ana else ""
|
| 58 |
suggestions = sug.group(1).strip() if sug else ""
|
| 59 |
|
| 60 |
-
# ensure exactly
|
| 61 |
-
bullets = [
|
| 62 |
if len(bullets) < 5:
|
| 63 |
bullets += ["- (no suggestion)"] * (5 - len(bullets))
|
| 64 |
suggestions = "\n".join(bullets[:5])
|
| 65 |
|
| 66 |
return category, analysis, suggestions, get_recommendations()
|
| 67 |
|
| 68 |
-
# build the Gradio interface
|
| 69 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 70 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 71 |
gr.Markdown(
|
|
@@ -74,7 +78,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 74 |
)
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
-
|
| 78 |
with gr.Column():
|
| 79 |
cat_out = gr.Textbox(label="Ad Category", interactive=False)
|
| 80 |
ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
|
|
@@ -82,12 +86,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 82 |
btn = gr.Button("Analyze Ad", size="sm", variant="primary")
|
| 83 |
|
| 84 |
gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
|
| 85 |
-
|
| 86 |
-
btn.click(
|
| 87 |
-
fn=process,
|
| 88 |
-
inputs=[image_input],
|
| 89 |
-
outputs=[cat_out, ana_out, sug_out, gallery]
|
| 90 |
-
)
|
| 91 |
|
| 92 |
gr.Markdown("Made by Simon Thalmay")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoProcessor,
|
| 6 |
+
AutoModelForVision2Seq,
|
| 7 |
+
pipeline,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
# Load processor & model
|
| 11 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
| 12 |
+
model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
| 13 |
|
| 14 |
+
# Create the image-text-to-text pipeline
|
| 15 |
pipe = pipeline(
|
| 16 |
task="image-text-to-text",
|
| 17 |
+
model=model,
|
| 18 |
+
feature_extractor=processor.feature_extractor,
|
| 19 |
+
tokenizer=processor.tokenizer,
|
| 20 |
max_new_tokens=500,
|
| 21 |
do_sample=True,
|
| 22 |
temperature=1.0,
|
|
|
|
| 24 |
top_p=0.9,
|
| 25 |
)
|
| 26 |
|
|
|
|
| 27 |
def get_recommendations():
|
| 28 |
return [
|
| 29 |
"https://i.imgur.com/InC88PP.jpeg",
|
|
|
|
| 51 |
"- <bullet 5>\n"
|
| 52 |
)
|
| 53 |
|
| 54 |
+
out = pipe(image, prompt=prompt)[0]["generated_text"]
|
|
|
|
| 55 |
|
| 56 |
+
# pull out each section via regex
|
| 57 |
+
cat = re.search(r"Category:(.*?)Analysis:", out, re.S)
|
| 58 |
+
ana = re.search(r"Analysis:(.*?)Suggestions:", out, re.S)
|
| 59 |
+
sug = re.search(r"Suggestions:(.*)", out, re.S)
|
| 60 |
|
| 61 |
category = cat.group(1).strip() if cat else ""
|
| 62 |
analysis = ana.group(1).strip() if ana else ""
|
| 63 |
suggestions = sug.group(1).strip() if sug else ""
|
| 64 |
|
| 65 |
+
# ensure exactly 5 bullets
|
| 66 |
+
bullets = [l for l in suggestions.splitlines() if l.startswith("-")]
|
| 67 |
if len(bullets) < 5:
|
| 68 |
bullets += ["- (no suggestion)"] * (5 - len(bullets))
|
| 69 |
suggestions = "\n".join(bullets[:5])
|
| 70 |
|
| 71 |
return category, analysis, suggestions, get_recommendations()
|
| 72 |
|
|
|
|
| 73 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 74 |
gr.Markdown("## 📢 Smart Ad Analyzer")
|
| 75 |
gr.Markdown(
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
+
img = gr.Image(type="pil", label="Upload Ad Image")
|
| 82 |
with gr.Column():
|
| 83 |
cat_out = gr.Textbox(label="Ad Category", interactive=False)
|
| 84 |
ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
|
|
|
|
| 86 |
btn = gr.Button("Analyze Ad", size="sm", variant="primary")
|
| 87 |
|
| 88 |
gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
|
| 89 |
+
btn.click(fn=process, inputs=[img], outputs=[cat_out, ana_out, sug_out, gallery])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
gr.Markdown("Made by Simon Thalmay")
|
| 92 |
|