sreenathsree1578 commited on
Commit
6255508
·
verified ·
1 Parent(s): 4af6758

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -42
app.py CHANGED
@@ -2,11 +2,16 @@ import gradio as gr
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
-
6
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
7
 
8
  MODEL_NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
9
 
 
 
 
 
10
  model = AutoModelForCausalLM.from_pretrained(
11
  MODEL_NAME,
12
  dtype=torch.float32
@@ -14,6 +19,12 @@ model = AutoModelForCausalLM.from_pretrained(
14
 
15
  model.eval()
16
 
 
 
 
 
 
 
17
 
18
  def generate_product_content(
19
  product_name,
@@ -23,25 +34,35 @@ def generate_product_content(
23
  features,
24
  target_customer
25
  ):
 
 
 
 
 
 
 
 
 
26
  prompt = f"""
27
- You are an ecommerce product content writer.
28
 
29
- Create professional ecommerce content for the following product.
30
 
31
- Product name: {product_name}
 
32
  Category: {category}
33
  Material: {material}
34
  Color: {color}
35
  Features: {features}
36
- Target customer: {target_customer}
37
 
38
- Return ONLY this format:
39
 
40
  SHORT_DESCRIPTION:
41
- 2 sentences.
42
 
43
  DESCRIPTION:
44
- 80-120 words.
45
 
46
  KEY_FEATURES:
47
  - Feature 1
@@ -50,23 +71,26 @@ KEY_FEATURES:
50
  - Feature 4
51
 
52
  SEO_TITLE:
53
- Maximum 60 characters.
54
 
55
  META_DESCRIPTION:
56
- Maximum 155 characters.
57
 
58
- Rules:
59
  - Do not invent specifications.
60
- - Do not mention unavailable features.
61
- - Do not use exaggerated medical or guaranteed claims.
62
- - Keep the writing suitable for an ecommerce store.
63
- - Use clear and natural English.
 
 
 
64
  """
65
 
66
  messages = [
67
  {
68
  "role": "system",
69
- "content": "You are a professional ecommerce product copywriter."
70
  },
71
  {
72
  "role": "user",
@@ -74,43 +98,121 @@ Rules:
74
  }
75
  ]
76
 
77
- inputs = tokenizer.apply_chat_template(
 
78
  messages,
79
- add_generation_prompt=True,
80
- tokenize=True,
 
 
 
 
81
  return_tensors="pt"
82
  )
83
 
 
84
  with torch.no_grad():
85
  outputs = model.generate(
86
- inputs,
87
- max_new_tokens=400,
88
  temperature=0.7,
89
  top_p=0.9,
90
- do_sample=True
 
91
  )
92
 
 
 
 
93
  result = tokenizer.decode(
94
- outputs[0][inputs.shape[-1]:],
95
  skip_special_tokens=True
96
  )
97
 
98
- return result
99
-
100
-
101
- demo = gr.Interface(
102
- fn=generate_product_content,
103
- inputs=[
104
- gr.Textbox(label="Product Name"),
105
- gr.Textbox(label="Category"),
106
- gr.Textbox(label="Material"),
107
- gr.Textbox(label="Color"),
108
- gr.Textbox(label="Features"),
109
- gr.Textbox(label="Target Customer"),
110
- ],
111
- outputs=gr.Textbox(label="Generated Content"),
112
- title="Ecommerce Product Content Generator",
113
- description="Generate product descriptions and SEO content."
114
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
- demo.launch()
 
 
 
 
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
+ # ============================================================
6
+ # MODEL
7
+ # ============================================================
8
 
9
  MODEL_NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
10
 
11
+ print("Loading tokenizer...")
12
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
13
+
14
+ print("Loading model...")
15
  model = AutoModelForCausalLM.from_pretrained(
16
  MODEL_NAME,
17
  dtype=torch.float32
 
19
 
20
  model.eval()
21
 
22
+ print("Model loaded successfully!")
23
+
24
+
25
+ # ============================================================
26
+ # PRODUCT CONTENT GENERATOR
27
+ # ============================================================
28
 
29
  def generate_product_content(
30
  product_name,
 
34
  features,
35
  target_customer
36
  ):
37
+
38
+ # Clean empty values
39
+ product_name = product_name.strip()
40
+ category = category.strip()
41
+ material = material.strip()
42
+ color = color.strip()
43
+ features = features.strip()
44
+ target_customer = target_customer.strip()
45
+
46
  prompt = f"""
47
+ You are an ecommerce product copywriter.
48
 
49
+ Write product content using ONLY the information provided below.
50
 
51
+ PRODUCT:
52
+ Name: {product_name}
53
  Category: {category}
54
  Material: {material}
55
  Color: {color}
56
  Features: {features}
57
+ Target Customer: {target_customer}
58
 
59
+ Return exactly this format:
60
 
61
  SHORT_DESCRIPTION:
62
+ Write 1-2 concise sentences.
63
 
64
  DESCRIPTION:
65
+ Write a natural ecommerce product description.
66
 
67
  KEY_FEATURES:
68
  - Feature 1
 
71
  - Feature 4
72
 
73
  SEO_TITLE:
74
+ Write an SEO-friendly title.
75
 
76
  META_DESCRIPTION:
77
+ Write a short SEO meta description.
78
 
79
+ RULES:
80
  - Do not invent specifications.
81
+ - Do not invent dimensions.
82
+ - Do not invent certifications.
83
+ - Do not make medical claims.
84
+ - Do not make unrealistic guarantees.
85
+ - Do not mention that you are an AI.
86
+ - Do not add explanations outside the requested format.
87
+ - Keep the language natural and suitable for an online store.
88
  """
89
 
90
  messages = [
91
  {
92
  "role": "system",
93
+ "content": "You are a professional ecommerce product content writer."
94
  },
95
  {
96
  "role": "user",
 
98
  }
99
  ]
100
 
101
+ # Apply model's chat template
102
+ input_text = tokenizer.apply_chat_template(
103
  messages,
104
+ tokenize=False,
105
+ add_generation_prompt=True
106
+ )
107
+
108
+ inputs = tokenizer(
109
+ input_text,
110
  return_tensors="pt"
111
  )
112
 
113
+ # Generate
114
  with torch.no_grad():
115
  outputs = model.generate(
116
+ **inputs,
117
+ max_new_tokens=300,
118
  temperature=0.7,
119
  top_p=0.9,
120
+ do_sample=True,
121
+ repetition_penalty=1.1
122
  )
123
 
124
+ # Remove input tokens
125
+ generated_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
126
+
127
  result = tokenizer.decode(
128
+ generated_tokens,
129
  skip_special_tokens=True
130
  )
131
 
132
+ return result.strip()
133
+
134
+
135
+ # ============================================================
136
+ # GRADIO UI
137
+ # ============================================================
138
+
139
+ with gr.Blocks(title="Product Content AI") as demo:
140
+
141
+ gr.Markdown(
142
+ """
143
+ # Product Content AI
144
+
145
+ Generate ecommerce product descriptions and SEO content.
146
+ """
147
+ )
148
+
149
+ with gr.Row():
150
+
151
+ with gr.Column():
152
+
153
+ product_name = gr.Textbox(
154
+ label="Product Name",
155
+ placeholder="Blue Crystal Necklace"
156
+ )
157
+
158
+ category = gr.Textbox(
159
+ label="Category",
160
+ placeholder="Necklace"
161
+ )
162
+
163
+ material = gr.Textbox(
164
+ label="Material",
165
+ placeholder="Alloy"
166
+ )
167
+
168
+ color = gr.Textbox(
169
+ label="Color",
170
+ placeholder="Blue and Gold"
171
+ )
172
+
173
+ features = gr.Textbox(
174
+ label="Product Features",
175
+ placeholder="Crystal pendant, lightweight, adjustable chain",
176
+ lines=4
177
+ )
178
+
179
+ target_customer = gr.Textbox(
180
+ label="Target Customer",
181
+ placeholder="Women"
182
+ )
183
+
184
+ generate_button = gr.Button(
185
+ "Generate Product Content",
186
+ variant="primary"
187
+ )
188
+
189
+ with gr.Column():
190
+
191
+ output = gr.Textbox(
192
+ label="Generated Content",
193
+ lines=18
194
+ )
195
+
196
+ generate_button.click(
197
+ fn=generate_product_content,
198
+ inputs=[
199
+ product_name,
200
+ category,
201
+ material,
202
+ color,
203
+ features,
204
+ target_customer
205
+ ],
206
+ outputs=output,
207
+ api_name="generate_product_content"
208
+ )
209
+
210
+
211
+ # ============================================================
212
+ # START
213
+ # ============================================================
214
 
215
+ demo.launch(
216
+ server_name="0.0.0.0",
217
+ server_port=7860
218
+ )