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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -49
app.py CHANGED
@@ -12,7 +12,7 @@ from transformers import (
12
 
13
  DEVICE = 0 if torch.cuda.is_available() else -1
14
 
15
- # BLIP captioner
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,7 +23,6 @@ caption_pipe = pipeline(
23
  device=DEVICE,
24
  )
25
 
26
- # FLAN-T5
27
  FLAN_MODEL = "google/flan-t5-large"
28
  flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
29
  flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
@@ -37,7 +36,6 @@ category_pipe = pipeline(
37
  do_sample=True,
38
  temperature=1.0,
39
  )
40
-
41
  analysis_pipe = pipeline(
42
  "text2text-generation",
43
  model=flan_model,
@@ -47,7 +45,6 @@ analysis_pipe = pipeline(
47
  do_sample=True,
48
  temperature=1.0,
49
  )
50
-
51
  suggestion_pipe = pipeline(
52
  "text2text-generation",
53
  model=flan_model,
@@ -57,7 +54,6 @@ suggestion_pipe = pipeline(
57
  do_sample=True,
58
  temperature=1.0,
59
  )
60
-
61
  expansion_pipe = pipeline(
62
  "text2text-generation",
63
  model=flan_model,
@@ -83,27 +79,19 @@ def get_recommendations():
83
 
84
  def process(image: Image):
85
  if image is None:
86
- return "", "", "", get_recommendations()
87
-
88
- # BLIP caption
89
  caption_res = caption_pipe(image, max_new_tokens=64)
90
  raw_caption = caption_res[0]["generated_text"].strip()
91
-
92
- # Expand if too short
93
  if len(raw_caption.split()) < 3:
94
  exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
95
  desc = exp[0]["generated_text"].strip()
96
  else:
97
  desc = raw_caption
98
-
99
- # Category
100
  cat_prompt = (
101
  f"Description: {desc}\n\n"
102
  "Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
103
  )
104
  cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
105
-
106
- # Five-sentence analysis
107
  ana_prompt = (
108
  f"Description: {desc}\n\n"
109
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
@@ -111,57 +99,58 @@ def process(image: Image):
111
  ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
112
  sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
113
  analysis = " ".join(sentences[:5])
114
-
115
- # Five bullet-point suggestions, improved filtering
116
  sug_prompt = (
117
  f"Description: {desc}\n\n"
118
- "Provide five unique, actionable improvement suggestions for this ad. "
119
- "Each suggestion must begin with '- ' and each one should cover a different topic, such as clarity, visual design, call-to-action, audience targeting, or emotional impact. "
120
- "Do not repeat ideas or use generic language. Each suggestion should be specific and creative."
121
  )
122
  sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
123
- lines = [l.strip() for l in sug_raw.splitlines() if l.strip().startswith("-")]
124
- used = set()
125
- unique_lines = []
126
- for l in lines:
127
- key = l.lower().strip("- .")
128
- if key not in used and len(unique_lines) < 5:
129
- unique_lines.append(l)
130
- used.add(key)
131
- fallbacks = [
132
- "- Use a bolder headline or color contrast to capture attention.",
133
- "- Make the product or offer more visually prominent.",
134
- "- Add a clearer call to action, such as 'Order Now' or 'Learn More'.",
135
- "- Tailor the messaging to a more specific audience segment.",
136
- "- Add emotional or humorous elements to boost engagement."
 
 
137
  ]
138
- for fb in fallbacks:
139
- if len(unique_lines) >= 5:
140
  break
141
- if fb.lower() not in used:
142
- unique_lines.append(fb)
143
- used.add(fb.lower())
144
- suggestions = "\n".join(unique_lines[:5])
145
-
146
- return cat_out, analysis, suggestions, get_recommendations()
147
 
148
  def main():
149
  with gr.Blocks(title="Smart Ad Analyzer") as demo:
150
- gr.Markdown("# πŸ“’ Smart Ad Analyzer")
151
  gr.Markdown(
152
  """
153
- Upload an ad image to instantly get:
154
- - **Ad Category**: A clear and relevant label for the type of ad.
155
- - **Five-sentence Analysis**: A comprehensive explanation of the ad's message, design, and emotional impact.
156
- - **Five Improvement Suggestions**: Specific, unique tips to boost ad performance.
157
- - **Example Ads**: Inspiring ad visuals for creative reference.
158
-
159
- This tool helps marketers, designers, and founders strengthen any ad campaign in seconds.
160
  """
161
  )
162
  with gr.Row():
163
  inp = gr.Image(type='pil', label='Upload Ad Image')
164
  with gr.Column():
 
165
  cat_out = gr.Textbox(label='πŸ“‚ Ad Category', interactive=False)
166
  ana_out = gr.Textbox(label='πŸ“Š Ad Analysis', lines=5, interactive=False)
167
  sug_out = gr.Textbox(label='πŸš€ Improvement Suggestions', lines=8, interactive=False)
 
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
  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
  do_sample=True,
37
  temperature=1.0,
38
  )
 
39
  analysis_pipe = pipeline(
40
  "text2text-generation",
41
  model=flan_model,
 
45
  do_sample=True,
46
  temperature=1.0,
47
  )
 
48
  suggestion_pipe = pipeline(
49
  "text2text-generation",
50
  model=flan_model,
 
54
  do_sample=True,
55
  temperature=1.0,
56
  )
 
57
  expansion_pipe = pipeline(
58
  "text2text-generation",
59
  model=flan_model,
 
79
 
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
  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)