Tulitula commited on
Commit
3d1f9e0
·
verified ·
1 Parent(s): a8dedf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -64
app.py CHANGED
@@ -1,23 +1,29 @@
1
  import re
2
  import gradio as gr
 
3
  from PIL import Image
4
- from transformers import pipeline
5
 
6
- # 1) BLIP captioner (rich COCO captions)
 
 
 
 
 
7
  caption_pipe = pipeline(
8
- task="image-to-text",
9
- model="Salesforce/blip-image-captioning-large",
10
- device=-1, # force CPU
11
- max_length=64,
12
- do_sample=False,
13
  )
14
 
15
- # 2) Flan-T5 for texttotext
16
  FLAN = "google/flan-t5-large"
17
  category_pipe = pipeline(
18
  "text2text-generation",
19
  model=FLAN,
20
  tokenizer=FLAN,
 
21
  max_new_tokens=32,
22
  do_sample=True,
23
  temperature=1.0,
@@ -26,6 +32,7 @@ analysis_pipe = pipeline(
26
  "text2text-generation",
27
  model=FLAN,
28
  tokenizer=FLAN,
 
29
  max_new_tokens=256,
30
  do_sample=True,
31
  temperature=1.0,
@@ -34,15 +41,17 @@ suggestion_pipe = pipeline(
34
  "text2text-generation",
35
  model=FLAN,
36
  tokenizer=FLAN,
 
37
  max_new_tokens=256,
38
  do_sample=True,
39
  temperature=1.0,
40
  )
41
- # 3) Caption expander when BLIP is too short
42
  expansion_pipe = pipeline(
43
  "text2text-generation",
44
  model=FLAN,
45
  tokenizer=FLAN,
 
46
  max_new_tokens=128,
47
  do_sample=False,
48
  )
@@ -62,81 +71,84 @@ def get_recommendations():
62
  "https://i.imgur.com/Xj92Cjv.jpeg",
63
  ]
64
 
65
-
66
  def process(image: Image):
67
  # 1) BLIP caption
68
- caption = caption_pipe(image)[0]["generated_text"].strip()
69
 
70
- # 1a) Expand if too short
71
  if len(caption.split()) < 3:
72
- exp_prompt = f"Expand this into a detailed description: {caption}"
73
- desc = expansion_pipe(exp_prompt)[0]["generated_text"].strip()
74
  else:
75
  desc = caption
76
 
77
- # 2) Single‑label category
78
  cat_prompt = (
79
  f"Description: {desc}\n\n"
80
- "Provide a concise category label for this ad (e.g. 'Fitness', 'Food'):"
81
  )
82
- category = category_pipe(cat_prompt)[0]["generated_text"].strip().splitlines()[0]
83
 
84
- # 3) Fivesentence analysis
85
  ana_prompt = (
86
  f"Description: {desc}\n\n"
87
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
88
  )
89
- raw_ana = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
90
- sents = re.split(r'(?<=[.!?])\s+', raw_ana)
91
- analysis = " ".join(sents[:5])
92
 
93
- # 4) Five bulletpoint suggestions
94
  sug_prompt = (
95
  f"Description: {desc}\n\n"
96
- "Suggest five distinct improvements for this ad. "
97
- "Each suggestion must start with '- ' and be one sentence."
98
  )
99
- raw_sug = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
100
- bullets = [l for l in raw_sug.splitlines() if l.strip().startswith("-")]
101
  if len(bullets) < 5:
102
- extras = [l.strip() for l in raw_sug.splitlines() if l.strip()]
103
- for ex in extras:
104
- if len(bullets) >= 5: break
105
- line = ex if ex.startswith("-") else "- " + ex
106
- bullets.append(line)
107
- suggestions = "\n".join(bullets[:5])
108
 
109
  return caption, category, analysis, suggestions, get_recommendations()
110
 
111
- with gr.Blocks() as demo:
112
- gr.Markdown("## 📢 Smart Ad Analyzer")
113
- gr.Markdown(
114
- "Upload an image ad to get:\n"
115
- "- **BLIP Caption** (debug)\n"
116
- "- **Ad Category**\n"
117
- "- **Five-sentence Analysis**\n"
118
- "- **Five bullet-point Suggestions**\n"
119
- "- **Example Ads**"
120
- )
121
-
122
- with gr.Row():
123
- inp = gr.Image(type="pil", label="Upload Ad Image")
124
- with gr.Column():
125
- cap_out = gr.Textbox(label="🔍 BLIP Caption", interactive=False)
126
- cat_out = gr.Textbox(label="Ad Category", interactive=False)
127
- ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
128
- sug_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
129
- btn = gr.Button("Analyze Ad")
130
-
131
- gallery = gr.Gallery(label="Example Ads")
132
-
133
- btn.click(
134
- fn=process,
135
- inputs=[inp],
136
- outputs=[cap_out, cat_out, ana_out, sug_out, gallery],
137
- )
138
-
139
- gr.Markdown("Made by Simon Thalmay")
140
-
141
- if __name__ == "__main__":
142
- demo.launch()
 
 
 
 
 
 
1
  import re
2
  import gradio as gr
3
+ import torch
4
  from PIL import Image
5
+ from transformers import pipeline, AutoProcessor, AutoModelForVision2Seq
6
 
7
+ # Auto-detect CPU/GPU
8
+ device = 0 if torch.cuda.is_available() else -1
9
+
10
+ # 1) BLIP captioner
11
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
12
+ model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
13
  caption_pipe = pipeline(
14
+ "image-to-text",
15
+ model=model,
16
+ processor=processor,
17
+ device=device
 
18
  )
19
 
20
+ # 2) Flan-T5 for text-to-text
21
  FLAN = "google/flan-t5-large"
22
  category_pipe = pipeline(
23
  "text2text-generation",
24
  model=FLAN,
25
  tokenizer=FLAN,
26
+ device=device,
27
  max_new_tokens=32,
28
  do_sample=True,
29
  temperature=1.0,
 
32
  "text2text-generation",
33
  model=FLAN,
34
  tokenizer=FLAN,
35
+ device=device,
36
  max_new_tokens=256,
37
  do_sample=True,
38
  temperature=1.0,
 
41
  "text2text-generation",
42
  model=FLAN,
43
  tokenizer=FLAN,
44
+ device=device,
45
  max_new_tokens=256,
46
  do_sample=True,
47
  temperature=1.0,
48
  )
49
+ # Expander when BLIP caption is too short
50
  expansion_pipe = pipeline(
51
  "text2text-generation",
52
  model=FLAN,
53
  tokenizer=FLAN,
54
+ device=device,
55
  max_new_tokens=128,
56
  do_sample=False,
57
  )
 
71
  "https://i.imgur.com/Xj92Cjv.jpeg",
72
  ]
