Spaces:
Sleeping
Sleeping
Download app.py from deepi8/solar-website: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/deepi8/solar-website/resolve/main/app.py
- Command line
-
hf download hf://spaces/deepi8/solar-website/app.py
-
curl -L -o app.py https://huggingface.co/spaces/deepi8/solar-website/resolve/main/app.py
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() | |