Tulitula commited on
Commit
5daac92
·
verified ·
1 Parent(s): 0a5ae92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -77
app.py CHANGED
@@ -3,40 +3,21 @@
3
  import re
4
  import gradio as gr
5
  from PIL import Image
6
- from transformers import (
7
- Blip2Processor,
8
- Blip2ForConditionalGeneration,
9
- pipeline
 
 
 
 
 
 
 
 
10
  )
11
 
12
- # 1) BLIP-2 for richer image captions
13
- processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
14
- model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
15
-
16
- def generate_caption(image: Image) -> str:
17
- inputs = processor(images=image, return_tensors="pt")
18
- outputs = model.generate(**inputs)
19
- return processor.decode(outputs[0], skip_special_tokens=True)
20
-
21
- # 2) Helper to build Flan-T5-small text pipelines (temperature=1.0)
22
- def make_pipeline(model_name: str, max_tokens: int):
23
- return pipeline(
24
- "text2text-generation",
25
- model=model_name,
26
- tokenizer=model_name,
27
- max_new_tokens=max_tokens,
28
- do_sample=True,
29
- temperature=1.0,
30
- top_k=50,
31
- top_p=0.95
32
- )
33
-
34
- # 3) Pipelines: category, analysis, suggestions
35
- category_generator = make_pipeline("google/flan-t5-small", 100)
36
- analysis_generator = make_pipeline("google/flan-t5-small", 500)
37
- suggestion_generator = make_pipeline("google/flan-t5-small", 500)
38
-
39
- # Hardcoded example ads for gallery
40
  def get_recommendations():
41
  return [
42
  "https://i.imgur.com/InC88PP.jpeg",
@@ -51,69 +32,60 @@ def get_recommendations():
51
  "https://i.imgur.com/Xj92Cjv.jpeg",
52
  ]
53
 
54
- # Step B: Flan interprets caption into concise category
55
- def generate_category(caption: str) -> str:
56
- prompt = f"Caption: {caption}\nProvide a concise category label for this ad."
57
- raw = category_generator(prompt)[0]["generated_text"].strip()
58
- return raw.splitlines()[0]
59
-
60
- # Step C: Flan produces exactly five-sentence analysis
61
- def generate_analysis(caption: str) -> str:
62
  prompt = (
63
- f"Caption: {caption}\n"
64
- "In exactly five sentences, explain what this ad communicates and its emotional impact."
 
 
 
 
 
 
 
65
  )
66
- raw = analysis_generator(prompt)[0]["generated_text"].strip()
67
- sentences = re.split(r'(?<=[.!?])\s+', raw)
68
- return " ".join(sentences[:5])
69
 
70
- # Step D: Flan suggests five actionable bullet-point improvements
71
- def generate_suggestions(caption: str) -> str:
72
- prompt = (
73
- f"Caption: {caption}\n"
74
- "Suggest five distinct improvements as bullet points. Each line must start with '- '."
75
- )
76
- raw = suggestion_generator(prompt)[0]["generated_text"].strip()
77
- lines = [l for l in raw.splitlines() if l.strip().startswith('- ')]
78
- if len(lines) < 5:
79
- all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
80
- lines = [
81
- ('- ' + all_lines[i]) if not all_lines[i].startswith('- ') else all_lines[i]
82
- for i in range(min(5, len(all_lines)))
83
- ]
84
- return "\n".join(lines[:5])
85
 
86
- # Orchestrator: process image through all steps
87
- def process(image: Image):
88
- caption = generate_caption(image)
89
- category = generate_category(caption)
90
- analysis = generate_analysis(caption)
91
- suggestions = generate_suggestions(caption)
92
- recs = get_recommendations()
93
- return category, analysis, suggestions, recs
 
 
 
94
 
95
- # Gradio UI layout
96
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
97
  gr.Markdown("## 📢 Smart Ad Analyzer")
98
  gr.Markdown(
99
- "Upload an image ad to see: an Ad Category, a five-sentence Analysis, "
100
- "five bullet-point Suggestions, and Example Ads."
101
  )
102
 
