Tulitula commited on
Commit
8d85f43
Β·
verified Β·
1 Parent(s): 238c8ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -54
app.py CHANGED
@@ -1,63 +1,146 @@
 
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
-
4
- # Load model and tokenizer
5
- MODEL_NAME = "meta-llama/Llama-3.1-8B-Instruct"
6
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
7
- model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
8
- # Uncomment the next line to run on GPU if available
9
- # model = model.to("cuda")
10
-
11
- def improve_ad(ad_analysis):
12
- prompt = (
13
- "You are an expert ad consultant. Based on the following ad description or analysis, "
14
- "give 5 unique, actionable suggestions to improve the ad. "
15
- "Each suggestion must be on a new line, start with '- ', and should NOT repeat. "
16
- "If possible, cover different aspects (messaging, design, call-to-action, targeting, layout, emotion, offer). "
17
- "\n\nAd Analysis or Description:\n"
18
- f"{ad_analysis}\n\n"
19
- "Improvement Suggestions:"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  )
21
- input_ids = tokenizer(prompt, return_tensors="pt")
22
- # Uncomment the next line to run on GPU if available
23
- # input_ids = input_ids.to("cuda")
24
- output = model.generate(
25
- **input_ids,
26
- max_new_tokens=256,
27
- do_sample=False,
28
- pad_token_id=tokenizer.eos_token_id,
29
  )
30
- response = tokenizer.decode(output[0], skip_special_tokens=True)
31
- # Extract suggestions: Find the part after "Improvement Suggestions:"
32
- if "Improvement Suggestions:" in response:
33
- response = response.split("Improvement Suggestions:")[-1]
34
- # Filter and clean up to get exactly 5 suggestions
35
- suggestions = []
36
- for line in response.splitlines():
37
- line = line.strip()
38
- if line.startswith("-") and line not in suggestions:
39
- suggestions.append(line)
40
- if len(suggestions) == 5:
 
 
 
 
41
  break
42
- # If not enough, pad with generic
43
  defaults = [
44
- "- Add a clear and attractive call-to-action.",
45
- "- Improve the visual contrast and headline font size.",
46
- "- Refine the main message for clarity.",
47
- "- Target a more specific audience segment.",
48
- "- Use more emotional or persuasive language."
49
  ]
