Tulitula commited on
Commit
71dc617
·
verified ·
1 Parent(s): 21890e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -59
app.py CHANGED
@@ -1,91 +1,93 @@
 
 
1
  import re
2
  import gradio as gr
3
  from PIL import Image
4
- from transformers import AutoProcessor, AutoModelForVision2Seq, pipeline
5
-
6
- # 1) Load BLIP-2 processor & model
7
- processor = AutoProcessor.from_pretrained("Salesforce/blip2-flan-t5-xl")
8
- model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip2-flan-t5-xl")
9
-
10
- # 2) Build the multimodal pipeline correctly
11
- pipe = pipeline(
12
- "image-text-to-text",
13
- model=model,
14
- feature_extractor=processor.image_processor, # BLIP2Processor uses .image_processor
15
- tokenizer=processor.tokenizer,
16
- max_new_tokens=500,
17
- do_sample=True,
18
- temperature=1.0,
19
- top_k=50,
20
- top_p=0.9,
21
  )
22
 
23
- def get_recommendations():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  return [
25
  "https://i.imgur.com/InC88PP.jpeg",
26
  "https://i.imgur.com/7BHfv4T.png",
27
  "https://i.imgur.com/wp3Wzc4.jpeg",
28
  "https://i.imgur.com/5e2xOA4.jpeg",
29
  "https://i.imgur.com/txjRk98.jpeg",
30
- "https://i.imgur.com/rQ4AYl0.jpeg",
31
- "https://i.imgur.com/bDzwD04.jpeg",
32
- "https://i.imgur.com/fLMngXI.jpeg",
33
- "https://i.imgur.com/nYEJzxt.png",
34
- "https://i.imgur.com/Xj92Cjv.jpeg",
35
  ]
36
 
 
37
  def process(image: Image):
38
- # A single prompt that asks BLIP-2+Flan-T5 to emit exactly three sections
39
- prompt = (
40
- "You are an expert ad critic. Given the image below, output exactly three sections:\n\n"
41
- "Category: <one concise label>\n\n"
42
- "Analysis: <exactly five sentences explaining what the ad communicates and its emotional impact>\n\n"
43
- "Suggestions:\n"
44
- "- <bullet 1>\n"
45
- "- <bullet 2>\n"
46
- "- <bullet 3>\n"
47
- "- <bullet 4>\n"
48
- "- <bullet 5>\n"
49
- )
50
-
51
- # Run the pipeline
52
- out = pipe(image, prompt=prompt)[0]["generated_text"]
53
 
54
- # Regex-extract each section
55
- cat_match = re.search(r"Category:(.*?)Analysis:", out, re.S)
56
- ana_match = re.search(r"Analysis:(.*?)Suggestions:", out, re.S)
57
- sug_match = re.search(r"Suggestions:(.*)", out, re.S)
58
 
59
- category = cat_match.group(1).strip() if cat_match else ""
60
- analysis = ana_match.group(1).strip() if ana_match else ""
61
- suggestions = sug_match.group(1).strip() if sug_match else ""
 
 
 
62
 
63
- # Ensure exactly 5 bullets
64
- bullets = [line for line in suggestions.splitlines() if line.startswith("-")]
 
 
 
65
  if len(bullets) < 5:
66
- bullets += ["- (no suggestion)"] * (5 - len(bullets))
 
67
  suggestions = "\n".join(bullets[:5])
68
 
69
- return category, analysis, suggestions, get_recommendations()
70
 
71
- # --- Gradio UI ---
72
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
73
  gr.Markdown("## 📢 Smart Ad Analyzer")
74
  gr.Markdown(
75
- "Upload an image ad to see: an **Ad Category**, a **five-sentence Analysis**, "
76
- "**five bullet-point Suggestions**, and **Example Ads**."
77
  )
78
 
79
  with gr.Row():
80
- img = gr.Image(type="pil", label="Upload Ad Image")
81
  with gr.Column():
82
- cat_out = gr.Textbox(label="Ad Category", interactive=False)
83
- ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
84
- sug_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
85
- btn = gr.Button("Analyze Ad", size="sm", variant="primary")
86
 
