vlithish commited on
Commit
2ac26ce
·
verified ·
1 Parent(s): 89f852d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -3,34 +3,49 @@ from PIL import Image
3
  import random
4
 
5
  # -------------------------------
6
- # Simple classification logic
7
  # -------------------------------
8
  def classify_item(image, description):
9
- categories = ["Recyclable", "Compostable", "Trash"]
10
 
11
  if description:
12
  desc = description.lower()
13
- if "banana" in desc or "food" in desc or "peel" in desc or "leaf" in desc:
 
 
14
  category = "Compostable"
 
 
 
 
 
 
15
  elif "plastic" in desc or "bottle" in desc or "can" in desc or "metal" in desc:
16
  category = "Recyclable"
17
  elif "paper" in desc and "greasy" not in desc:
18
  category = "Recyclable"
 
 
19
  elif "pizza box" in desc or "styrofoam" in desc or "chip bag" in desc:
20
  category = "Trash"
 
 
21
  else:
22
  category = random.choice(categories)
 
23
  elif image:
24
- # Placeholder – replace with ML model if you train one
25
  category = random.choice(categories)
 
26
  else:
27
  return "No input", "⚠️ Please upload an image or type a description."
28
 
29
- # Tips
30
  tips = {
31
  "Recyclable": "♻️ Rinse before recycling. Check local rules for plastics.",
32
  "Compostable": "🌱 Add to compost bin or green waste collection.",
33
- "Trash": "🗑️ Not recyclable. Consider reusable alternatives."
 
34
  }
35
 
36
  return category, tips.get(category, "Check local disposal guidelines.")
@@ -40,11 +55,11 @@ def classify_item(image, description):
40
  # -------------------------------
41
  with gr.Blocks() as demo:
42
  gr.Markdown("# 🌍 EcoSort: Smart Waste Classifier")
43
- gr.Markdown("Upload an **image** or type a **description** to check if it's Recyclable, Compostable, or Trash.")
44
 
45
  with gr.Row():
46
  image_input = gr.Image(type="pil", label="Upload Image")
47
- text_input = gr.Textbox(label="Or type a description (e.g., 'banana peel', 'plastic bottle')")
48
 
49
  output_label = gr.Label(label="Prediction")
50
  output_tip = gr.Textbox(label="Eco-Friendly Tip", interactive=False)
@@ -53,3 +68,4 @@ with gr.Blocks() as demo:
53
  btn.click(fn=classify_item, inputs=[image_input, text_input], outputs=[output_label, output_tip])
54
 
55
  demo.launch()
 
 
3
  import random
4
 
5
  # -------------------------------
6
+ # Classification logic
7
  # -------------------------------
8
  def classify_item(image, description):
9
+ categories = ["Recyclable", "Compostable", "Trash", "Harmful"]
10
 
11
  if description:
12
  desc = description.lower()
13
+
14
+ # Compostable: fruits & vegetables
15
+ if "banana" in desc or "apple" in desc or "fruit" in desc or "vegetable" in desc or "food" in desc or "peel" in desc or "leaf" in desc:
16
  category = "Compostable"
17
+
18
+ # Harmful: medical + batteries
19
+ elif "syringe" in desc or "needle" in desc or "battery" in desc or "cell" in desc:
20
+ category = "Harmful"
21
+
22
+ # Recyclable
23
  elif "plastic" in desc or "bottle" in desc or "can" in desc or "metal" in desc:
24
  category = "Recyclable"
25
  elif "paper" in desc and "greasy" not in desc:
26
  category = "Recyclable"
27
+
28
+ # Trash
29
  elif "pizza box" in desc or "styrofoam" in desc or "chip bag" in desc:
30
  category = "Trash"
31
+
32
+ # Fallback
33
  else:
34
  category = random.choice(categories)
35
+
36
  elif image:
37
+ # Placeholder – replace with ML image model later
38
  category = random.choice(categories)
39
+
40
  else:
41
  return "No input", "⚠️ Please upload an image or type a description."
42
 
43
+ # Eco tips
44
  tips = {
45
  "Recyclable": "♻️ Rinse before recycling. Check local rules for plastics.",
46
  "Compostable": "🌱 Add to compost bin or green waste collection.",
47
+ "Trash": "🗑️ Not recyclable. Consider reusable alternatives.",
48
+ "Harmful": "⚠️ Special disposal needed. Take syringes, needles, and batteries to hazardous waste collection centers."
49
  }
50
 
51
  return category, tips.get(category, "Check local disposal guidelines.")
 
55
  # -------------------------------
56
  with gr.Blocks() as demo:
57
  gr.Markdown("# 🌍 EcoSort: Smart Waste Classifier")
58
+ gr.Markdown("Upload an **image** or type a **description** to check if it's Recyclable, Compostable, Trash, or Harmful.")
59
 
60
  with gr.Row():
61
  image_input = gr.Image(type="pil", label="Upload Image")
62
+ text_input = gr.Textbox(label="Or type a description (e.g., 'banana peel', 'plastic bottle', 'syringe')")
63
 
64
  output_label = gr.Label(label="Prediction")
65
  output_tip = gr.Textbox(label="Eco-Friendly Tip", interactive=False)
 
68
  btn.click(fn=classify_item, inputs=[image_input, text_input], outputs=[output_label, output_tip])
69
 
70
  demo.launch()
71
+