Tulitula commited on
Commit
d275ca5
·
verified ·
1 Parent(s): 034aeca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -44
app.py CHANGED
@@ -1,25 +1,44 @@
1
  import os
2
  import gradio as gr
3
- from transformers import pipeline
 
 
4
 
5
- HF_TOKEN = os.environ.get("HF_TOKEN")
 
6
 
7
- # 1. Image Captioning (fast lightweight)
8
- captioner = pipeline(
9
- "image-to-text",
10
- model="google/paligemma-3b-pt-224",
11
- token=HF_TOKEN
 
 
 
 
 
 
 
 
12
  )
13
 
14
- # 2. Text Feedback/Analysis (fast lightweight)
15
- reviewer = pipeline(
 
 
 
 
 
16
  "text-generation",
17
- model="google/gemma-1.1-2b-it",
18
- token=HF_TOKEN
 
 
 
 
19
  )
20
 
21
  def get_recommendations():
22
- # Returns list of 10 example ad image URLs
23
  return [
24
  "https://i.imgur.com/InC88PP.jpeg",
25
  "https://i.imgur.com/7BHfv4T.png",
@@ -33,43 +52,69 @@ def get_recommendations():
33
  "https://i.imgur.com/Xj92Cjv.jpeg",
34
  ]
35
 
36
- def process(image):
37
  if image is None:
38
  return "", "", "", get_recommendations()
39
-
40
- # 1. Caption/ad description
41
- cap = captioner(image)[0]["generated_text"].strip()
42
-
43
- # 2. Build feedback prompt
44
- prompt = (
45
- f"Ad description: {cap}\n"
46
- "Your task: \n"
47
- "1. Give a concise category label for this ad (e.g., 'Food', 'Fitness').\n"
48
- "2. Write exactly five sentences analyzing what this ad communicates and its emotional impact.\n"
49
- "3. Suggest five ways to improve this ad. Each suggestion should be a short, practical sentence."
 
 
50
  )
