import gradio as gr import tensorflow as tf import numpy as np from PIL import Image import json # --- Load metadata (class names) --- with open("metadata.json") as f: metadata = json.load(f) labels = metadata["labels"] # --- Load Teachable Machine TensorFlow.js model --- import tensorflowjs as tfjs print("Loading model...") model = tfjs.converters.load_keras_model("model.json") print("✅ Model loaded successfully!") # --- Prediction function --- def predict_image(img): img = img.resize((224, 224)) img = np.array(img) / 255.0 img = np.expand_dims(img, axis=0) preds = model.predict(img)[0] confidences = {labels[i]: float(preds[i]) for i in range(len(labels))} return confidences # --- Gradio Web Interface --- demo = gr.Interface( fn=predict_image, inputs=gr.Image(type="pil", label="Upload Solar Panel Image"), outputs=gr.Label(num_top_classes=3, label="Predictions"), title="🔆 Solar Panel Condition Classifier", description="Upload an image to check if the solar panel is Clean, Dusty, Bird-drop, Electrical Damage, Physical Damage, or Snow-Covered." ) if __name__ == "__main__": demo.launch()