vlithish commited on
Commit
a707f72
·
verified ·
1 Parent(s): ce2f93e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -44
app.py CHANGED
@@ -1,55 +1,79 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- # Categories
4
- compostable = ["vegetable", "fruit", "food", "leaves"]
5
- recyclable = ["plastic", "paper", "glass", "metal", "bottle", "can"]
6
- harmful = ["syringe", "battery", "medical", "chemical"]
7
-
8
- def classify_waste(input_type, image_path, text_input):
9
- """
10
- Classify waste based on either an uploaded image or text description.
11
- """
12
- # Choose which input to use
13
- if input_type == "Image Upload" and image_path:
14
- # Demo: classify based on filename
15
- filename = image_path.lower()
16
- if any(word in filename for word in compostable):
17
- return "✅ Compostable Waste (Green Bin)"
18
- elif any(word in filename for word in recyclable):
19
- return "♻️ Recyclable Waste (Blue Bin)"
20
- elif any(word in filename for word in harmful):
21
- return "⚠️ Harmful/Non-Decomposable Waste (Red Bin)"
22
- else:
23
- return "🚮 General Waste (Grey Bin)"
24
  elif input_type == "Text Description" and text_input:
25
- item = text_input.lower()
26
- if item in compostable:
27
- return "✅ Compostable Waste (Green Bin)"
28
- elif item in recyclable:
29
- return "♻️ Recyclable Waste (Blue Bin)"
30
- elif item in harmful:
31
- return "⚠️ Harmful/Non-Decomposable Waste (Red Bin)"
32
- else:
33
- return "🚮 General Waste (Grey Bin)"
34
  else:
35
  return "❌ Please provide input for the selected option."
36
 
37
  # Gradio UI
38
- input_type = gr.Radio(["Image Upload", "Text Description"], label="Select input type")
39
-
40
- image_input = gr.Image(type="filepath", label="Upload a photo of your waste item")
41
- text_input = gr.Textbox(label="Type the waste item description")
42
-
43
- iface = gr.Interface(
44
- fn=classify_waste,
45
- inputs=[input_type, image_input, text_input],
46
- outputs="text",
47
- title="♻️ Eco-Friendly Waste Classifier",
48
- description="Upload a photo OR type a description of a waste item to see which bin it belongs to!"
49
- )
50
-
 
 
 
 
 
 
 
 
 
 
 
51
  if __name__ == "__main__":
52
- iface.launch()
53
 
54
 
55
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load a pre-trained image classifier (small + fast for demo)
5
+ # You can replace this with a waste-specific model if available
6
+ image_classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
7
+
8
+ # Categories with more examples
9
+ compostable = ["vegetable", "vegetables", "tomato", "onion", "potato", "carrot",
10
+ "fruit", "apple", "banana", "orange", "mango", "food", "leaves", "cardboard", "paper"]
11
+ recyclable = ["plastic", "bottle", "can", "glass", "metal", "aluminum", "tin", "carton"]
12
+ harmful = ["syringe", "battery", "medical", "medicine", "chemical", "paint", "electronics", "toxic"]
13
+
14
+ def classify_text(item):
15
+ """Classify based on text input"""
16
+ item = item.lower()
17
+ if any(word in item for word in compostable):
18
+ return "✅ Compostable Waste (Green Bin)"
19
+ elif any(word in item for word in recyclable):
20
+ return "♻️ Recyclable Waste (Blue Bin)"
21
+ elif any(word in item for word in harmful):
22
+ return "⚠️ Harmful/Non-Decomposable Waste (Red Bin)"
23
+ else:
24
+ return "🚮 Unknown Waste (Grey Bin)"
25
+
26
+ def classify_image(image_path):
27
+ """Classify based on uploaded or webcam image"""
28
+ preds = image_classifier(image_path, top_k=5)
29
+ labels = [p["label"].lower() for p in preds]
30
+
31
+ # Match predictions with categories
32
+ if any(word in labels for word in compostable):
33
+ return "✅ Compostable Waste (Green Bin)"
34
+ elif any(word in labels for word in recyclable):
35
+ return "♻️ Recyclable Waste (Blue Bin)"
36
+ elif any(word in labels for word in harmful):
37
+ return "⚠️ Harmful/Non-Decomposable Waste (Red Bin)"
38
+ else:
39
+ return "🚮 Unknown Waste (Grey Bin)"
40
 
41
+ def classify_waste(input_type, image_input, text_input):
42
+ """Main function to choose input type"""
43
+ if input_type == "Image Upload / Webcam" and image_input is not None:
44
+ return classify_image(image_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  elif input_type == "Text Description" and text_input:
46
+ return classify_text(text_input)
 
 
 
 
 
 
 
 
47
  else:
48
  return "❌ Please provide input for the selected option."
49
 
50
  # Gradio UI
51
+ with gr.Blocks() as demo:
52
+ gr.Markdown("# ♻️ Eco-Friendly Waste Classifier")
53
+ gr.Markdown("Upload a photo (or use webcam) **OR** type a description to see which bin the item belongs to!")
54
+
55
+ input_type = gr.Radio(
56
+ ["Image Upload / Webcam", "Text Description"],
57
+ label="Select input type", value="Text Description"
58
+ )
59
+
60
+ with gr.Row():
61
+ image_input = gr.Image(type="filepath", label="Upload or capture photo", sources=["upload", "webcam"])
62
+ text_input = gr.Textbox(label="Type the waste item description")
63
+
64
+ output = gr.Textbox(label="Classification Result")
65
+
66
+ # When user changes
67
+ classify_btn = gr.Button("Classify")
68
+ classify_btn.click(
69
+ fn=classify_waste,
70
+ inputs=[input_type, image_input, text_input],
71
+ outputs=output
72
+ )
73
+
74
+ # Launch
75
  if __name__ == "__main__":
76
+ demo.launch()
77
 
78
 
79