| import gradio as gr |
| import torch |
| from transformers import ViTImageProcessor, ViTForImageClassification |
| from PIL import Image |
| import numpy as np |
| import os |
|
|
| |
| def load_model_safely(): |
| """Load model with fallback options and proper error handling""" |
| try: |
| |
| if os.path.exists("./waste-classification"): |
| print("Loading model from local clone...") |
| processor = ViTImageProcessor.from_pretrained("./waste-classification") |
| model = ViTForImageClassification.from_pretrained("./waste-classification") |
| print("Successfully loaded model from local clone") |
| return processor, model |
| except Exception as e: |
| print(f"Failed to load from local clone: {e}") |
| |
| try: |
| |
| print("Loading model from HuggingFace...") |
| processor = ViTImageProcessor.from_pretrained("watersplash/waste-classification", cache_dir="./cache") |
| model = ViTForImageClassification.from_pretrained("watersplash/waste-classification", cache_dir="./cache") |
| print("Successfully loaded model from HuggingFace") |
| return processor, model |
| except Exception as e: |
| print(f"Failed to load from HuggingFace: {e}") |
| |
| try: |
| |
| print("Loading base ViT model as fallback...") |
| processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k") |
| |
| |
| model = ViTForImageClassification.from_pretrained( |
| "google/vit-base-patch16-224-in21k", |
| num_labels=12, |
| id2label={ |
| "0": "battery", "1": "biological", "2": "brown-glass", "3": "cardboard", |
| "4": "clothes", "5": "green-glass", "6": "metal", "7": "paper", |
| "8": "plastic", "9": "shoes", "10": "trash", "11": "white-glass" |
| }, |
| label2id={ |
| "battery": "0", "biological": "1", "brown-glass": "2", "cardboard": "3", |
| "clothes": "4", "green-glass": "5", "metal": "6", "paper": "7", |
| "plastic": "8", "shoes": "9", "trash": "10", "white-glass": "11" |
| } |
| ) |
| print("Loaded base ViT model as fallback (untrained)") |
| return processor, model |
| except Exception as e: |
| print(f"Failed to load fallback model: {e}") |
| return None, None |
|
|
| |
| print("Initializing model...") |
| processor, model = load_model_safely() |
|
|
| |
| class_names = [ |
| 'Battery', 'Biological', 'Brown-glass', 'Cardboard', 'Clothes', |
| 'Green-glass', 'Metal', 'Paper', 'Plastic', 'Shoes', 'Trash', 'White-glass' |
| ] |
|
|
| def classify_waste(image): |
| """ |
| Classify waste image into one of 12 categories |
| """ |
| if processor is None or model is None: |
| return {"Error": "Model failed to load. Please try refreshing the page or contact support."} |
| |
| if image is None: |
| return {"Error": "Please upload an image."} |
| |
| try: |
| |
| if image.mode != 'RGB': |
| image = image.convert('RGB') |
| |
| |
| inputs = processor(images=image, return_tensors="pt") |
| |
| |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| predictions = torch.nn.functional.softmax(outputs.logits, dim=-1) |
| |
| |
| confidence_scores = predictions[0].tolist() |
| |
| |
| results = {} |
| for i, confidence in enumerate(confidence_scores): |
| class_name = class_names[i] |
| results[class_name] = confidence |
| |
| return results |
| |
| except Exception as e: |
| return {"Error": f"Classification failed: {str(e)}"} |
|
|
| def get_model_status(): |
| """Return model loading status for debugging""" |
| if processor is not None and model is not None: |
| return "โ
Model loaded successfully" |
| else: |
| return "โ Model failed to load" |
|
|
| |
| try: |
| model_status = get_model_status() |
| |
| |
| examples = [] |
| if os.path.exists("green_glass.png"): |
| examples.append(["green_glass.png"]) |
| |
| interface = gr.Interface( |
| fn=classify_waste, |
| inputs=gr.Image(type="pil", label="Upload Waste Image"), |
| outputs=gr.Label(num_top_classes=5, label="Waste Classification Results"), |
| title="๐๏ธ AI Waste Classification", |
| description=f""" |
| ### Waste Classification using Vision Transformer (ViT) |
| |
| **Model Status:** {model_status} |
| |
| Upload an image of waste and get AI-powered classification into 12 categories: |
| |
| **Categories:** Battery, Biological, Brown-glass, Cardboard, Clothes, Green-glass, Metal, Paper, Plastic, Shoes, Trash, White-glass |
| |
| **Model Details:** |
| - Architecture: Vision Transformer (ViT) |
| - Accuracy: 98% on Garbage Classification dataset |
| - Model: watersplash/waste-classification |
| - Base: google/vit-base-patch16-224-in21k |
| |
| *Tip: For best results, use clear images with good lighting.* |
| """, |
| examples=examples, |
| theme=gr.themes.Soft(), |
| allow_flagging="never", |
| cache_examples=False |
| ) |
| |
| print("Gradio interface created successfully") |
| |
| except Exception as e: |
| print(f"Error creating Gradio interface: {e}") |
| |
| def show_error(image): |
| return {"Error": "Application failed to initialize properly. Please contact support."} |
| |
| interface = gr.Interface( |
| fn=show_error, |
| inputs=gr.Image(type="pil", label="Upload Waste Image"), |
| outputs=gr.Label(label="Error"), |
| title="๐๏ธ AI Waste Classification - Error", |
| description="The application encountered an error during initialization." |
| ) |
|
|
| if __name__ == "__main__": |
| try: |
| print("Launching Gradio interface...") |
| interface.launch( |
| server_name="0.0.0.0", |
| server_port=7860, |
| show_error=True, |
| share=False |
| ) |
| except Exception as e: |
| print(f"Failed to launch interface: {e}") |
| |
| interface.launch() |