Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,35 @@
|
|
1 |
-
import
|
|
|
|
|
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 |
-
#
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
# Class names
|
11 |
class_names = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
img_array = image.img_to_array(img)
|
16 |
-
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return {
|
24 |
-
|
25 |
-
|
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()
|