deepi8 commited on
Commit
5dca65e
Β·
verified Β·
1 Parent(s): cc501cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -1,35 +1,47 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
- from PIL import Image
5
  import json
 
 
6
 
7
- # Load model and metadata
8
- model = tf.keras.models.load_model("keras_model.h5", compile=False)
9
  with open("metadata.json") as f:
10
  metadata = json.load(f)
11
  labels = metadata["labels"]
12
 
 
 
 
13
  # Prediction function
14
- def predict_image(image):
15
- image = image.convert("RGB").resize((224, 224))
16
- img_array = np.array(image) / 255.0
17
- img_array = np.expand_dims(img_array, axis=0)
18
-
19
- preds = model.predict(img_array)[0]
20
- pred_idx = np.argmax(preds)
21
- pred_label = labels[pred_idx]
22
- confidence = float(preds[pred_idx]) * 100
23
-
24
- return f"🟒 Predicted: {pred_label}", f"🎯 Accuracy: {confidence:.2f}%"
25
-
26
- # Gradio Interface
27
- demo = gr.Interface(
 
 
 
 
 
 
28
  fn=predict_image,
29
  inputs=gr.Image(type="pil", label="Upload Solar Panel Image"),
30
- outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Accuracy")],
 
 
 
31
  title="πŸ”† Solar Panel Condition Classifier",
32
  description="Upload a solar panel image to detect if it is Clean, Dusty, Bird-drop, Electrical Damage, Physical Damage, or Snow-Covered.",
33
  )
34
 
35
- demo.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
 
4
  import json
5
+ from PIL import Image
6
+ import tensorflowjs as tfjs
7
 
8
+ # Load metadata (class labels)
 
9
  with open("metadata.json") as f:
10
  metadata = json.load(f)
11
  labels = metadata["labels"]
12
 
13
+ # Load TensorFlow.js model
14
+ model = tfjs.converters.load_keras_model("model.json")
15
+
16
  # Prediction function
17
+ def predict_image(img):
18
+ try:
19
+ # Preprocess image
20
+ img = img.convert("RGB").resize((224, 224))
21
+ img_array = np.array(img) / 255.0
22
+ img_array = np.expand_dims(img_array, axis=0)
23
+
24
+ # Make prediction
25
+ predictions = model.predict(img_array)[0]
26
+ pred_idx = np.argmax(predictions)
27
+ pred_label = labels[pred_idx]
28
+ accuracy = float(predictions[pred_idx]) * 100
29
+
30
+ return f"βœ… Condition: {pred_label}", f"🎯 Accuracy: {accuracy:.2f}%"
31
+
32
+ except Exception as e:
33
+ return f"❌ Error: {str(e)}", ""
34
+
35
+ # UI
36
+ interface = gr.Interface(
37
  fn=predict_image,
38
  inputs=gr.Image(type="pil", label="Upload Solar Panel Image"),
39
+ outputs=[
40
+ gr.Textbox(label="Prediction"),
41
+ gr.Textbox(label="Accuracy")
42
+ ],
43
  title="πŸ”† Solar Panel Condition Classifier",
44
  description="Upload a solar panel image to detect if it is Clean, Dusty, Bird-drop, Electrical Damage, Physical Damage, or Snow-Covered.",
45
  )
46
 
47
+ interface.launch()