87
- gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
88
- btn.click(fn=process, inputs=[img], outputs=[cat_out, ana_out, sug_out, gallery])
89
 
90
  gr.Markdown("Made by Simon Thalmay")
91
 
 
1
+ # app.py
2
+
3
  import re
4
  import gradio as gr
5
  from PIL import Image
6
+ from transformers import (
7
+ AutoProcessor,
8
+ AutoModelForVision2Seq,
9
+ pipeline,
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  )
11
 
12
+ # 1 – BLIP-large for image captioning
13
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
14
+ model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
15
+
16
+ def generate_caption(image: Image) -> str:
17
+ inputs = processor(images=image, return_tensors="pt")
18
+ outputs = model.generate(**inputs)
19
+ return processor.tokenizer.decode(outputs[0], skip_special_tokens=True)
20
+
21
+ # 2 – Flan-T5 pipelines
22
+ def make_pipe(model_name, max_tokens):
23
+ return pipeline(
24
+ "text2text-generation",
25
+ model=model_name,
26
+ tokenizer=model_name,
27
+ max_new_tokens=max_tokens,
28
+ do_sample=True,
29
+ temperature=1.0,
30
+ )
31
+
32
+ cat_pipe = make_pipe("google/flan-t5-small", 80)
33
+ ana_pipe = make_pipe("google/flan-t5-small", 200)
34
+ sug_pipe = make_pipe("google/flan-t5-small", 200)
35
+
36
+ # 3 – Recommendation gallery
37
+ def get_recs():
38
  return [
39
  "https://i.imgur.com/InC88PP.jpeg",
40
  "https://i.imgur.com/7BHfv4T.png",
41
  "https://i.imgur.com/wp3Wzc4.jpeg",
42
  "https://i.imgur.com/5e2xOA4.jpeg",
43
  "https://i.imgur.com/txjRk98.jpeg",
 
 
 
 
 
44
  ]
45
 
46
+ # 4 – Full workflow
47
  def process(image: Image):
48
+ caption = generate_caption(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ # category
51
+ raw_cat = cat_pipe(f"Caption: {caption}\nLabel this ad in one phrase:")[0]["generated_text"]
52
+ category = raw_cat.strip().splitlines()[0]
 
53
 
54
+ # analysis
55
+ raw_ana = ana_pipe(
56
+ f"Caption: {caption}\nWrite exactly five sentences explaining what this ad communicates and its emotional impact."
57
+ )[0]["generated_text"]
58
+ sentences = re.split(r'(?<=[.!?])\s+', raw_ana.strip())
59
+ analysis = " ".join(sentences[:5])
60
 
61
+ # suggestions
62
+ raw_sug = sug_pipe(
63
+ f"Caption: {caption}\nSuggest five distinct improvements as bullets, each starting with '- '."
64
+ )[0]["generated_text"]
65
+ bullets = [l for l in raw_sug.splitlines() if l.strip().startswith("-")]
66
  if len(bullets) < 5:
67
+ lines = [l.strip() for l in raw_sug.splitlines() if l.strip()]
68
+ bullets = [("- " + lines[i]) for i in range(min(5, len(lines)))]
69
  suggestions = "\n".join(bullets[:5])
70
 
71
+ return category, analysis, suggestions, get_recs()
72
 
73
+ # 5 Gradio UI
74
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
75
  gr.Markdown("## 📢 Smart Ad Analyzer")
76
  gr.Markdown(
77
+ "Upload an image ad to get: a Category, five-sentence Analysis, "
78
+ "five bullet-point Suggestions, and Example Ads."
79
  )
80
 
81
  with gr.Row():
82
+ inp = gr.Image(type="pil", label="Upload Ad Image")
83
  with gr.Column():
84
+ out_cat = gr.Textbox(label="Ad Category", interactive=False)
85
+ out_ana = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
86
+ out_sug = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
87
+ btn = gr.Button("Analyze Ad", size="sm")
88
 
89
+ gallery = gr.Gallery(label="Example Ads", show_label=True)
90
+ btn.click(process, inputs=[inp], outputs=[out_cat, out_ana, out_sug, gallery])
91
 
92
  gr.Markdown("Made by Simon Thalmay")
93