Tulitula commited on
Commit
54e8072
·
verified ·
1 Parent(s): 5fea42e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -1,21 +1,17 @@
1
  import re
2
  import gradio as gr
3
  from PIL import Image
4
- from transformers import (
5
- AutoProcessor,
6
- AutoModelForVision2Seq,
7
- pipeline,
8
- )
9
 
10
- # Load processor & model
11
  processor = AutoProcessor.from_pretrained("Salesforce/blip2-flan-t5-xl")
12
  model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip2-flan-t5-xl")
13
 
14
- # Create the image-text-to-text pipeline
15
  pipe = pipeline(
16
- task="image-text-to-text",
17
  model=model,
18
- feature_extractor=processor.feature_extractor,
19
  tokenizer=processor.tokenizer,
20
  max_new_tokens=500,
21
  do_sample=True,
@@ -39,6 +35,7 @@ def get_recommendations():
39
  ]
40
 
41
  def process(image: Image):
 
42
  prompt = (
43
  "You are an expert ad critic. Given the image below, output exactly three sections:\n\n"
44
  "Category: <one concise label>\n\n"
@@ -51,25 +48,27 @@ def process(image: Image):
51
  "- <bullet 5>\n"
52
  )
53
 
 
54
  out = pipe(image, prompt=prompt)[0]["generated_text"]
55
 
56
- # pull out each section via regex
57
- cat = re.search(r"Category:(.*?)Analysis:", out, re.S)
58
- ana = re.search(r"Analysis:(.*?)Suggestions:", out, re.S)
59
- sug = re.search(r"Suggestions:(.*)", out, re.S)
60
 
61
- category = cat.group(1).strip() if cat else ""
62
- analysis = ana.group(1).strip() if ana else ""
63
- suggestions = sug.group(1).strip() if sug else ""
64
 
65
- # ensure exactly 5 bullets
66
- bullets = [l for l in suggestions.splitlines() if l.startswith("-")]
67
  if len(bullets) < 5:
68
  bullets += ["- (no suggestion)"] * (5 - len(bullets))
69
  suggestions = "\n".join(bullets[:5])
70
 
71
  return category, analysis, suggestions, get_recommendations()
72
 
 
73
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
74
  gr.Markdown("## 📢 Smart Ad Analyzer")
75
  gr.Markdown(
 
1
  import re
2
  import gradio as gr
3
  from PIL import Image
4
+ from transformers import AutoProcessor, AutoModelForVision2Seq, pipeline
 
 
 
 
5
 
6
+ # 1) Load BLIP-2 processor & model
7
  processor = AutoProcessor.from_pretrained("Salesforce/blip2-flan-t5-xl")
8
  model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip2-flan-t5-xl")
9
 
10
+ # 2) Build the multimodal pipeline correctly
11
  pipe = pipeline(
12
+ "image-text-to-text",
13
  model=model,
14
+ feature_extractor=processor.image_processor, # BLIP2Processor uses .image_processor
15
  tokenizer=processor.tokenizer,
16
  max_new_tokens=500,
17
  do_sample=True,
 
35
  ]
36
 
37
  def process(image: Image):
38
+ # A single prompt that asks BLIP-2+Flan-T5 to emit exactly three sections
39
  prompt = (
40
  "You are an expert ad critic. Given the image below, output exactly three sections:\n\n"
41
  "Category: <one concise label>\n\n"
 
48
  "- <bullet 5>\n"
49
  )
50
 
51
+ # Run the pipeline
52
  out = pipe(image, prompt=prompt)[0]["generated_text"]
53
 
54
+ # Regex-extract each section
55
+ cat_match = re.search(r"Category:(.*?)Analysis:", out, re.S)
56
+ ana_match = re.search(r"Analysis:(.*?)Suggestions:", out, re.S)
57
+ sug_match = re.search(r"Suggestions:(.*)", out, re.S)
58
 
59
+ category = cat_match.group(1).strip() if cat_match else ""
60
+ analysis = ana_match.group(1).strip() if ana_match else ""
61
+ suggestions = sug_match.group(1).strip() if sug_match else ""
62
 
63
+ # Ensure exactly 5 bullets
64
+ bullets = [line for line in suggestions.splitlines() if line.startswith("-")]
65
  if len(bullets) < 5:
66
  bullets += ["- (no suggestion)"] * (5 - len(bullets))
67
  suggestions = "\n".join(bullets[:5])
68
 
69
  return category, analysis, suggestions, get_recommendations()
70
 
71
+ # --- Gradio UI ---
72
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
73
  gr.Markdown("## 📢 Smart Ad Analyzer")
74
  gr.Markdown(