Tulitula commited on
Commit
0dc2d54
Β·
verified Β·
1 Parent(s): fed88b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -41
app.py CHANGED
@@ -10,10 +10,9 @@ from transformers import (
10
  AutoModelForSeq2SeqLM,
11
  )
12
 
13
- # Auto-detect CPU/GPU
14
  DEVICE = 0 if torch.cuda.is_available() else -1
15
 
16
- # 1) 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,7 +23,7 @@ caption_pipe = pipeline(
24
  device=DEVICE,
25
  )
26
 
27
- # 2) 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,7 +37,6 @@ category_pipe = pipeline(
38
  do_sample=True,
39
  temperature=1.0,
40
  )
41
-
42
  analysis_pipe = pipeline(
43
  "text2text-generation",
44
  model=flan_model,
@@ -48,7 +46,6 @@ analysis_pipe = pipeline(
48
  do_sample=True,
49
  temperature=1.0,
50
  )
51
-
52
  suggestion_pipe = pipeline(
53
  "text2text-generation",
54
  model=flan_model,
@@ -58,7 +55,6 @@ suggestion_pipe = pipeline(
58
  do_sample=True,
59
  temperature=1.0,
60
  )
61
-
62
  expansion_pipe = pipeline(
63
  "text2text-generation",
64
  model=flan_model,
@@ -68,7 +64,6 @@ expansion_pipe = pipeline(
68
  do_sample=False,
69
  )
70
 
71
- # Example gallery helper
72
  def get_recommendations():
73
  return [
74
  "https://i.imgur.com/InC88PP.jpeg",
@@ -83,74 +78,71 @@ def get_recommendations():
83
  "https://i.imgur.com/Xj92Cjv.jpeg",
84
  ]
85
 
86
- # Main processing function
87
-
88
  def process(image: Image):
89
  if image is None:
90
- return "", "", "", "", get_recommendations()
91
 
92
- # 1) BLIP caption
93
  caption_res = caption_pipe(image, max_new_tokens=64)
94
  raw_caption = caption_res[0]["generated_text"].strip()
95
-
96
- # 1a) Expand if too short
97
  if len(raw_caption.split()) < 3:
98
- exp = expansion_pipe(f"Expand into a detailed description: {raw_caption}")
99
- desc = exp[0]["generated_text"].strip()
100
  else:
101
  desc = raw_caption
102
 
103
- # 2) Category
104
  cat_prompt = (
105
  f"Description: {desc}\n\n"
106
- "Provide a concise category label for this ad (e.g. 'Food', 'Fitness'):"
107
  )
108
  cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
109
 
110
- # 3) Five-sentence analysis
111
  ana_prompt = (
112
  f"Description: {desc}\n\n"
113
- "Write exactly five sentences explaining what this ad communicates and its emotional impact."
114
  )
115
  ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
116
  sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
117
  analysis = " ".join(sentences[:5])
118
 
119
- # 4) Five bullet-point suggestions
120
  sug_prompt = (
121
  f"Description: {desc}\n\n"
122
- "Provide five distinct improvement suggestions, each starting with '- '."
123
  )
124
  sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
125
- bullets = [l for l in sug_raw.splitlines() if l.startswith("-")]
126
- if len(bullets) < 5:
127
- for line in sug_raw.splitlines():
128
- if len(bullets) >= 5:
129
- break
130
- if line and not line.startswith("-"):
131
- bullets.append("- " + line)
132
- while len(bullets) < 5:
133
- bullets.append("- Improve visual appeal and clarity.")
134
- suggestions = "\n".join(bullets[:5])
 
 
135
 
136
- return raw_caption, cat_out, analysis, suggestions, get_recommendations()
137
 
138
- # Gradio UI
139
  def main():
140
  with gr.Blocks(title="Smart Ad Analyzer") as demo:
141
  gr.Markdown("## πŸ“’ Smart Ad Analyzer")
142
  gr.Markdown(
143
- "Upload an ad image to get:\n"
144
- "- πŸ” **BLIP Caption**\n"
145
- "- πŸ“‚ **Ad Category**\n"
146
- "- πŸ“Š **Five-sentence Analysis**\n"
147
- "- πŸš€ **Five Improvement Suggestions**\n"
148
- "- πŸ“Έ **Example Ads**"
 
149
  )
150
  with gr.Row():
151
  inp = gr.Image(type='pil', label='Upload Ad Image')
152
  with gr.Column():
153
- cap_out = gr.Textbox(label='πŸ” BLIP Caption', interactive=False)
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=5, interactive=False)
@@ -159,7 +151,7 @@ def main():
159
  btn.click(
160
  fn=process,
161
  inputs=[inp],
162
- outputs=[cap_out, cat_out, ana_out, sug_out, gallery],
163
  )
164
  gr.Markdown('Made by Simon Thalmay')
165
  return demo
 
10
  AutoModelForSeq2SeqLM,
11
  )
12
 
 
13
  DEVICE = 0 if torch.cuda.is_available() else -1
14
 
15
+ # BLIP captioner setup
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-T5 setup
27
  FLAN_MODEL = "google/flan-t5-large"
28
  flan_tokenizer = AutoTokenizer.from_pretrained(FLAN_MODEL)
29
  flan_model = AutoModelForSeq2SeqLM.from_pretrained(FLAN_MODEL)
 
37
  do_sample=True,
38
  temperature=1.0,
39
  )
 
