import gradio as gr import tensorflow as tf import numpy as np from PIL import Image # Initial parameters for pretrained model IMG_SIZE = 300 labelCOVID = { 'COVID-19': 0, 'Normal': 1, 'Viral Pneumonia': 2, 'Lung_Opacity': 3 } # Load the model from the H5 file model = tf.keras.models.load_model('model/covid.h5') # Define the prediction function def predict(img): img_height = 150 img_width = 150 # Convert the NumPy array to a PIL Image object pil_img = Image.fromarray(img) # Resize the image using the PIL Image object pil_img = pil_img.resize((img_height, img_width)) # Convert the PIL Image object to a NumPy array x = tf.keras.preprocessing.image.img_to_array(pil_img) x = x.reshape(1, img_height, img_width, 3) np.set_printoptions(formatter={'float': '{: 0.3f}'.format}) predi = model.predict(x) accuracy_of_class = '{:.1f}'.format(predi[0][np.argmax(predi)] * 100) + "%" classes = list(labelCOVID.keys())[np.argmax(predi)] context = { 'predictedLabel': classes, # 'y_class': y_class, # 'z_class': z_class, 'accuracy_of_class': accuracy_of_class } return context demo = gr.Interface(fn=predict, inputs="image", outputs="text" , examples=[["c1.png"],["c2.jpeg"]],) demo.launch()