51
- # 3. Run through Gemma
52
- resp = reviewer(prompt, max_new_tokens=256)[0]["generated_text"]
53
-
54
- # 4. Simple parsing logic
55
- lines = resp.split('\n')
56
- cat, analysis, suggestions = "", "", ""
57
- for line in lines:
58
- if "category" in line.lower():
59
- cat = line.split(":", 1)[-1].strip()
60
- elif "analysis" in line.lower() or "sentence" in line.lower():
61
- analysis += line + " "
62
- elif "suggestion" in line.lower() or line.strip().startswith("-"):
63
- suggestions += line + "\n"
64
- if not cat: cat = lines[0][:80]
65
- if not analysis: analysis = "\n".join(lines[1:6])
66
- if not suggestions: suggestions = "\n".join(lines[6:11])
67
-
68
- return cat.strip(), analysis.strip(), suggestions.strip(), get_recommendations()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  def main():
71
- with gr.Blocks(title="Smart Ad Analyzer (Fast Edition)") as demo:
72
- gr.Markdown("## 📢 Smart Ad Analyzer (Fast Edition)")
73
  gr.Markdown(
74
  """
75
  Upload your ad image below and instantly get expert feedback.
 
1
  import os
2
  import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+ from transformers import pipeline, AutoProcessor, AutoModelForVision2Seq, AutoTokenizer, AutoModelForCausalLM
6
 
7
+ # --- SETUP TOKEN ---
8
+ HF_TOKEN = os.getenv("HF_TOKEN") # Set in env or Secrets on Spaces
9
 
10
+ # --- DEVICE ---
11
+ DEVICE = 0 if torch.cuda.is_available() else -1
12
+
13
+ # --- BLIP: Captioning ---
14
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large", token=HF_TOKEN)
15
+ blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large", token=HF_TOKEN)
16
+ caption_pipe = pipeline(
17
+ task="image-to-text",
18
+ model=blip_model,
19
+ tokenizer=processor.tokenizer,
20
+ image_processor=processor.image_processor,
21
+ device=DEVICE,
22
+ token=HF_TOKEN,
23
  )
24
 
25
+ # --- GEMMA: Text Generation ---
26
+ # Swap this to your preferred Gemma model ID, e.g. "google/gemma-2b-it"
27
+ GEMMA_MODEL = "google/gemma-2b-it"
28
+
29
+ gemma_tokenizer = AutoTokenizer.from_pretrained(GEMMA_MODEL, token=HF_TOKEN)
30
+ gemma_model = AutoModelForCausalLM.from_pretrained(GEMMA_MODEL, token=HF_TOKEN)
31
+ gemma_pipe = pipeline(
32
  "text-generation",
33
+ model=gemma_model,
34
+ tokenizer=gemma_tokenizer,
35
+ device=DEVICE,
36
+ max_new_tokens=384,
37
+ do_sample=False,
38
+ token=HF_TOKEN,
39
  )
40
 
41
  def get_recommendations():
 
42
  return [
43
  "https://i.imgur.com/InC88PP.jpeg",
44
  "https://i.imgur.com/7BHfv4T.png",
 
52
  "https://i.imgur.com/Xj92Cjv.jpeg",
53
  ]
54
 
55
+ def process(image: Image):
56
  if image is None:
57
  return "", "", "", get_recommendations()
58
+
59
+ # 1. BLIP: Caption
60
+ caption_res = caption_pipe(image, max_new_tokens=64)
61
+ description = caption_res[0]["generated_text"].strip()
62
+
63
+ # 2. GEMMA: Category
64
+ prompt_cat = f"This is an ad image. Description: {description}\n\nProvide a concise category label for this ad (e.g. Food, Fitness, Technology):"
65
+ cat_out = gemma_pipe(prompt_cat)[0]['generated_text'].splitlines()[0].strip()
66
+
67
+ # 3. GEMMA: Five-sentence analysis
68
+ prompt_ana = (
69
+ f"This is an ad image. Description: {description}\n\n"
70
+ "Write exactly five sentences explaining what this ad communicates and its emotional impact."
71
  )
72
+ ana_raw = gemma_pipe(prompt_ana)[0]['generated_text'].strip()
73
+ # Get only first five sentences.
74
+ import re
75
+ sentences = re.split(r'(?<=[.!?])\s+', ana_raw)
76
+ analysis = " ".join(sentences[:5])
77
+
78
+ # 4. GEMMA: Five suggestions (bullets, unique)
79
+ prompt_sug = (
80
+ f"This is an ad image. Description: {description}\n\n"
81
+ "Suggest five unique, practical improvements for this ad. Each must address a different aspect (message, visuals, call-to-action, targeting, layout, or design). "
82
+ "Each suggestion must be one sentence and start with '- '. Do NOT repeat suggestions."
83
+ )
84
+ sug_raw = gemma_pipe(prompt_sug)[0]['generated_text']
85
+ bullets = []
86
+ seen = set()
87
+ for line in sug_raw.splitlines():
88
+ if line.startswith("-"):
89
+ suggestion = line.strip()
90
+ if suggestion and suggestion not in seen:
91
+ bullets.append(suggestion)
92
+ seen.add(suggestion)
93
+ elif line.strip():
94
+ suggestion = "- " + line.strip()
95
+ if suggestion and suggestion not in seen:
96
+ bullets.append(suggestion)
97
+ seen.add(suggestion)
98
+ if len(bullets) == 5:
99
+ break
100
+ # Defaults if not enough bullets
101
+ defaults = [
102
+ "- Make the main headline more eye-catching.",
103
+ "- Add a clear and visible call-to-action button.",
104
+ "- Use contrasting colors for better readability.",
105
+ "- Highlight the unique selling point of the product.",
106
+ "- Simplify the design to reduce clutter."
107
+ ]
108
+ for default in defaults:
109
+ if len(bullets) < 5 and default not in seen:
110
+ bullets.append(default)
111
+ suggestions = "\n".join(bullets[:5])
112
+
113
+ return cat_out, analysis, suggestions, get_recommendations()
114
 
115
  def main():
116
+ with gr.Blocks(title="Smart Ad Analyzer (BLIP+Gemma)") as demo:
117
+ gr.Markdown("## 📢 Smart Ad Analyzer (BLIP + Gemma)")
118
  gr.Markdown(
119
  """
120
  Upload your ad image below and instantly get expert feedback.