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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -13
app.py CHANGED
@@ -12,7 +12,7 @@ caption_pipe = pipeline(
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",
@@ -38,7 +38,16 @@ suggestion_pipe = pipeline(
38
  do_sample=True,
39
  temperature=1.0,
40
  )
 
 
 
 
 
 
 
 
41
 
 
42
  def get_recommendations():
43
  return [
44
  "https://i.imgur.com/InC88PP.jpeg",
@@ -46,38 +55,49 @@ def get_recommendations():
46
  "https://i.imgur.com/wp3Wzc4.jpeg",
47
  "https://i.imgur.com/5e2xOA4.jpeg",
48
  "https://i.imgur.com/txjRk98.jpeg",
 
 
 
 
 
49
  ]
50
 
 
51
  def process(image: Image):
52
  # 1) BLIP caption
53
  caption = caption_pipe(image)[0]["generated_text"].strip()
54
 
55
- # 2) Single‐label category
 
 
 
 
 
 
 
56
  cat_prompt = (
57
- f"Caption: {caption}\n\n"
58
- "Give me one concise category label for this ad (e.g. 'Fitness', 'Food'):"
59
  )
60
  category = category_pipe(cat_prompt)[0]["generated_text"].strip().splitlines()[0]
61
 
62
- # 3) Fivesentence analysis
63
  ana_prompt = (
64
- f"Caption: {caption}\n\n"
65
  "Write exactly five sentences explaining what this ad communicates and its emotional impact."
66
  )
67
  raw_ana = analysis_pipe(ana_prompt)[0]["generated_text"].strip()
68
- # ensure exactly five sentences
69
  sents = re.split(r'(?<=[.!?])\s+', raw_ana)
70
  analysis = " ".join(sents[:5])
71
 
72
- # 4) Five bulletpoint suggestions
73
  sug_prompt = (
74
- f"Caption: {caption}\n\n"
75
  "Suggest five distinct improvements for this ad. "
76
  "Each suggestion must start with '- ' and be one sentence."
77
  )
78
  raw_sug = suggestion_pipe(sug_prompt)[0]["generated_text"].strip()
79
  bullets = [l for l in raw_sug.splitlines() if l.strip().startswith("-")]
80
- # pad/truncate to 5
81
  if len(bullets) < 5:
82
  extras = [l.strip() for l in raw_sug.splitlines() if l.strip()]
83
  for ex in extras:
@@ -93,7 +113,7 @@ with gr.Blocks() as demo:
93
  gr.Markdown(
94
  "Upload an image ad to get:\n"
95
  "- **BLIP Caption** (debug)\n"
96
- "- **Category**\n"
97
  "- **Five-sentence Analysis**\n"
98
  "- **Five bullet-point Suggestions**\n"
99
  "- **Example Ads**"
@@ -108,7 +128,7 @@ with gr.Blocks() as demo:
108
  sug_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
109
  btn = gr.Button("Analyze Ad")
110
 
111
- gallery = gr.Gallery(label="Example Ads").style(grid=[5], height="auto")
112
 
113
  btn.click(
114
  fn=process,
@@ -119,4 +139,4 @@ with gr.Blocks() as demo:
119
  gr.Markdown("Made by Simon Thalmay")
120
 
121
  if __name__ == "__main__":
122
- demo.launch()
 
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",
 
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
+ )
49
 
50
+ # Example gallery helper returns 10 example ad URLs
51
  def get_recommendations():
52
  return [
53
  "https://i.imgur.com/InC88PP.jpeg",
 
55
  "https://i.imgur.com/wp3Wzc4.jpeg",
56
  "https://i.imgur.com/5e2xOA4.jpeg",
57
  "https://i.imgur.com/txjRk98.jpeg",
58
+ "https://i.imgur.com/rQ4AYl0.jpeg",
59
+ "https://i.imgur.com/bDzwD04.jpeg",
60
+ "https://i.imgur.com/fLMngXI.jpeg",
61
+ "https://i.imgur.com/nYEJzxt.png",
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:
 
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**"
 
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,
 
139
  gr.Markdown("Made by Simon Thalmay")
140
 
141
  if __name__ == "__main__":
142
+ demo.launch()