import gradio as gr import numpy as np from PIL import Image import tensorflow as tf import logging import os # Set up logging to capture errors logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info("Starting application initialization") # Load class names from class_indices.txt try: logger.info("Loading class names from class_indices.txt") with open("class_indices.txt", "r") as f: class_names = [line.strip() for line in f.readlines() if line.strip()] logger.info(f"Class names loaded: {class_names}") except Exception as e: logger.error(f"Failed to load class_indices.txt: {str(e)}") raise Exception(f"Failed to load class_indices.txt: {str(e)}") # Load the Keras .h5 model try: logger.info("Loading model: garbage_classifier.h5") model = tf.keras.models.load_model("garbage_classifier.h5", compile=False) logger.info("Model loaded successfully") except Exception as e: logger.error(f"Failed to load model: {str(e)}") raise Exception(f"Failed to load model: {str(e)}") def predict_image(image: Image.Image): try: logger.info("Processing image for prediction") # Preprocess the image img = image.convert("RGB").resize((128, 128)) arr = np.array(img).astype("float32") / 255.0 arr = np.expand_dims(arr, axis=0) # Make prediction preds = model.predict(arr)[0] logger.info("Prediction completed") # Get predicted class and confidence pred_class_idx = np.argmax(preds) pred_class = class_names[pred_class_idx] confidence = float(preds[pred_class_idx]) # Create HTML for per-class probabilities with progress bars prob_html = "" for i in range(len(class_names)): class_name = class_names[i] prob = float(preds[i]) prob_html += f'