ant40526 commited on
Commit
b45c977
Β·
verified Β·
1 Parent(s): 7913799

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -1,27 +1,28 @@
1
  import gradio as gr
2
  import numpy as np
3
- from tensorflow.keras.models import load_model
4
  from tensorflow.keras.preprocessing import image
 
5
 
6
- # Load the model from local file (Hugging Face will cache it automatically)
7
- model = load_model("syaha/skin_cancer_detection_model")
8
 
9
  # Class names used in the model
10
  class_names = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
11
 
12
- def predict(img):
13
  img = img.resize((224, 224))
14
  img_array = image.img_to_array(img)
15
  img_array = np.expand_dims(img_array, axis=0) / 255.0
16
 
17
- predictions = model.predict(img_array)
18
  predicted_class = np.argmax(predictions, axis=1)[0]
19
  label = class_names[predicted_class]
20
  confidence = float(predictions[0][predicted_class])
21
 
22
  return {
23
  "label": label,
24
- "confidence": confidence
25
  }
26
 
27
  iface = gr.Interface(
@@ -29,7 +30,7 @@ iface = gr.Interface(
29
  inputs=gr.Image(type="pil"),
30
  outputs="json",
31
  title="Skin Cancer Classifier",
32
- description="Upload a skin lesion image to get the predicted class"
33
  )
34
 
35
  iface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ from keras.layers import TFSMLayer
4
  from tensorflow.keras.preprocessing import image
5
+ from PIL import Image
6
 
7
+ # Load the model from Hugging Face using TFSMLayer
8
+ model = TFSMLayer("https://huggingface.co/syaha/skin_cancer_detection_model/resolve/main", call_endpoint="serving_default")
9
 
10
  # Class names used in the model
11
  class_names = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
12
 
13
+ def predict(img: Image.Image):
14
  img = img.resize((224, 224))
15
  img_array = image.img_to_array(img)
16
  img_array = np.expand_dims(img_array, axis=0) / 255.0
17
 
18
+ predictions = model(img_array, training=False).numpy()
19
  predicted_class = np.argmax(predictions, axis=1)[0]
20
  label = class_names[predicted_class]
21
  confidence = float(predictions[0][predicted_class])
22
 
23
  return {
24
  "label": label,
25
+ "confidence": round(confidence * 100, 2)
26
  }
27
 
28
  iface = gr.Interface(
 
30
  inputs=gr.Image(type="pil"),
31
  outputs="json",
32
  title="Skin Cancer Classifier",
33
+ description="Upload a skin lesion image to get a prediction"
34
  )
35
 
36
  iface.launch()