Tulitula commited on
Commit
970fe59
·
verified ·
1 Parent(s): e72643a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -5,19 +5,19 @@ 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",
@@ -33,11 +33,10 @@ def get_recommendations():
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"
@@ -45,26 +44,28 @@ def process(image: Image):
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(
@@ -75,10 +76,10 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
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
 
 
5
  from PIL import Image
6
  from transformers import pipeline
7
 
8
+ # Single multi-modal pipeline: BLIP2 + Flan-T5-XL
9
  pipe = pipeline(
10
+ task="image-text-to-text",
11
  model="Salesforce/blip2-flan-t5-xl",
12
  tokenizer="Salesforce/blip2-flan-t5-xl",
13
+ max_new_tokens=500,
14
  do_sample=True,
15
  temperature=1.0,
16
  top_k=50,
17
+ top_p=0.9,
 
18
  )
19
 
20
+ # Hard-coded gallery URLs
21
  def get_recommendations():
22
  return [
23
  "https://i.imgur.com/InC88PP.jpeg",
 
33
  ]
34
 
35
  def process(image: Image):
 
36
  prompt = (
37
+ "You are an expert ad critic. Given the image below, output exactly three sections:\n\n"
38
+ "Category: <one concise label>\n\n"
39
+ "Analysis: <exactly five sentences explaining what the ad communicates and its emotional impact>\n\n"
40
  "Suggestions:\n"
41
  "- <bullet 1>\n"
42
  "- <bullet 2>\n"
 
44
  "- <bullet 4>\n"
45
  "- <bullet 5>\n"
46
  )
 
47
 
48
+ # run the multi-modal pipeline
49
+ result = pipe(image, prompt=prompt)[0]["generated_text"]
 
 
50
 
51
+ # extract the three parts via regex
52
+ cat = re.search(r"Category:(.*?)Analysis:", result, re.S)
53
+ ana = re.search(r"Analysis:(.*?)Suggestions:", result, re.S)
54
+ sug = re.search(r"Suggestions:(.*)", result, re.S)
55
 
56
+ category = cat.group(1).strip() if cat else ""
57
+ analysis = ana.group(1).strip() if ana else ""
58
+ suggestions = sug.group(1).strip() if sug else ""
59
+
60
+ # ensure exactly five bullets
61
+ bullets = [line for line in suggestions.splitlines() if line.startswith("-")]
62
  if len(bullets) < 5:
63
+ bullets += ["- (no suggestion)"] * (5 - len(bullets))
64
  suggestions = "\n".join(bullets[:5])
65
 
66
  return category, analysis, suggestions, get_recommendations()
67
 
68
+ # build the Gradio interface
69
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
70
  gr.Markdown("## 📢 Smart Ad Analyzer")
71
  gr.Markdown(
 
76
  with gr.Row():
77
  image_input = gr.Image(type="pil", label="Upload Ad Image")
78
  with gr.Column():
79
+ cat_out = gr.Textbox(label="Ad Category", interactive=False)
80
+ ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
81
+ sug_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
82
+ btn = gr.Button("Analyze Ad", size="sm", variant="primary")
83
 
84
  gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
85