Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
from tensorflow.keras.preprocessing import image | |
from tensorflow.keras.optimizers import Adam, Adamax | |
import numpy as np | |
# Load the trained model | |
model = tf.keras.models.load_model('Model1_kera.h5', compile=False) | |
model.compile(Adamax(learning_rate= 0.001), loss= 'categorical_crossentropy', metrics= ['accuracy']) | |
# model = tf.keras.models.load_model('Model1_kera.h5') | |
# Define the class names | |
classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma'] | |
# Function to preprocess the uploaded image and make predictions | |
def predict(img): | |
try: | |
img = img.resize((224, 224)) | |
img_array = tf.keras.preprocessing.image.img_to_array(img) | |
img_array = tf.expand_dims(img_array, 0) | |
predictions = model.predict(img_array) | |
class_labels = classes | |
# predictions = model.predict(img_array) | |
# predicted_class = classes[np.argmax(predictions[0])] | |
score = tf.nn.softmax(predictions[0]) | |
# print(f"{class_labels[tf.argmax(score)]}") | |
return f"Prediction: {class_labels[tf.argmax(score)]}" | |
except Exception as e: | |
return str(e) | |
# Create a Gradio interface | |
iface = gr.Interface( | |
fn=predict, | |
inputs=gr.Image(type='pil'), | |
outputs=gr.Textbox(label="Prediction"), | |
title="Lung and Colon Cancer Detection", | |
description="Upload an image of histopathological tissue to detect if it is a type of lung or colon cancer." | |
) | |
# Launch the interface | |
iface.launch(share=True) | |