73
 
74
+ # Main processing function
75
  def process(image: Image):
76
  # 1) BLIP caption
77
+ caption = caption_pipe(image, max_new_tokens=64, do_sample=False)[0]['generated_text'].strip()
78
 
79
+ # 1a) Expand caption if too short
80
  if len(caption.split()) < 3:
81
+ desc = expansion_pipe(f"Expand into a detailed description: {caption}")[0]['generated_text'].strip()
 
82
  else:
83
  desc = caption
84
 
85
+ # 2) Ad category
86
  cat_prompt = (
87
  f"Description: {desc}\n\n"
88
+ "Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
89
  )
90
+ category = category_pipe(cat_prompt)[0]['generated_text'].splitlines()[0].strip()
91
 
92
+ # 3) Five-sentence analysis
93
  ana_prompt = (
94
  f"Description: {desc}\n\n"
95
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
96
  )
97
+ raw_ana = analysis_pipe(ana_prompt)[0]['generated_text'].strip()
98
+ sentences = re.split(r'(?<=[.!?])\s+', raw_ana)
99
+ analysis = " ".join(sentences[:5])
100
 
101
+ # 4) Five bullet-point suggestions
102
  sug_prompt = (
103
  f"Description: {desc}\n\n"
104
+ "Suggest five distinct improvements for this ad. Each must start with '- ' and be one sentence."
 
105
  )
106
+ raw_sug = suggestion_pipe(sug_prompt)[0]['generated_text'].strip()
107
+ bullets = [l for l in raw_sug.splitlines() if l.startswith('-')]
108
  if len(bullets) < 5:
109
+ extra_lines = [l for l in raw_sug.splitlines() if l.strip()]
110
+ for line in extra_lines:
111
+ if len(bullets) >= 5:
112
+ break
113
+ bullets.append(line if line.startswith('-') else '- ' + line)
114
+ suggestions = '\n'.join(bullets[:5])
115
 
116
  return caption, category, analysis, suggestions, get_recommendations()
117
 
118
+ # Gradio UI
119
+ def main():
120
+ with gr.Blocks() as demo:
121
+ gr.Markdown("## 📢 Smart Ad Analyzer")
122
+ gr.Markdown(
123
+ "Upload an image ad to get:\n"
124
+ "- **BLIP Caption** (raw)\n"
125
+ "- **Ad Category**\n"
126
+ "- **Five-sentence Analysis**\n"
127
+ "- **Five bullet-point Suggestions**\n"
128
+ "- **Example Ads**"
129
+ )
130
+
131
+ with gr.Row():
132
+ inp = gr.Image(type='pil', label='Upload Ad Image')
133
+ with gr.Column():
134
+ cap_out = gr.Textbox(label='🔍 BLIP Caption', interactive=False)
135
+ cat_out = gr.Textbox(label='Ad Category', interactive=False)
136
+ ana_out = gr.Textbox(label='Ad Analysis', lines=5, interactive=False)
137
+ sug_out = gr.Textbox(label='Improvement Suggestions', lines=5, interactive=False)
138
+ btn = gr.Button('Analyze Ad', size='sm', variant='primary')
139
+
140
+ gallery = gr.Gallery(label='Example Ads')
141
+
142
+ btn.click(
143
+ fn=process,
144
+ inputs=[inp],
145
+ outputs=[cap_out, cat_out, ana_out, sug_out, gallery],
146
+ )
147
+
148
+ gr.Markdown('Made by Simon Thalmay')
149
+
150
+ demo.launch()
151
+
152
+ if __name__ == '__main__':
153
+ main()
154
+