haxerwddle commited on
Commit
e2229d2
·
1 Parent(s): a0868e2

Model prompt change

Browse files
Files changed (1) hide show
  1. app.py +87 -8
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import torch
3
  from transformers import (
4
  AutoTokenizer, AutoModelForCausalLM,
@@ -36,7 +37,7 @@ pipe = pipeline(
36
  model=chat_model,
37
  tokenizer=tokenizer,
38
  device_map="auto",
39
- max_new_tokens=80
40
  )
41
 
42
  def clean_chat_output(full_text):
@@ -75,7 +76,7 @@ def explain_recycling(class_label):
75
 
76
  outputs = pipe(
77
  prompt,
78
- max_new_tokens=80,
79
  do_sample=True,
80
  top_p = 0.9,
81
  temperature=0.3
@@ -87,7 +88,7 @@ def explain_recycling(class_label):
87
 
88
  # ------------------ PIPELINE ------------------
89
  top_label = None
90
- waste_analization = {
91
  "cardboard": {
92
  "Recycling type": "Paper/Cardboard recycling",
93
  "Disposal": "Flatten the cardboard boxes and remove any non-paper packing materials before placing them in the recycling bin.",
@@ -125,6 +126,83 @@ waste_analization = {
125
  "Extra": "Try to reduce overall waste by reusing and composting when possible."
126
  }
127
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  def classify_pipeline(image):
130
  global top_label
@@ -133,14 +211,15 @@ def classify_pipeline(image):
133
  top_label = max(predictions, key=predictions.get) # top-1
134
  return predictions
135
 
136
- def analyze_pipeline():
137
  global top_label
138
 
139
  if top_label is None:
140
  return "Please classify an image first."
141
 
142
- # explanation = explain_recycling(top_label)
143
- info = waste_analization[top_label]
 
144
  result = (
145
  f"• Recycling type: {info['Recycling type']}\n"
146
  f"• Disposal: {info['Disposal']}\n"
@@ -257,11 +336,11 @@ with gr.Blocks() as demo:
257
  )
258
 
259
  # BUTTON
260
- analyze_result_btn = gr.Button("Analyze Waste", variant="primary")
261
 
262
  analyze_result_btn.click(
263
  analyze_pipeline,
264
- inputs=None,
265
  outputs=explain_output
266
  )
267
 
 
1
  import gradio as gr
2
+ import random
3
  import torch
4
  from transformers import (
5
  AutoTokenizer, AutoModelForCausalLM,
 
37
  model=chat_model,
38
  tokenizer=tokenizer,
39
  device_map="auto",
40
+ max_new_tokens=60
41
  )
42
 
43
  def clean_chat_output(full_text):
 
76
 
77
  outputs = pipe(
78
  prompt,
79
+ max_new_tokens=60,
80
  do_sample=True,
81
  top_p = 0.9,
82
  temperature=0.3
 
88
 
89
  # ------------------ PIPELINE ------------------
90
  top_label = None
91
+ waste_analyzation = {
92
  "cardboard": {
93
  "Recycling type": "Paper/Cardboard recycling",
94
  "Disposal": "Flatten the cardboard boxes and remove any non-paper packing materials before placing them in the recycling bin.",
 
126
  "Extra": "Try to reduce overall waste by reusing and composting when possible."
127
  }
128
  }
129
+ waste_analyzation_v2 = {
130
+ "cardboard": {
131
+ "Recycling type": "Paper/Cardboard recycling",
132
+ "Disposal": "Fold or cut cardboard into smaller flat pieces so it fits properly in recycling bins.",
133
+ "Tips": "Keep cardboard free from oils or food stains to avoid contaminating the batch.",
134
+ "Extra": "Cardboard with shiny or laminated surfaces may need separate processing."
135
+ },
136
+ "glass": {
137
+ "Recycling type": "Glass recycling",
138
+ "Disposal": "Remove lids and caps, lightly rinse, and place glass containers into the glass-only bin.",
139
+ "Tips": "Do not place broken drinking glasses or ceramics with recyclable glass—they melt at different temperatures.",
140
+ "Extra": "Some regions require taking glass to drop-off centers instead of curbside bins."
141
+ },
142
+ "metal": {
143
+ "Recycling type": "Metal recycling",
144
+ "Disposal": "Empty metal containers and place them in the metal bin; labels may stay on.",
145
+ "Tips": "Keep metal separate from electronics or batteries, which require special facilities.",
146
+ "Extra": "Large metal objects like appliances may require bulk recycling pickup."
147
+ },
148
+ "paper": {
149
+ "Recycling type": "Paper recycling",
150
+ "Disposal": "Place clean, dry paper products such as envelopes and notebooks into paper bins.",
151
+ "Tips": "Do not recycle coated paper (e.g., laminated or plastic-lined pages).",
152
+ "Extra": "Paper with heavy ink coverage might be processed separately."
153
+ },
154
+ "plastic": {
155
+ "Recycling type": "Plastic recycling",
156
+ "Disposal": "Clean and drain plastic bottles or tubs, then place them in the plastics bin.",
157
+ "Tips": "Flatten plastic bottles to reduce volume unless your local guidelines say otherwise.",
158
+ "Extra": "Some plastics like polystyrene foam require special drop-off locations."
159
+ },
160
+ "trash": {
161
+ "Recycling type": "General waste / landfill",
162
+ "Disposal": "Dispose of unrecyclable materials in regular waste; secure loose items in bags.",
163
+ "Tips": "Separate hazardous or toxic waste like paint, solvents, or chemicals.",
164
+ "Extra": "Reduce landfill impact by choosing reusable products whenever possible."
165
+ }
166
+ }
167
+ waste_analyzation_v3 = {
168
+ "cardboard": {
169
+ "Recycling type": "Paper/Cardboard recycling",
170
+ "Disposal": "Remove packing tape when possible and place cardboard in dry storage until pickup day.",
171
+ "Tips": "Avoid leaving cardboard outdoors where rain could weaken fibers and ruin recyclability.",
172
+ "Extra": "Food-contaminated cardboard can often be composted instead of recycled."
173
+ },
174
+ "glass": {
175
+ "Recycling type": "Glass recycling",
176
+ "Disposal": "Sort by color only if required locally; rinse lightly and drop into the proper bin.",
177
+ "Tips": "Handle carefully to avoid breakage, as shattered glass is often not recyclable in curbside programs.",
178
+ "Extra": "Glass jars with metal clasps or rubber seals may need partial disassembly before recycling."
179
+ },
180
+ "metal": {
181
+ "Recycling type": "Metal recycling",
182
+ "Disposal": "Ensure metal food cans are clean and empty before placing in the recycling bin.",
183
+ "Tips": "Rinse cans briefly—no need for perfect cleaning—as this prevents pests and odors.",
184
+ "Extra": "Metal lids from jars should be recycled separately from the glass container."
185
+ },
186
+ "paper": {
187
+ "Recycling type": "Paper recycling",
188
+ "Disposal": "Place paper in the appropriate bin; keep shredded paper in a paper bag if accepted.",
189
+ "Tips": "Avoid mixing paper with wet waste like food scraps to maintain recyclability.",
190
+ "Extra": "Sticky notes and small scraps may or may not be accepted depending on the facility."
191
+ },
192
+ "plastic": {
193
+ "Recycling type": "Plastic recycling",
194
+ "Disposal": "Recycle only plastics accepted by your local program; rinse and drain them completely.",
195
+ "Tips": "Avoid recycling small plastic pieces under 2 inches—they may jam sorting machinery.",
196
+ "Extra": "Hard-to-recycle plastics may be collected through special community programs."
197
+ },
198
+ "trash": {
199
+ "Recycling type": "General waste / landfill",
200
+ "Disposal": "Place all non-recyclables in the trash bin; tie bags securely to avoid leakage.",
201
+ "Tips": "Do not place batteries, electronics, or sharp objects directly into household trash.",
202
+ "Extra": "Consider composting organic waste to reduce household trash volume."
203
+ }
204
+ }
205
+ analysis = [waste_analyzation_v3, waste_analyzation_v2, waste_analyzation]
206
 
207
  def classify_pipeline(image):
208
  global top_label
 
211
  top_label = max(predictions, key=predictions.get) # top-1
212
  return predictions
213
 
214
+ def analyze_pipeline(i):
215
  global top_label
216
 
217
  if top_label is None:
218
  return "Please classify an image first."
219
 
220
+ explanation = explain_recycling(top_label)
221
+
222
+ info = analysis[i][top_label]
223
  result = (
224
  f"• Recycling type: {info['Recycling type']}\n"
225
  f"• Disposal: {info['Disposal']}\n"
 
336
  )
337
 
338
  # BUTTON
339
+ analyze_result_btn = gr.Button("Analyze", variant="primary")
340
 
341
  analyze_result_btn.click(
342
  analyze_pipeline,
343
+ inputs=random.randint(0, 2),
344
  outputs=explain_output
345
  )
346