metal / app.py
ant40526's picture
Update app.py
417de76 verified
import tensorflow as tf
from huggingface_hub import snapshot_download
from keras.preprocessing import image
import numpy as np
import gradio as gr
from keras.layers import TFSMLayer
# Download the model from Hugging Face Space to a local path
local_model_path = snapshot_download("syaha/skin_cancer_detection_model")
# Load the model using TFSMLayer
model = TFSMLayer(local_model_path, call_endpoint="serving_default")
# Class names for skin cancer classification (assuming you already know these)
class_names = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
# Define a prediction function
def predict_skin_cancer(image_path):
# Preprocess the image for prediction
img = image.load_img(image_path, target_size=(224, 224)) # Resize to 224x224 (or use the correct input size)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize the image
# Make prediction
predictions = model.predict(img_array)
predicted_class = np.argmax(predictions, axis=1)[0] # Get the class index
predicted_label = class_names[predicted_class] # Map the index to the class name
return f"Predicted class: {predicted_label}"
# Set up Gradio interface to interact with the model
iface = gr.Interface(fn=predict_skin_cancer, inputs=gr.Image(type="filepath"), outputs="text", live=True)
# Launch the Gradio interface
iface.launch()