tegana commited on
Commit
f20117f
·
verified ·
1 Parent(s): d0df7d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -15
app.py CHANGED
@@ -3,31 +3,63 @@ import numpy as np
3
  from PIL import Image
4
  from tensorflow.keras.models import load_model
5
 
6
- # Load the trained model
7
  model = load_model("model.h5")
8
 
9
- # Define class labels
10
  class_names = ["Monkeypox", "Not Monkeypox"]
11
 
12
  def predict(img):
13
- # Resize image to match model input size
14
- img = img.resize((224, 224))
15
- img = np.array(img) / 255.0 # normalize
16
- img = np.expand_dims(img, axis=0)
17
 
18
  # Predict
19
- preds = model.predict(img)
20
 
21
- # Convert predictions to dictionary
22
  return {class_names[i]: float(preds[0][i]) for i in range(len(class_names))}
23
 
24
- demo = gr.Interface(
25
- fn=predict,
26
- inputs=gr.Image(type="pil"),
27
- outputs=gr.Label(num_top_classes=2),
28
- title="Monkeypox Classifier",
29
- description="Upload an image and the model will classify it as Monkeypox or Not."
30
- )
 
 
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  if __name__ == "__main__":
33
  demo.launch()
 
3
  from PIL import Image
4
  from tensorflow.keras.models import load_model
5
 
6
+ # Load the trained CNN model
7
  model = load_model("model.h5")
8
 
9
+ # Define class labels
10
  class_names = ["Monkeypox", "Not Monkeypox"]
11
 
12
  def predict(img):
13
+ # Resize & preprocess image
14
+ img_resized = img.resize((224, 224))
15
+ img_array = np.array(img_resized) / 255.0 # normalize
16
+ img_array = np.expand_dims(img_array, axis=0)
17
 
18
  # Predict
19
+ preds = model.predict(img_array)
20
 
21
+ # Return probabilities
22
  return {class_names[i]: float(preds[0][i]) for i in range(len(class_names))}
23
 
24
+ # -------- Gradio Interface --------
25
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
26
+ gr.Markdown(
27
+ """
28
+ <div style="text-align:center; padding: 15px; background: linear-gradient(90deg, #ff6f61, #ffcc70); border-radius: 12px;">
29
+ <h1 style="color:white;">🐵 Monkeypox Classifier</h1>
30
+ <p style="color:white; font-size:18px;">Upload or capture an image, and the model will classify it as <b>Monkeypox</b> or <b>Not Monkeypox</b>.</p>
31
+ </div>
32
+ """
33
+ )
34
 
35
+ with gr.Row():
36
+ with gr.Column():
37
+ input_img = gr.Image(type="pil", label="📸 Upload or Capture Image", sources=["upload", "webcam"])
38
+ predict_btn = gr.Button("🔍 Predict", elem_id="predict-btn")
39
+ with gr.Column():
40
+ output_label = gr.Label(num_top_classes=2, label="Prediction")
41
+
42
+ # Add custom CSS
43
+ demo.load(
44
+ lambda: None,
45
+ None,
46
+ None,
47
+ _js="""
48
+ () => {
49
+ let btn = document.getElementById("predict-btn");
50
+ if(btn){
51
+ btn.style.background = "linear-gradient(45deg, #36d1dc, #5b86e5)";
52
+ btn.style.color = "white";
53
+ btn.style.fontWeight = "bold";
54
+ btn.style.padding = "10px 20px";
55
+ btn.style.borderRadius = "12px";
56
+ }
57
+ }
58
+ """
59
+ )
60
+
61
+ predict_btn.click(fn=predict, inputs=input_img, outputs=output_label)
62
+
63
+ # Launch App
64
  if __name__ == "__main__":
65
  demo.launch()