solar-website / app.py
deepi8's picture
Update app.py
5dca65e verified
Raw History Blame Contribute Delete
1.36 kB
import gradio as gr
import tensorflow as tf
import numpy as np
import json
from PIL import Image
import tensorflowjs as tfjs
# Load metadata (class labels)
with open("metadata.json") as f:
metadata = json.load(f)
labels = metadata["labels"]
# Load TensorFlow.js model
model = tfjs.converters.load_keras_model("model.json")
# Prediction function
def predict_image(img):
try:
# Preprocess image
img = img.convert("RGB").resize((224, 224))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Make prediction
predictions = model.predict(img_array)[0]
pred_idx = np.argmax(predictions)
pred_label = labels[pred_idx]
accuracy = float(predictions[pred_idx]) * 100
return f"βœ… Condition: {pred_label}", f"🎯 Accuracy: {accuracy:.2f}%"
except Exception as e:
return f"❌ Error: {str(e)}", ""
# UI
interface = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil", label="Upload Solar Panel Image"),
outputs=[
gr.Textbox(label="Prediction"),
gr.Textbox(label="Accuracy")
],
title="πŸ”† Solar Panel Condition Classifier",
description="Upload a solar panel image to detect if it is Clean, Dusty, Bird-drop, Electrical Damage, Physical Damage, or Snow-Covered.",
)
interface.launch()