ant40526 commited on
Commit
417de76
Β·
verified Β·
1 Parent(s): 57f4121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -1,36 +1,35 @@
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(
29
- fn=predict,
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()
 
1
+ import tensorflow as tf
2
+ from huggingface_hub import snapshot_download
3
+ from keras.preprocessing import image
4
  import numpy as np
5
+ import gradio as gr
6
  from keras.layers import TFSMLayer
 
 
7
 
8
+ # Download the model from Hugging Face Space to a local path
9
+ local_model_path = snapshot_download("syaha/skin_cancer_detection_model")
10
+
11
+ # Load the model using TFSMLayer
12
+ model = TFSMLayer(local_model_path, call_endpoint="serving_default")
13
 
14
+ # Class names for skin cancer classification (assuming you already know these)
15
  class_names = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
16
 
17
+ # Define a prediction function
18
+ def predict_skin_cancer(image_path):
19
+ # Preprocess the image for prediction
20
+ img = image.load_img(image_path, target_size=(224, 224)) # Resize to 224x224 (or use the correct input size)
21
  img_array = image.img_to_array(img)
22
+ img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize the image
23
+
24
+ # Make prediction
25
+ predictions = model.predict(img_array)
26
+ predicted_class = np.argmax(predictions, axis=1)[0] # Get the class index
27
+ predicted_label = class_names[predicted_class] # Map the index to the class name
28
+
29
+ return f"Predicted class: {predicted_label}"
30
+
31
+ # Set up Gradio interface to interact with the model
32
+ iface = gr.Interface(fn=predict_skin_cancer, inputs=gr.Image(type="filepath"), outputs="text", live=True)
 
 
 
 
 
 
 
 
33
 
34
+ # Launch the Gradio interface
35
  iface.launch()