Tulitula commited on
Commit
2205c78
Β·
verified Β·
1 Parent(s): da115a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
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
-
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 +24,7 @@ caption_pipe = pipeline(
23
  device=DEVICE,
24
  )
25
 
26
-
27
  FLAN_MODEL = "google/flan-t5-large"
28
  flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
29
  flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
@@ -68,7 +69,7 @@ expansion_pipe = pipeline(
68
  )
69
 
70
  def get_recommendations():
71
-
72
  return [
73
  "https://i.imgur.com/InC88PP.jpeg",
74
  "https://i.imgur.com/7BHfv4T.png",
@@ -86,25 +87,25 @@ def process(image: Image):
86
  if image is None:
87
  return "", "", "", get_recommendations()
88
 
89
-
90
  caption_res = caption_pipe(image, max_new_tokens=64)
91
  raw_caption = caption_res[0]["generated_text"].strip()
92
 
93
-
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
-
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
-
108
  ana_prompt = (
109
  f"Description: {desc}\n\n"
110
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
@@ -113,7 +114,7 @@ def process(image: Image):
113
  sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
114
  analysis = " ".join(sentences[:5])
115
 
116
-
117
  sug_prompt = (
118
  f"Description: {desc}\n\n"
119
  "Suggest five unique, practical improvements for this ad. Each must address a different aspect (message, visuals, call-to-action, targeting, layout, or design). Each suggestion must be one sentence and start with '- '. Do NOT repeat suggestions."
@@ -135,7 +136,7 @@ def process(image: Image):
135
  seen.add(suggestion)
136
  if len(bullets) == 5:
137
  break
138
-
139
  defaults = [
140
  "- Make the main headline more eye-catching.",
141
  "- Add a clear and visible call-to-action button.",
@@ -158,10 +159,10 @@ def main():
158
  **Upload your ad image below and instantly get expert feedback.**
159
 
160
  This AI tool will analyze your ad and provide:
161
- - **Category** β€” What type of ad is this?
162
- - **In-depth Analysis** β€” Five detailed sentences covering message, visuals, emotional impact, and more.
163
- - **Improvement Suggestions** β€” Five actionable, unique ways to make your ad better.
164
- - **Inspiration Gallery** β€” See other effective ads for ideas.
165
 
166
  Perfect for marketers, founders, designers, and anyone looking to boost ad performance with actionable insights!
167
  """
@@ -169,9 +170,9 @@ def main():
169
  with gr.Row():
170
  inp = gr.Image(type='pil', label='Upload Ad Image')
171
  with gr.Column():
172
- cat_out = gr.Textbox(label=' Ad Category', interactive=False)
173
- ana_out = gr.Textbox(label=' Ad Analysis', lines=5, interactive=False)
174
- sug_out = gr.Textbox(label=' Improvement Suggestions', lines=5, interactive=False)
175
  btn = gr.Button('Analyze Ad', variant='primary')
176
  gallery = gr.Gallery(label='Example Ads')
177
  btn.click(
 
10
  AutoModelForSeq2SeqLM,
11
  )
12
 
13
+ # Auto-detect CPU/GPU
14
  DEVICE = 0 if torch.cuda.is_available() else -1
15
 
16
+ # Load BLIP captioning model
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
+ # Load 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)
 
69
  )
70
 
71
  def get_recommendations():
72
+ # Returns list of 10 example ad image URLs
73
  return [
74
  "https://i.imgur.com/InC88PP.jpeg",
75
  "https://i.imgur.com/7BHfv4T.png",
 
87
  if image is None:
88
  return "", "", "", get_recommendations()
89
 
90
+ # 1. BLIP caption
91
  caption_res = caption_pipe(image, max_new_tokens=64)
92
  raw_caption = caption_res[0]["generated_text"].strip()
93
 
94
+ # 1a. Expand caption if too short
95
  if len(raw_caption.split()) < 3:
96
  exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
97
  desc = exp[0]["generated_text"].strip()
98
  else:
99
  desc = raw_caption
100
 
101
+ # 2. Category
102
  cat_prompt = (
103
  f"Description: {desc}\n\n"
104
  "Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
105
  )
106
  cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
107
 
108
+ # 3. Five-sentence analysis
109
  ana_prompt = (
110
  f"Description: {desc}\n\n"
111
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
 
114
  sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
115
  analysis = " ".join(sentences[:5])
116
 
117
+ # 4. Five bullet-point suggestions (unique only)
118
  sug_prompt = (
119
  f"Description: {desc}\n\n"
120
  "Suggest five unique, practical improvements for this ad. Each must address a different aspect (message, visuals, call-to-action, targeting, layout, or design). Each suggestion must be one sentence and start with '- '. Do NOT repeat suggestions."
 
136
  seen.add(suggestion)
137
  if len(bullets) == 5:
138
  break
139
+ # Add non-repetitive defaults if needed
140
  defaults = [
141
  "- Make the main headline more eye-catching.",
142
  "- Add a clear and visible call-to-action button.",
 
159
  **Upload your ad image below and instantly get expert feedback.**
160
 
161
  This AI tool will analyze your ad and provide:
162
+ - πŸ“‚ **Category** β€” What type of ad is this?
163
+ - πŸ“Š **In-depth Analysis** β€” Five detailed sentences covering message, visuals, emotional impact, and more.
164
+ - πŸš€ **Improvement Suggestions** β€” Five actionable, unique ways to make your ad better.
165
+ - πŸ“Έ **Inspiration Gallery** β€” See other effective ads for ideas.
166
 
167
  Perfect for marketers, founders, designers, and anyone looking to boost ad performance with actionable insights!
168
  """
 
170
  with gr.Row():
171
  inp = gr.Image(type='pil', label='Upload Ad Image')
172
  with gr.Column():
173
+ cat_out = gr.Textbox(label='πŸ“‚ Ad Category', interactive=False)
174
+ ana_out = gr.Textbox(label='πŸ“Š Ad Analysis', lines=5, interactive=False)
175
+ sug_out = gr.Textbox(label='πŸš€ Improvement Suggestions', lines=5, interactive=False)
176
  btn = gr.Button('Analyze Ad', variant='primary')
177
  gallery = gr.Gallery(label='Example Ads')
178
  btn.click(