Update app.py

#20
by Muthuraja18 - opened
Files changed (1) hide show
  1. app.py +30 -10
app.py CHANGED
@@ -29,6 +29,16 @@ TIPS = {
29
  'cardboard': 'Recycle cardboard to reduce waste.'
30
  }
31
 
 
 
 
 
 
 
 
 
 
 
32
  # -----------------------------
33
  # PAGE SETTINGS
34
  # -----------------------------
@@ -41,9 +51,6 @@ st.set_page_config(
41
  # VALIDATE DATASET STRUCTURE
42
  # -----------------------------
43
  def validate_dataset():
44
- """
45
- Ensure dataset folder contains 6 required class subfolders
46
- """
47
  if not os.path.exists(DATASET_DIR):
48
  st.error(f"❌ Dataset folder '{DATASET_DIR}' not found.")
49
  st.stop()
@@ -87,7 +94,7 @@ def train_and_save_model():
87
  DATASET_DIR,
88
  target_size=IMG_SIZE,
89
  batch_size=BATCH_SIZE,
90
- classes=CLASSES, # FORCE CORRECT CLASSES
91
  class_mode='categorical',
92
  subset='training',
93
  shuffle=True
@@ -262,19 +269,31 @@ if uploaded_file is not None:
262
  with st.spinner("πŸ” Analyzing waste type..."):
263
  predicted_class, confidence, probabilities = predict_waste(image)
264
 
 
265
  st.subheader("πŸ“Š Prediction Scores")
266
 
267
  for i, class_name in enumerate(CLASSES):
268
  st.progress(float(probabilities[i]))
269
  st.write(f"{class_name.upper()}: {probabilities[i] * 100:.2f}%")
270
 
 
271
  st.success(f"βœ… Predicted Type: {predicted_class.upper()}")
272
  st.info(f"🎯 Confidence: {confidence:.2f}%")
273
  st.write(f"πŸ“ Uploaded File: {uploaded_file.name}")
274
 
 
275
  st.subheader("🌱 Sustainability Suggestion")
276
  st.write(TIPS.get(predicted_class, "Dispose responsibly."))
277
 
 
 
 
 
 
 
 
 
 
278
  except UnidentifiedImageError:
279
  st.error("❌ Invalid image file. Upload JPG, JPEG, or PNG.")
280
 
@@ -290,12 +309,13 @@ st.write("""
290
  Your dataset folder should look like:
291
 
292
  dataset-resized/
293
- β”œβ”€β”€ cardboard/
294
- β”œβ”€β”€ glass/
295
- β”œβ”€β”€ metal/
296
- β”œβ”€β”€ paper/
297
- β”œβ”€β”€ plastic/
298
- └── trash/
 
299
  """)
300
 
301
  # -----------------------------
 
29
  'cardboard': 'Recycle cardboard to reduce waste.'
30
  }
31
 
32
+ # AI Eco Insights
33
+ AI_MESSAGES = {
34
+ 'plastic': "πŸ€– AI Insight: This appears to be plastic waste. Recycling plastic helps reduce pollution and protects oceans.",
35
+ 'paper': "πŸ€– AI Insight: Paper waste detected. Recycling paper saves trees and reduces landfill burden.",
36
+ 'metal': "πŸ€– AI Insight: Metal detected. Metal recycling conserves raw materials and energy.",
37
+ 'glass': "πŸ€– AI Insight: Glass waste identified. Glass is highly recyclable and reusable.",
38
+ 'trash': "πŸ€– AI Insight: General waste detected. Proper disposal minimizes environmental damage.",
39
+ 'cardboard': "πŸ€– AI Insight: Cardboard detected. Recycling cardboard supports sustainable packaging."
40
+ }
41
+
42
  # -----------------------------
43
  # PAGE SETTINGS
44
  # -----------------------------
 
51
  # VALIDATE DATASET STRUCTURE
52
  # -----------------------------
53
  def validate_dataset():
 
 
 
54
  if not os.path.exists(DATASET_DIR):
55
  st.error(f"❌ Dataset folder '{DATASET_DIR}' not found.")
56
  st.stop()
 
94
  DATASET_DIR,
95
  target_size=IMG_SIZE,
96
  batch_size=BATCH_SIZE,
97
+ classes=CLASSES,
98
  class_mode='categorical',
99
  subset='training',
100
  shuffle=True
 
269
  with st.spinner("πŸ” Analyzing waste type..."):
270
  predicted_class, confidence, probabilities = predict_waste(image)
271
 
272
+ # Prediction Scores
273
  st.subheader("πŸ“Š Prediction Scores")
274
 
275
  for i, class_name in enumerate(CLASSES):
276
  st.progress(float(probabilities[i]))
277
  st.write(f"{class_name.upper()}: {probabilities[i] * 100:.2f}%")
278
 
279
+ # Main Output
280
  st.success(f"βœ… Predicted Type: {predicted_class.upper()}")
281
  st.info(f"🎯 Confidence: {confidence:.2f}%")
282
  st.write(f"πŸ“ Uploaded File: {uploaded_file.name}")
283
 
284
+ # Sustainability Tip
285
  st.subheader("🌱 Sustainability Suggestion")
286
  st.write(TIPS.get(predicted_class, "Dispose responsibly."))
287
 
288
+ # AI Environmental Analysis
289
+ st.subheader("πŸ€– AI Environmental Analysis")
290
+ st.success(
291
+ AI_MESSAGES.get(
292
+ predicted_class,
293
+ "AI recommends responsible disposal."
294
+ )
295
+ )
296
+
297
  except UnidentifiedImageError:
298
  st.error("❌ Invalid image file. Upload JPG, JPEG, or PNG.")
299
 
 
309
  Your dataset folder should look like:
310
 
311
  dataset-resized/
312
+ └── dataset-resized/
313
+ β”œβ”€β”€ cardboard/
314
+ β”œβ”€β”€ glass/
315
+ β”œβ”€β”€ metal/
316
+ β”œβ”€β”€ paper/
317
+ β”œβ”€β”€ plastic/
318
+ └── trash/
319
  """)
320
 
321
  # -----------------------------