40
  analysis_pipe = pipeline(
41
  "text2text-generation",
42
  model=flan_model,
 
46
  do_sample=True,
47
  temperature=1.0,
48
  )
 
49
  suggestion_pipe = pipeline(
50
  "text2text-generation",
51
  model=flan_model,
 
55
  do_sample=True,
56
  temperature=1.0,
57
  )
 
58
  expansion_pipe = pipeline(
59
  "text2text-generation",
60
  model=flan_model,
 
64
  do_sample=False,
65
  )
66
 
 
67
  def get_recommendations():
68
  return [
69
  "https://i.imgur.com/InC88PP.jpeg",
 
78
  "https://i.imgur.com/Xj92Cjv.jpeg",
79
  ]
80
 
 
 
81
  def process(image: Image):
82
  if image is None:
83
+ return "", "", "", get_recommendations()
84
 
85
+ # Captioning with BLIP
86
  caption_res = caption_pipe(image, max_new_tokens=64)
87
  raw_caption = caption_res[0]["generated_text"].strip()
 
 
88
  if len(raw_caption.split()) < 3:
89
+ desc = expansion_pipe(f"Expand into a detailed description: {raw_caption}")[0]["generated_text"].strip()
 
90
  else:
91
  desc = raw_caption
92
 
93
+ # Ad Category
94
  cat_prompt = (
95
  f"Description: {desc}\n\n"
96
+ "Provide a concise, one or two-word category label for this ad (examples: 'Food', 'Fitness', 'Fashion', 'Tech'):"
97
  )
98
  cat_out = category_pipe(cat_prompt)[0]["generated_text"].splitlines()[0].strip()
99
 
100
+ # Analysis
101
  ana_prompt = (
102
  f"Description: {desc}\n\n"
103
+ "Write exactly five unique and meaningful sentences explaining what this ad communicates, its visual style, the target audience, the marketing message, and emotional appeal."
104
  )
105
  ana_raw = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
106
  sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
107
  analysis = " ".join(sentences[:5])
108
 
109
+ # Bullet Suggestions (enforced non-repetitive)
110
  sug_prompt = (
111
  f"Description: {desc}\n\n"
112
+ "Suggest five actionable and distinct ways this ad could be improved. Each improvement must start with '- ' and each one should address a different aspect such as clarity, visual design, call-to-action, message, or emotional engagement. No repeats or generic phrases."
113
  )
114
  sug_raw = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
115
+ bullets = [l for l in sug_raw.splitlines() if l.strip().startswith("- ")]
116
+ seen = set()
117
+ cleaned = []
118
+ for b in bullets:
119
+ text = b.strip().lower()
120
+ if text not in seen and len(cleaned) < 5:
121
+ cleaned.append(b)
122
+ seen.add(text)
123
+ while len(cleaned) < 5:
124
+ cleaned.append("- Add more product context or a stronger call to action.")
125
+
126
+ suggestions = "\n".join(cleaned[:5])
127
 
128
+ return cat_out, analysis, suggestions, get_recommendations()
129
 
 
130
  def main():
131
  with gr.Blocks(title="Smart Ad Analyzer") as demo:
132
  gr.Markdown("## πŸ“’ Smart Ad Analyzer")
133
  gr.Markdown(
134
+ "Welcome to Smart Ad Analyzer! \n\n"
135
+ "Upload any ad image and instantly receive:\n"
136
+ "- πŸ“‚ **Ad Category:** How would an AI classify this ad?\n"
137
+ "- πŸ“Š **In-depth Analysis:** Five unique sentences covering message, design, audience, and emotion.\n"
138
+ "- πŸš€ **Improvement Suggestions:** Five non-repetitive, actionable ideas to make your ad stronger.\n"
139
+ "- πŸ“Έ **Example Ads Gallery:** For creative inspiration.\n\n"
140
+ "Designed for marketers, designers, students, or anyone curious about effective advertising. No account or API needed."
141
  )
142
  with gr.Row():
143
  inp = gr.Image(type='pil', label='Upload Ad Image')
144
  with gr.Column():
145
+ # BLIP caption hidden from UI
146
  cat_out = gr.Textbox(label='πŸ“‚ Ad Category', interactive=False)
147
  ana_out = gr.Textbox(label='πŸ“Š Ad Analysis', lines=5, interactive=False)
148
  sug_out = gr.Textbox(label='πŸš€ Improvement Suggestions', lines=5, interactive=False)
 
151
  btn.click(
152
  fn=process,
153
  inputs=[inp],
154
+ outputs=[cat_out, ana_out, sug_out, gallery],
155
  )
156
  gr.Markdown('Made by Simon Thalmay')
157
  return demo