Tulitula commited on
Commit
1bc4d86
·
verified ·
1 Parent(s): 567e198

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -27
app.py CHANGED
@@ -10,9 +10,10 @@ from transformers import (
10
  AutoModelForSeq2SeqLM,
11
  )
12
 
 
13
  DEVICE = 0 if torch.cuda.is_available() else -1
14
 
15
- # BLIP
16
  processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
17
  blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
18
  caption_pipe = pipeline(
@@ -23,6 +24,7 @@ caption_pipe = pipeline(
23
  device=DEVICE,
24
  )
25
 
 
26
  FLAN_MODEL = "google/flan-t5-large"
27
  flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
28
  flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
@@ -36,6 +38,7 @@ category_pipe = pipeline(
36
  do_sample=True,
37
  temperature=1.0,
38
  )
 
39
  analysis_pipe = pipeline(
40
  "text2text-generation",
41
  model=flan_model,
@@ -45,6 +48,7 @@ analysis_pipe = pipeline(
45
  do_sample=True,
46
  temperature=1.0,
47
  )
 
48
  suggestion_pipe = pipeline(
49
  "text2text-generation",
50
  model=flan_model,
@@ -54,6 +58,7 @@ suggestion_pipe = pipeline(
54
  do_sample=True,
55
  temperature=1.0,
56
  )
 
57
  expansion_pipe = pipeline(
58
  "text2text-generation",
59
  model=flan_model,
@@ -80,18 +85,26 @@ def get_recommendations():
80
  def process(image: Image):
81
  if image is None:
82
  return "", "", "", "", get_recommendations()
 
 
83
  caption_res = caption_pipe(image, max_new_tokens=64)
84
  raw_caption = caption_res[0]["generated_text"].strip()
 
 
85
  if len(raw_caption.split()) < 3:
86
  exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
87
  desc = exp[0]["generated_text"].strip()
88
  else:
89
  desc = raw_caption
 
 
90
  cat_prompt = (
91
  f"Description: {desc}\n\n"
92
  "Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
93
  )
94
  cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
 
 
95
  ana_prompt = (
96
  f"Description: {desc}\n\n"
97
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
@@ -99,67 +112,68 @@ def process(image: Image):
99
  ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
100
  sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
101
  analysis = " ".join(sentences[:5])
 
 
102
  sug_prompt = (
103
  f"Description: {desc}\n\n"
104
- "Suggest five **different** and actionable improvements for this ad. "
105
- "Each must start with '- ' and be a single sentence. "
106
- "Avoid repeating any idea or wording."
107
  )
108
  sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
109
- seen = set()
110
  bullets = []
 
111
  for l in sug_raw.splitlines():
112
- if l.startswith("-"):
113
- key = l[2:].strip().lower()
114
- if key and key not in seen:
 
 
115
  seen.add(key)
116
- bullets.append(l.strip())
117
  if len(bullets) == 5:
118
  break
 
119
  fallback = [
120
  "- Add a bold and visible call-to-action button.",
121
  "- Use brighter colors or higher contrast for more visual impact.",
122
  "- Refine the text for greater clarity and conciseness.",
123
  "- Adjust the image layout for better balance and focus.",
124
- "- Highlight product benefits more clearly in the headline.",
125
  ]
126
  for fb in fallback:
127
  if len(bullets) == 5:
128
  break
129
- fb_key = fb[2:].strip().lower()
130
  if fb_key not in seen:
131
  bullets.append(fb)
132
  seen.add(fb_key)
133
  suggestions = "\n".join(bullets[:5])
134
- return "", cat_out, analysis, suggestions, get_recommendations()
 
135
 
136
  def main():
137
  with gr.Blocks(title="Smart Ad Analyzer") as demo:
138
- gr.Markdown("# Smart Ad Analyzer")
139
  gr.Markdown(
140
- """
141
- Upload an ad image and get AI-powered creative feedback instantly:
142
- - **Ad Category** (concise and relevant)
143
- - **Five-sentence Analysis** (ad message, design, impact)
144
- - **Five unique Improvement Suggestions**
145
- - **Example Ads** for inspiration
146
-
147
- Get quick, actionable advice for better ads—no creative block, no guesswork.
148
- """
149
  )
150
  with gr.Row():
151
  inp = gr.Image(type='pil', label='Upload Ad Image')
152
  with gr.Column():
153
- # BLIP caption hidden!
154
- cat_out = gr.Textbox(label='📂 Ad Category', interactive=False)
155
- ana_out = gr.Textbox(label='📊 Ad Analysis', lines=5, interactive=False)
156
- sug_out = gr.Textbox(label='🚀 Improvement Suggestions', lines=8, interactive=False)
 
157
  btn = gr.Button('Analyze Ad', variant='primary')
158
  gallery = gr.Gallery(label='Example Ads')
159
  btn.click(
160
  fn=process,
161
  inputs=[inp],
162
- outputs=[cat_out, ana_out, sug_out, gallery],
163
  )
164
  gr.Markdown('Made by Simon Thalmay')
165
  return demo
 
10
  AutoModelForSeq2SeqLM,
11
  )
12
 
13
+ # Auto-detect CPU/GPU
14
  DEVICE = 0 if torch.cuda.is_available() else -1
15
 
16
+ # BLIP captioner
17
  processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
18
  blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
19
  caption_pipe = pipeline(
 
24
  device=DEVICE,
25
  )
26
 
27
+ # FLAN-T5 for text-to-text
28
  FLAN_MODEL = "google/flan-t5-large"
29
  flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
30
  flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
 
38
  do_sample=True,
39
  temperature=1.0,
40
  )
41
+
42
  analysis_pipe = pipeline(
43
  "text2text-generation",
44
  model=flan_model,
 
48
  do_sample=True,
49
  temperature=1.0,
50
  )
51
+
52
  suggestion_pipe = pipeline(
53
  "text2text-generation",
54
  model=flan_model,
 
58
  do_sample=True,
59
  temperature=1.0,
60
  )
61
+
62
  expansion_pipe = pipeline(
63
  "text2text-generation",
64
  model=flan_model,
 
85
  def process(image: Image):
86
  if image is None:
87
  return "", "", "", "", get_recommendations()
88
+
89
+ # BLIP caption
90
  caption_res = caption_pipe(image, max_new_tokens=64)
91
  raw_caption = caption_res[0]["generated_text"].strip()
92
+
93
+ # Expand if too short
94
  if len(raw_caption.split()) < 3:
95
  exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
96
  desc = exp[0]["generated_text"].strip()
97
  else:
98
  desc = raw_caption
99
+
100
+ # Category
101
  cat_prompt = (
102
  f"Description: {desc}\n\n"
103
  "Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
104
  )
105
  cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
106
+
107
+ # Five-sentence analysis
108
  ana_prompt = (
109
  f"Description: {desc}\n\n"
110
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
 
112
  ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
113
  sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
114
  analysis = " ".join(sentences[:5])
115
+
116
+ # Five bullet-point suggestions - unique, high-quality
117
  sug_prompt = (
118
  f"Description: {desc}\n\n"
119
+ "Provide five distinct improvement suggestions for this ad. Each must start with '- ', be one sentence, and not repeat the same idea."
 
 
120
  )
121
  sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
 
122
  bullets = []
123
+ seen = set()
124
  for l in sug_raw.splitlines():
125
+ line = l.strip()
126
+ if line.startswith("-"):
127
+ key = line[2:].lower()
128
+ if key not in seen and len(key) > 5:
129
+ bullets.append(line)
130
  seen.add(key)
 
131
  if len(bullets) == 5:
132
  break
133
+ # Fallbacks
134
  fallback = [
135
  "- Add a bold and visible call-to-action button.",
136
  "- Use brighter colors or higher contrast for more visual impact.",
137
  "- Refine the text for greater clarity and conciseness.",
138
  "- Adjust the image layout for better balance and focus.",
139
+ "- Highlight product benefits more clearly in the headline."
140
  ]
141
  for fb in fallback:
142
  if len(bullets) == 5:
143
  break
144
+ fb_key = fb[2:].lower()
145
  if fb_key not in seen:
146
  bullets.append(fb)
147
  seen.add(fb_key)
148
  suggestions = "\n".join(bullets[:5])
149
+
150
+ return "", cat_out, analysis, suggestions, get_recommendations() # Hides BLIP Caption by returning blank
151
 
152
  def main():
153
  with gr.Blocks(title="Smart Ad Analyzer") as demo:
 
154
  gr.Markdown(
155
+ "## Smart Ad Analyzer\n"
156
+ "Upload any advertisement image below and get an instant breakdown:\n\n"
157
+ "- **Ad Category:** Instantly identifies what sector your ad fits into\n"
158
+ "- **Ad Analysis:** Five concise sentences explaining what your ad communicates and the emotional response it targets\n"
159
+ "- **Improvement Suggestions:** Five practical, unique tips to make your ad more effective\n"
160
+ "- **Example Ads Gallery:** See proven ad designs for inspiration\n\n"
161
+ "_Ideal for marketers, business owners, students, and creative teams who want to boost ad impact using AI. No technical skill required._"
 
 
162
  )
163
  with gr.Row():
164
  inp = gr.Image(type='pil', label='Upload Ad Image')
165
  with gr.Column():
166
+ # To hide BLIP caption, don't add the textbox output
167
+ # cap_out = gr.Textbox(label=' BLIP Caption', interactive=False, visible=False)
168
+ cat_out = gr.Textbox(label=' Ad Category', interactive=False)
169
+ ana_out = gr.Textbox(label=' Ad Analysis', lines=5, interactive=False)
170
+ sug_out = gr.Textbox(label=' Improvement Suggestions', lines=5, interactive=False)
171
  btn = gr.Button('Analyze Ad', variant='primary')
172
  gallery = gr.Gallery(label='Example Ads')
173
  btn.click(
174
  fn=process,
175
  inputs=[inp],
176
+ outputs=["textbox", cat_out, ana_out, sug_out, gallery], # BLIP output is blank, textbox hidden
177
  )
178
  gr.Markdown('Made by Simon Thalmay')
179
  return demo