syaha's picture
Update app.py
39fca12 verified
raw
history blame
1.69 kB
import gradio as gr
from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image
# Load model from Hugging Face model repository
model = load_model("syaha/skin_cancer_detection_model")
# Preprocess function
def preprocess_image(image):
image = image.resize((224, 224)) # Resize to match model input size
image = np.array(image) / 255.0 # Normalize
image = np.expand_dims(image, axis=0) # Add batch dimension
return image
# Predict function
def predict_image(image):
img = preprocess_image(image)
prediction = model.predict(img)
predicted_class = np.argmax(prediction, axis=1)[0]
class_label = disease_info[predicted_class]['name']
description = disease_info[predicted_class]['description']
return f"Prediction: {class_label}\nDescription: {description}"
# Disease information mapping
disease_info = {
0: {'name': 'Actinic Keratoses (akiec)', 'description': 'Rough, scaly patches caused by sun exposure.'},
1: {'name': 'Basal Cell Carcinoma (bcc)', 'description': 'A type of skin cancer that rarely spreads.'},
2: {'name': 'Benign Keratosis (bkl)', 'description': 'Non-cancerous skin lesions.'},
3: {'name': 'Dermatofibroma (df)', 'description': 'A benign lesion often on the legs.'},
4: {'name': 'Melanocytic Nevus (nv)', 'description': 'Common mole, can develop into melanoma.'},
5: {'name': 'Vascular Lesions (vasc)', 'description': 'Blood vessel-related skin growths.'},
6: {'name': 'Melanoma (mel)', 'description': 'Most dangerous skin cancer, early detection is key.'}
}
# Gradio interface
iface = gr.Interface(fn=predict_image, inputs="image", outputs="text")
iface.launch()