Tulitula commited on
Commit
787bf7c
·
verified ·
1 Parent(s): 0e87a2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -103
app.py CHANGED
@@ -1,19 +1,44 @@
1
  import gradio as gr
2
  from PIL import Image
3
- import io
4
- import os
5
- from huggingface_hub import InferenceClient
6
-
7
- # You may need to set your Hugging Face token as an environment variable:
8
- # os.environ["HF_TOKEN"] = "your-hf-token"
9
- client = InferenceClient(
10
- model="google/gemma-3-4b-it",
11
- token=os.environ.get("HF_TOKEN") # Add your token if needed for Spaces
12
- )
13
-
14
- def get_recommendations():
15
- # Returns list of 10 example ad image URLs
16
- return [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  "https://i.imgur.com/InC88PP.jpeg",
18
  "https://i.imgur.com/7BHfv4T.png",
19
  "https://i.imgur.com/wp3Wzc4.jpeg",
@@ -26,104 +51,32 @@ def get_recommendations():
26
  "https://i.imgur.com/Xj92Cjv.jpeg",
27
  ]
28
 
29
- def gemma_image_analysis(image: Image.Image):
30
- buf = io.BytesIO()
31
- image.save(buf, format="PNG")
32
- buf.seek(0)
33
- img_bytes = buf.read()
34
-
35
- messages = [
36
- {
37
- "role": "system",
38
- "content": [
39
- {"type": "text", "text": (
40
- "You are an expert ad analyst AI. Analyze the given ad and provide answers for three sections:"
41
- " 1. Category (one word),"
42
- " 2. Analysis (five sentences),"
43
- " 3. Five unique, actionable improvement suggestions as a list starting with '- '."
44
- " Output must be in three sections with clear headings."
45
- )}
46
- ]
47
- },
48
- {
49
- "role": "user",
50
- "content": [
51
- {"type": "image", "image": img_bytes},
52
- {"type": "text", "text": (
53
- "Please respond with:\n\nCategory:\n[category]\n\nAnalysis:\n[5 sentences]\n\nImprovement Suggestions:\n"
54
- "- [suggestion 1]\n- [suggestion 2]\n- [suggestion 3]\n- [suggestion 4]\n- [suggestion 5]\n\n"
55
- "Each suggestion must be unique and actionable. Do not repeat suggestions. If you don't know, say 'not detected'."
56
- )}
57
- ]
58
- }
59
- ]
60
-
61
- response = client.chat.completions.create(
62
- model="google/gemma-3-4b-it",
63
- messages=messages,
64
- max_tokens=512,
65
- )
66
-
67
- return response.choices[0].message["content"]
68
-
69
- def process(image: Image.Image):
70
- if image is None:
71
- return "", "", "", get_recommendations()
72
-
73
- # Run multimodal analysis
74
- full_output = gemma_image_analysis(image)
75
-
76
- # Parse the output into sections (simple but robust splitting)
77
- category = ""
78
- analysis = ""
79
- suggestions = ""
80
- # Find the sections in model output (robust to little formatting errors)
81
- try:
82
- lower = full_output.lower()
83
- cat_idx = lower.find("category")
84
- ana_idx = lower.find("analysis")
85
- sug_idx = lower.find("improvement suggestions")
86
-
87
- if cat_idx != -1 and ana_idx != -1:
88
- category = full_output[cat_idx + 8 : ana_idx].strip().strip(":")
89
- if ana_idx != -1 and sug_idx != -1:
90
- analysis = full_output[ana_idx + 8 : sug_idx].strip().strip(":")
91
- if sug_idx != -1:
92
- suggestions = full_output[sug_idx + len("improvement suggestions"):].strip()
93
- except Exception:
94
- # Fallback if parsing fails
95
- return "", "Analysis parsing failed", "Suggestion parsing failed", get_recommendations()
96
-
97
- return category.strip(), analysis.strip(), suggestions.strip(), get_recommendations()
98
 
99
  def main():
100
- with gr.Blocks(title="Smart Ad Analyzer") as demo:
101
- gr.Markdown("## 📢 Smart Ad Analyzer (Gemma-3 Model)")
102
- gr.Markdown(
103
- """
104
- **Upload your ad image below and instantly get expert feedback.**
105
-
106
- This AI tool will analyze your ad and provide:
107
- - 📂 **Category** — What type of ad is this?
108
- - 📊 **In-depth Analysis** — Five detailed sentences covering message, visuals, emotional impact, and more.
109
- - 🚀 **Improvement Suggestions** — Five actionable, unique ways to make your ad better.
110
- - 📸 **Inspiration Gallery** — See other effective ads for ideas.
111
-
112
- Perfect for marketers, founders, designers, and anyone looking to boost ad performance with actionable insights!
113
- """
114
- )
115
  with gr.Row():
