import gradio as gr import tensorflow as tf from tensorflow.keras.applications.mobilenet_v2 import preprocess_input from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing import image import numpy as np from PIL import Image # Load the model model = load_model("keras_model.h5", compile=False) # Load the labels class_names = open("labels.txt", "r").readlines() def predict_image(img): # Resize the image img = Image.fromarray(img.astype('uint8'), 'RGB') img = img.resize((224, 224), resample=Image.BILINEAR) # Preprocess the image img_array = image.img_to_array(img) img_array = preprocess_input(img_array) # Expand the dimensions to create a batch of size 1 img_batch = tf.expand_dims(img_array, axis=0) # Predict the class probabilities preds = model.predict(img_batch) class_idx = tf.argmax(preds, axis=1)[0] class_name = class_names[class_idx].strip() confidence_score = float(preds[0][class_idx]) # convert to float if class_idx == 5: return "We couldn't detect anything from this image. Please try with a different image." elif confidence_score >= 0.70: return f"There is a {confidence_score*100:.2f}% chance for this image to be {class_name}. Even though it has good accuracy, please consult a doctor for confirmation." elif 0.50 <= confidence_score < 0.70: return f"There is a {confidence_score*100:.2f}% chance for this image to be {class_name}, but considering the accuracy, it's better to consult a doctor before using our service." else: return f"There is a {confidence_score*100:.2f}% chance for this image to be {class_name}. Since the accuracy is very low, please consider a doctor's advice and we recommend you not to rely on our predictions." # Launch the Gradio interface iface = gr.Interface(fn=predict_image, inputs="image", outputs="text", title="Bee4Med - Skin Disease Classifier", description="This is a machine learning model that predicts skin disease from an image(limited dataset). However, please note that there are chances that the predictions may go wrong, and we strongly recommend you to consult a doctor for confirmation.") # Launch the interface iface.launch()