import tensorflow as tf import gradio as gr import numpy as np from PIL import Image from tensorflow.keras.models import load_model # Load your trained model and label map path="skinDiseaseDetection (1).h5" model = load_model(path) label_map={0: 'pigmented benign keratosis', 1: 'melanoma', 2: 'vascular lesion', 3: 'actinic keratosis', 4: 'squamous cell carcinoma', 5: 'basal cell carcinoma', 6: 'seborrheic keratosis', 7: 'dermatofibroma', 8: 'nevus'} def predict_image(image): """ Predict the class of an uploaded image of a skin lesion. Parameters: - image: The uploaded image in PIL format. Returns: - predicted_class: The name of the predicted class or an error message. - confidence: The confidence score of the prediction (0 to 1) or None. - prediction_type: The type of prediction or an error message. """ if image is None: return "Please upload an image.", None, "No Diagnosis" # Preprocess the image image = image.resize((100, 75)) # Resize to model's expected input size image_array = np.asarray(image) image_array = (image_array - np.mean(image_array)) / np.std(image_array) # Normalize image_array = np.expand_dims(image_array, axis=0) # Add batch dimension # Make prediction using the loaded model predictions = model.predict(image_array) predicted_index = np.argmax(predictions, axis=1)[0] confidence = np.max(predictions) predicted_class = label_map[predicted_index] # Define classes for benign and malignant lesions benign_classes = [ 'pigmented benign keratosis', 'vascular lesion', 'actinic keratosis', 'seborrheic keratosis', 'dermatofibroma', 'nevus' ] malignant_classes = [ 'melanoma', 'squamous cell carcinoma', 'basal cell carcinoma' ] # Determine the type of prediction if predicted_class in benign_classes: prediction_type = 'Benign Neoplasm' elif predicted_class in malignant_classes: prediction_type = 'Malignant Neoplasm' else: prediction_type = 'Unknown Neoplasm' return predicted_class, float(f"{confidence:.2f}"), prediction_type # Format confidence here # Example images and their descriptions examples = [ ["bcc.jpg"], ["d.jpg"], ["m.PNG"], ["sck.jpg"] ] # Set up the Gradio interface iface = gr.Interface( fn=predict_image, inputs=gr.Image(type="pil", label="Upload Skin Lesion Image"), outputs=[ gr.Label(label="Predicted Class"), gr.Number(label="Confidence Score", precision=2), gr.Textbox(label="Diagnosis Type", interactive=False) ], title="Skin Cancer Image Classification", description="Upload an image of a skin lesion to predict its type and determine if the diagnosis is Benign or Malignant.", theme="default", # Use a valid theme like "default" or "compact" allow_flagging="never", # Optional: Disable flagging for a cleaner UI examples=examples , css=""" .gradio-container { font-family: 'Arial', sans-serif; background-color: #ddebf7; border-radius: 10px; padding: 20px; } .gr-button { background-color: #4CAF50; color: white; } .gr-image { border: 2px solid #ddd; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); } .output-textbox { font-size: 1.2em; text-align: center; color: #333; } """, ) # Launch the Gradio interface iface.launch()