103
  with gr.Row():
104
  image_input = gr.Image(type="pil", label="Upload Ad Image")
105
  with gr.Column():
106
- category_out = gr.Textbox(label="Ad Category", interactive=False)
107
- analysis_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
108
- suggestion_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
109
- btn = gr.Button("Analyze Ad", size="sm", variant="primary")
110
 
111
- recommendation_gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
112
 
113
  btn.click(
114
  fn=process,
115
  inputs=[image_input],
116
- outputs=[category_out, analysis_out, suggestion_out, recommendation_gallery]
117
  )
118
 
119
  gr.Markdown("Made by Simon Thalmay")
 
3
  import re
4
  import gradio as gr
5
  from PIL import Image
6
+ from transformers import pipeline
7
+
8
+ # Single pipeline: BLIP-2 + Flan-T5-XL for image-to-text
9
+ pipe = pipeline(
10
+ "image-to-text",
11
+ model="Salesforce/blip2-flan-t5-xl",
12
+ tokenizer="Salesforce/blip2-flan-t5-xl",
13
+ do_sample=True,
14
+ temperature=1.0,
15
+ top_k=50,
16
+ top_p=0.95,
17
+ max_new_tokens=512
18
  )
19
 
20
+ # Hard-coded example-ad URLs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def get_recommendations():
22
  return [
23
  "https://i.imgur.com/InC88PP.jpeg",
 
32
  "https://i.imgur.com/Xj92Cjv.jpeg",
33
  ]
34
 
35
+ def process(image: Image):
36
+ # A single prompt that asks for exactly what you need
 
 
 
 
 
 
37
  prompt = (
38
+ "You are a smart ad analyst. Given the following ad image, output:\n"
39
+ "Category: <one concise label>\n"
40
+ "Analysis: <exactly five sentences explaining what it communicates and its emotional impact>\n"
41
+ "Suggestions:\n"
42
+ "- <bullet 1>\n"
43
+ "- <bullet 2>\n"
44
+ "- <bullet 3>\n"
45
+ "- <bullet 4>\n"
46
+ "- <bullet 5>\n"
47
  )
48
+ raw = pipe(image, prompt=prompt)[0]["generated_text"]
 
 
49
 
50
+ # Parse out the three sections
51
+ cat_match = re.search(r"Category:(.*)Analysis:", raw, re.S)
52
+ ana_match = re.search(r"Analysis:(.*)Suggestions:", raw, re.S)
53
+ sug_match = re.search(r"Suggestions:(.*)", raw, re.S)
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ category = cat_match.group(1).strip() if cat_match else ""
56
+ analysis = ana_match.group(1).strip() if ana_match else ""
57
+ suggestions = sug_match.group(1).strip() if sug_match else ""
58
+
59
+ # Ensure suggestions each start with '-'
60
+ bullets = [line.strip() for line in suggestions.splitlines() if line.strip()]
61
+ if len(bullets) < 5:
62
+ bullets = bullets + ["- (no bullet returned)"] * (5 - len(bullets))
63
+ suggestions = "\n".join(bullets[:5])
64
+
65
+ return category, analysis, suggestions, get_recommendations()
66
 
67
+ # Build UI
68
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
69
  gr.Markdown("## 📢 Smart Ad Analyzer")
70
  gr.Markdown(
71
+ "Upload an image ad to see: an **Ad Category**, a **five-sentence Analysis**, "
72
+ "**five bullet-point Suggestions**, and **Example Ads**."
73
  )
74
 
75
  with gr.Row():
76
  image_input = gr.Image(type="pil", label="Upload Ad Image")
77
  with gr.Column():
78
+ cat_out = gr.Textbox(label="Ad Category", interactive=False)
79
+ ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
80
+ sug_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
81
+ btn = gr.Button("Analyze Ad", size="sm", variant="primary")
82
 
83
+ gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
84
 
85
  btn.click(
86
  fn=process,
87
  inputs=[image_input],
88
+ outputs=[cat_out, ana_out, sug_out, gallery]
89
  )
90
 
91
  gr.Markdown("Made by Simon Thalmay")