Tulitula commited on
Commit
91a3acb
·
verified ·
1 Parent(s): edd7100

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -41
app.py CHANGED
@@ -4,22 +4,22 @@ from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
4
 
5
  # Initialize BLIP for image captioning
6
  blip_processor = BlipProcessor.from_pretrained(
7
- "Salesforce/blip-image-captioning-base"
 
8
  )
9
  blip_model = BlipForConditionalGeneration.from_pretrained(
10
  "Salesforce/blip-image-captioning-base"
11
  )
12
 
13
- # Instruction‑tuned HF model for ad improvement suggestions
14
- adv_generator = pipeline(
15
  "text2text-generation",
16
- model="google/flan-ul2",
17
- tokenizer="google/flan-ul2",
18
- max_new_tokens=150,
19
  do_sample=True,
20
  temperature=0.7,
21
- top_k=50,
22
- top_p=0.95
23
  )
24
 
25
 
@@ -28,73 +28,60 @@ def get_recommendations():
28
  "https://i.imgur.com/InC88PP.jpeg",
29
  "https://i.imgur.com/7BHfv4T.png",
30
  "https://i.imgur.com/wp3Wzc4.jpeg",
31
- # ... add more examples as needed
32
  ]
33
 
34
 
35
  def generate_caption(image):
36
  inputs = blip_processor(images=image, return_tensors="pt")
37
- out = blip_model.generate(**inputs)
38
- caption = blip_processor.decode(out[0], skip_special_tokens=True)
39
  return caption
40
 
41
 
42
  def generate_advice_from_caption(caption):
43
- """
44
- Uses a Hugging Face instruction‑tuned model (Flan‑UL2) to turn the BLIP caption
45
- into targeted, bullet‑pointed ad improvement suggestions.
46
- """
47
  prompt = (
48
  f"Ad description: {caption}\n"
49
- "Please suggest three specific improvements as bullet points, focusing on a clear call to action, key benefits, and visual appeal."
50
  )
51
- result = adv_generator(prompt)
52
- # The model output may include the prompt, so extract after prompt
53
  text = result[0]["generated_text"].replace(prompt, "").strip()
54
  return text
55
 
56
 
57
  def analyze_caption(caption):
58
- keywords = ["product", "offer", "smile", "call to action", "brand"]
59
- if any(k in caption.lower() for k in keywords):
60
- return "👍 Likely effective for advertising."
61
- return "👎 Consider clearer focus or stronger messaging."
 
 
62
 
63
 
64
  def process(image):
65
  caption = generate_caption(image)
66
- suggestions = generate_advice_from_caption(caption)
67
  analysis = analyze_caption(caption)
68
- recomms = get_recommendations()
69
- return caption, suggestions, analysis, recomms
70
 
71
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
72
  gr.Markdown("## 📢 Smart Ad Analyzer")
73
  gr.Markdown(
74
- "Upload an image ad. The app will caption it, suggest improvements with a local instruction‑tuned model, analyze focus, and show example ads."
75
  )
76
 
77
  with gr.Row():
78
  image_input = gr.Image(type="pil", label="Upload Ad Image")
79
  with gr.Column():
80
- caption_out = gr.Textbox(
81
- label="Generated Caption", interactive=False
82
- )
83
- suggestion_out = gr.Textbox(
84
- label="Improvement Suggestions", interactive=False
85
- )
86
- analysis_out = gr.Textbox(
87
- label="Ad Analysis", interactive=False
88
- )
89
 
90
  btn = gr.Button("Analyze Ad")
91
- recommendation_gallery = gr.Gallery(label="Recommended Example Ads")
92
 
93
- btn.click(
94
- fn=process,
95
- inputs=[image_input],
96
- outputs=[caption_out, suggestion_out, analysis_out, recommendation_gallery]
97
- )
98
 
99
  gr.Markdown("Made by Simon Thalmay")
100
 
 
4
 
5
  # Initialize BLIP for image captioning
6
  blip_processor = BlipProcessor.from_pretrained(
7
+ "Salesforce/blip-image-captioning-base",
8
+ use_fast=True
9
  )
10
  blip_model = BlipForConditionalGeneration.from_pretrained(
11
  "Salesforce/blip-image-captioning-base"
12
  )
13
 
14
+ # Lightweight instruction‑tuned HF model for suggestions
15
+ advisor = pipeline(
16
  "text2text-generation",
17
+ model="google/flan-t5-base",
18
+ tokenizer="google/flan-t5-base",
19
+ max_new_tokens=80,
20
  do_sample=True,
21
  temperature=0.7,
22
+ top_p=0.9
 
23
  )
24
 
25
 
 
28
  "https://i.imgur.com/InC88PP.jpeg",
29
  "https://i.imgur.com/7BHfv4T.png",
30
  "https://i.imgur.com/wp3Wzc4.jpeg",
31
+ # add more examples if desired
32
  ]
33
 
34
 
35
  def generate_caption(image):
36
  inputs = blip_processor(images=image, return_tensors="pt")
37
+ outputs = blip_model.generate(**inputs)
38
+ caption = blip_processor.decode(outputs[0], skip_special_tokens=True)
39
  return caption
40
 
41
 
42
  def generate_advice_from_caption(caption):
 
 
 
 
43
  prompt = (
44
  f"Ad description: {caption}\n"
45
+ "Suggest three concise bullet‑point improvements: clear call to action, highlight key benefits, boost visual appeal."
46
  )
47
+ result = advisor(prompt)
 
48
  text = result[0]["generated_text"].replace(prompt, "").strip()
49
  return text
50
 
51
 
52
  def analyze_caption(caption):
53
+ keywords = ["product", "offer", "call to action", "brand"]
54
+ return (
55
+ "👍 Likely effective for advertising."
56
+ if any(k in caption.lower() for k in keywords)
57
+ else "👎 Consider clearer focus or stronger messaging."
58
+ )
59
 
60
 
61
  def process(image):
62
  caption = generate_caption(image)
63
+ advice = generate_advice_from_caption(caption)
64
  analysis = analyze_caption(caption)
65
+ recs = get_recommendations()
66
+ return caption, advice, analysis, recs
67
 
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. BLIP captions it. Flan‑T5‑Base suggests improvements. Then you get analysis and example ads."
72
  )
73
 
74
  with gr.Row():
75
  image_input = gr.Image(type="pil", label="Upload Ad Image")
76
  with gr.Column():
77
+ caption_out = gr.Textbox(label="Generated Caption", interactive=False)
78
+ suggestion_out = gr.Textbox(label="Improvement Suggestions", interactive=False)
79
+ analysis_out = gr.Textbox(label="Ad Analysis", interactive=False)
 
 
 
 
 
 
80
 
81
  btn = gr.Button("Analyze Ad")
82
+ gallery = gr.Gallery(label="Recommended Example Ads")
83
 
84
+ btn.click(fn=process, inputs=[image_input], outputs=[caption_out, suggestion_out, analysis_out, gallery])
 
 
 
 
85
 
86
  gr.Markdown("Made by Simon Thalmay")
87