116
  inp = gr.Image(type='pil', label='Upload Ad Image')
117
  with gr.Column():
118
- cat_out = gr.Textbox(label='📂 Ad Category', interactive=False)
119
- ana_out = gr.Textbox(label='📊 Ad Analysis', lines=5, interactive=False)
120
- sug_out = gr.Textbox(label='🚀 Improvement Suggestions', lines=5, interactive=False)
 
121
  btn = gr.Button('Analyze Ad', variant='primary')
122
  gallery = gr.Gallery(label='Example Ads')
123
  btn.click(
124
  fn=process,
125
  inputs=[inp],
126
- outputs=[cat_out, ana_out, sug_out, gallery],
127
  )
128
  gr.Markdown('Made by Simon Thalmay')
129
  return demo
@@ -131,4 +84,3 @@ def main():
131
  if __name__ == "__main__":
132
  demo = main()
133
  demo.launch()
134
-
 
1
  import gradio as gr
2
  from PIL import Image
3
+ from transformers import pipeline
4
+ import tempfile
5
+
6
+ # Initialize pipeline (Gemma 3, image-text-to-text)
7
+ pipe = pipeline("image-text-to-text", model="google/gemma-3-4b-it")
8
+
9
+ def process(image):
10
+ # Save image to temp file and use local path for Gemma
11
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
12
+ image.save(tmp, format="PNG")
13
+ tmp.flush()
14
+ img_path = tmp.name
15
+
16
+ # Build multimodal message for Gemma
17
+ messages = [
18
+ {
19
+ "role": "user",
20
+ "content": [
21
+ {"type": "image", "path": img_path},
22
+ {"type": "text", "text": "Analyze this ad image in detail. What is the product or service? Who is the target audience? Suggest five unique improvements to the ad. Output exactly four fields separated by |||: (1) Category (2) Five-sentence analysis (3) Five bullet suggestions (4) Short punchy headline for the ad."}
23
+ ]
24
+ }
25
+ ]
26
+
27
+ # Get model output
28
+ out = pipe(text=messages, max_new_tokens=512)
29
+ output = out[0]["generated_text"][-1]["content"] if "generated_text" in out[0] else out[0]["content"]
30
+
31
+ # Parse results by "|||"
32
+ if "|||" in output:
33
+ cat, analysis, suggestions, headline = [x.strip() for x in output.split("|||")]
34
+ else:
35
+ cat = "N/A"
36
+ analysis = output.strip()
37
+ suggestions = ""
38
+ headline = ""
39
+
40
+ # Also provide example ads (unchanged)
41
+ gallery = [
42
  "https://i.imgur.com/InC88PP.jpeg",
43
  "https://i.imgur.com/7BHfv4T.png",
44
  "https://i.imgur.com/wp3Wzc4.jpeg",
 
51
  "https://i.imgur.com/Xj92Cjv.jpeg",
52
  ]
53
 
54
+ return cat, analysis, suggestions, headline, gallery
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  def main():
57
+ with gr.Blocks(title="Smart Ad Analyzer (Gemma 3)") as demo:
58
+ gr.Markdown("## 📢 Smart Ad Analyzer (Gemma 3)")
59
+ gr.Markdown("""
60
+ Upload your ad image and get:
61
+ - **Category**
62
+ - **Analysis**
63
+ - **5 Suggestions**
64
+ - **Headline idea**
65
+ - **Example ad gallery**
66
+ """)
 
 
 
 
 
67
  with gr.Row():
68
  inp = gr.Image(type='pil', label='Upload Ad Image')
69
  with gr.Column():
70
+ cat_out = gr.Textbox(label='Ad Category')
71
+ ana_out = gr.Textbox(label='Analysis', lines=5)
72
+ sug_out = gr.Textbox(label='Improvement Suggestions', lines=5)
73
+ head_out = gr.Textbox(label='Headline Suggestion')
74
  btn = gr.Button('Analyze Ad', variant='primary')
75
  gallery = gr.Gallery(label='Example Ads')
76
  btn.click(
77
  fn=process,
78
  inputs=[inp],
79
+ outputs=[cat_out, ana_out, sug_out, head_out, gallery],
80
  )
81
  gr.Markdown('Made by Simon Thalmay')
82
  return demo
 
84
  if __name__ == "__main__":
85
  demo = main()
86
  demo.launch()