Spaces:
Runtime error
Runtime error
import gradio as gr | |
import numpy as np | |
from tensorflow.keras.models import load_model | |
# Load the trained model | |
model = load_model('skin_model.h5') | |
# Define a function to make predictions | |
def predict(image): | |
# Preprocess the image | |
image = image / 255.0 | |
image = np.expand_dims(image, axis=0) | |
# Make a prediction using the model | |
prediction = model.predict(image) | |
# Get the predicted class label | |
if prediction[0][0] < 0.5: | |
label = 'Benign' | |
confidence = 1 - prediction[0][0] # Confidence for benign | |
else: | |
label = 'Malignant' | |
confidence = prediction[0][0] # Confidence for malignant | |
return {'label': label, 'confidence': float(confidence)} # Convert confidence to float | |
# Custom post-processing function to sort by confidence | |
def custom_postprocess(output): | |
sorted_output = sorted(output.items(), key=lambda x: x[1], reverse=True) | |
return f"{sorted_output[0][0]} ({sorted_output[0][1] * 100:.2f}%)" | |
examples = [["benign.jpg"], ["malignant.jpg"]] | |
# Define input and output components | |
image_input = gr.inputs.Image(shape=(150, 150)) | |
label_output = gr.outputs.Label(postprocess=custom_postprocess) | |
# Define a Gradio interface for user interaction | |
iface = gr.Interface( | |
fn=predict, | |
inputs=image_input, | |
outputs=label_output, | |
examples=examples, | |
title="Skin Cancer Classification", | |
description="Predicts whether a skin image is cancerous or not.", | |
theme="default", # Choose a theme: "default", "compact", "huggingface" | |
layout="vertical", # Choose a layout: "vertical", "horizontal", "double" | |
live=False # Set to True for live updates without clicking "Submit" | |
) | |
iface.launch() | |