50
- for d in defaults:
51
- if len(suggestions) < 5 and d not in suggestions:
52
- suggestions.append(d)
53
- return "\n".join(suggestions[:5])
54
-
55
- with gr.Blocks() as demo:
56
- gr.Markdown("## πŸ¦™ Llama-3.1 Ad Improvement Consultant\nPaste your ad analysis or description below, and get 5 unique suggestions to improve it, powered by Meta Llama 3.1 8B Instruct (runs 100 percent locally, no API required).")
57
- inp = gr.Textbox(label="Ad Analysis or Description", lines=6, placeholder="Paste your ad description, image caption, or analysis here...")
58
- out = gr.Textbox(label="Improvement Suggestions", lines=7)
59
- btn = gr.Button("Get Suggestions")
60
- btn.click(fn=improve_ad, inputs=inp, outputs=out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
 
63
  demo.launch()
 
1
+ import re
2
  import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+ from transformers import (
6
+ pipeline,
7
+ AutoProcessor,
8
+ AutoModelForVision2Seq,
9
+ AutoTokenizer,
10
+ AutoModelForCausalLM,
11
+ )
12
+
13
+ # Auto-detect CPU/GPU
14
+ DEVICE = 0 if torch.cuda.is_available() else -1
15
+
16
+ # Load BLIP for image captioning
17
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
18
+ blip_model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-large")
19
+ caption_pipe = pipeline(
20
+ task="image-to-text",
21
+ model=blip_model,
22
+ tokenizer=processor.tokenizer,
23
+ image_processor=processor.image_processor,
24
+ device=DEVICE,
25
+ )
26
+
27
+ # Load Mistral-7B-Instruct for text tasks
28
+ INSTRUCT_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
29
+ instr_tokenizer = AutoTokenizer.from_pretrained(INSTRUCT_MODEL)
30
+ instr_model = AutoModelForCausalLM.from_pretrained(INSTRUCT_MODEL)
31
+ instr_pipe = pipeline(
32
+ "text-generation",
33
+ model=instr_model,
34
+ tokenizer=instr_tokenizer,
35
+ device=DEVICE,
36
+ max_new_tokens=256,
37
+ temperature=0.5, # Lowered temp for less repetition
38
+ do_sample=True,
39
+ repetition_penalty=1.2, # Discourage repetition
40
+ )
41
+
42
+ def get_recommendations():
43
+ return [
44
+ "https://i.imgur.com/InC88PP.jpeg",
45
+ "https://i.imgur.com/7BHfv4T.png",
46
+ "https://i.imgur.com/wp3Wzc4.jpeg",
47
+ "https://i.imgur.com/5e2xOA4.jpeg",
48
+ "https://i.imgur.com/txjRk98.jpeg",
49
+ "https://i.imgur.com/rQ4AYl0.jpeg",
50
+ "https://i.imgur.com/bDzwD04.jpeg",
51
+ "https://i.imgur.com/fLMngXI.jpeg",
52
+ "https://i.imgur.com/nYEJzxt.png",
53
+ "https://i.imgur.com/Xj92Cjv.jpeg",
54
+ ]
55
+
56
+ def process(image: Image):
57
+ if image is None:
58
+ return "", "", "", get_recommendations()
59
+ # 1. BLIP caption
60
+ caption_res = caption_pipe(image, max_new_tokens=64)
61
+ desc = caption_res[0]["generated_text"].strip()
62
+
63
+ # 2. Category (via instruct model)
64
+ cat_prompt = f"<s>[INST] The following is an ad: {desc}\nWhat is the concise category of this ad (e.g. Food, Fitness, Tech, Fashion, Travel, etc)? Respond with one word. [/INST]"
65
+ cat_out = instr_pipe(cat_prompt)[0]['generated_text'].split("\n")[0].strip()
66
+
67
+ # 3. Analysis (via instruct model)
68
+ ana_prompt = (
69
+ f"<s>[INST] Here is an ad description: {desc}\n"
70
+ "Write five sentences analyzing the ad. Cover: 1) What product or service is advertised, 2) Main visual elements, 3) The target audience, 4) The marketing message, 5) The emotional appeal or persuasion technique. Each sentence should be informative and not repeat. [/INST]"
71
  )
72
+ ana_raw = instr_pipe(ana_prompt)[0]['generated_text']
73
+ ana_sentences = re.split(r'(?<=[.!?])\s+', ana_raw.strip())
74
+ analysis = " ".join(ana_sentences[:5])
75
+
76
+ # 4. Five suggestions (via instruct model, filter repetitions)
77
+ sug_prompt = (
78
+ f"<s>[INST] Given this ad analysis: {analysis}\n"
79
+ "Suggest five practical, unique improvements for the ad. Each should address a different aspect (message, visuals, call-to-action, targeting, layout, or design). Each suggestion must be one sentence, not repeat, and start with '- '. [/INST]"
80
  )
81
+ sug_raw = instr_pipe(sug_prompt)[0]['generated_text']
82
+ bullets = []
83
+ seen = set()
84
+ for line in sug_raw.splitlines():
85
+ if line.startswith("-"):
86
+ suggestion = line.strip()
87
+ if suggestion not in seen and suggestion:
88
+ bullets.append(suggestion)
89
+ seen.add(suggestion)
90
+ elif line.strip():
91
+ suggestion = "- " + line.strip()
92
+ if suggestion not in seen and suggestion:
93
+ bullets.append(suggestion)
94
+ seen.add(suggestion)
95
+ if len(bullets) == 5:
96
  break
97
+ # Add generic suggestions only if needed
98
  defaults = [
99
+ "- Make the main headline more eye-catching.",
100
+ "- Add a clear and visible call-to-action button.",
101
+ "- Use contrasting colors for better readability.",
102
+ "- Highlight the unique selling point of the product.",
103
+ "- Simplify the design to reduce clutter."
104
  ]
105
+ for default in defaults:
106
+ if len(bullets) < 5 and default not in seen:
107
+ bullets.append(default)
108
+ suggestions = "\n".join(bullets[:5])
109
+
110
+ return cat_out, analysis, suggestions, get_recommendations()
111
+
112
+ def main():
113
+ with gr.Blocks(title="Smart Ad Analyzer") as demo:
114
+ gr.Markdown("## πŸ“’ Smart Ad Analyzer")
115
+ gr.Markdown(
116
+ """
117
+ **Upload your ad image below and instantly get expert feedback.**
118
+
119
+ This AI tool will analyze your ad and provide:
120
+ - πŸ“‚ **Category** β€” What type of ad is this?
121
+ - πŸ“Š **In-depth Analysis** β€” Five detailed sentences covering message, visuals, emotional impact, and more.
122
+ - πŸš€ **Improvement Suggestions** β€” Five actionable, unique ways to make your ad better.
123
+ - πŸ“Έ **Inspiration Gallery** β€” See other effective ads for ideas.
124
+
125
+ Perfect for marketers, founders, designers, and anyone looking to boost ad performance with actionable insights!
126
+ """
127
+ )
128
+ with gr.Row():
129
+ inp = gr.Image(type='pil', label='Upload Ad Image')
130
+ with gr.Column():
131
+ cat_out = gr.Textbox(label='πŸ“‚ Ad Category', interactive=False)
132
+ ana_out = gr.Textbox(label='πŸ“Š Ad Analysis', lines=5, interactive=False)
133
+ sug_out = gr.Textbox(label='πŸš€ Improvement Suggestions', lines=5, interactive=False)
134
+ btn = gr.Button('Analyze Ad', variant='primary')
135
+ gallery = gr.Gallery(label='Example Ads')
136
+ btn.click(
137
+ fn=process,
138
+ inputs=[inp],
139
+ outputs=[cat_out, ana_out, sug_out, gallery],
140
+ )
141
+ gr.Markdown('Made by Simon Thalmay')
142
+ return demo
143
 
144
  if __name__ == "__main__":
145
+ demo = main()
146
  demo.launch()