Spaces:
Sleeping
Sleeping
# import gradio as gr | |
# import tensorflow as tf | |
# from tensorflow.keras.preprocessing import image | |
# import numpy as np | |
# # Load the trained model | |
# model = tf.keras.models.load_model('./model12_acc99_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): | |
# img = img.resize((224, 224)) | |
# img_array = image.img_to_array(img) | |
# img_array = np.expand_dims(img_array, axis=0) | |
# img_array = img_array / 255.0 # Normalize the image | |
# predictions = model.predict(img_array) | |
# predicted_class = classes[np.argmax(predictions[0])] | |
# return predicted_class | |
# # 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() | |
# import gradio as gr | |
# import tensorflow as tf | |
# from tensorflow.keras.preprocessing import image | |
# import numpy as np | |
# import logging | |
# # Set up logging | |
# logging.basicConfig(level=logging.DEBUG) | |
# # Initialize the model variable | |
# model = None | |
# # Load the trained model | |
# try: | |
# model_path = './model1_kera.h5' | |
# logging.info(f"Loading model from: {model_path}") | |
# model = tf.keras.models.load_model(model_path) | |
# logging.info("Model loaded successfully.") | |
# except Exception as e: | |
# logging.error(f"Error loading model: {e}") | |
# # 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): | |
# global model | |
# try: | |
# logging.debug("Received image for prediction.") | |
# # Resize and preprocess the image | |
# img = img.resize((224, 224)) | |
# img_array = image.img_to_array(img) | |
# img_array = np.expand_dims(img_array, axis=0) | |
# logging.debug("Image preprocessed successfully.") | |
# # Ensure the model is loaded | |
# if model is None: | |
# raise ValueError("Model is not loaded properly.") | |
# # Make predictions | |
# predictions = model.predict(img_array) | |
# predicted_class_index = np.argmax(predictions[0]) | |
# predicted_class = classes[predicted_class_index] | |
# logging.debug(f"Prediction successful: {predicted_class}") | |
# # Return the predicted class and the raw predictions | |
# return predicted_class, predictions[0].tolist() | |
# except Exception as e: | |
# logging.error(f"Error during prediction: {e}") | |
# # Print the error message in the output | |
# return str(e), str(e) | |
# # Create a Gradio interface | |
# iface = gr.Interface( | |
# fn=predict, | |
# inputs=gr.Image(type='pil'), | |
# outputs=[ | |
# gr.Textbox(label="Prediction"), | |
# gr.Label(label="Raw Predictions") | |
# ], | |
# 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() | |
import gradio as gr | |
import tensorflow as tf | |
from tensorflow.keras.preprocessing import image as keras_image | |
from PIL import Image | |
import numpy as np | |
import logging | |
import os | |
# Set up logging | |
logging.basicConfig(level=logging.DEBUG) | |
# Initialize the model variable | |
model = None | |
# Function to load the model | |
def load_model(): | |
global model | |
model_path = r'Model1_kera.h5' # Replace with the actual path to your model file | |
if not os.path.exists(model_path): | |
logging.error(f"Model file does not exist at path: {model_path}") | |
return False | |
try: | |
logging.info(f"Loading model from: {model_path}") | |
model = tf.keras.models.load_model(model_path) | |
logging.info("Model loaded successfully.") | |
return True | |
except Exception as e: | |
logging.error(f"Error loading model: {e}") | |
return False | |
# Load the model when the script starts | |
model_loaded = load_model() | |
# 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): | |
global model | |
try: | |
logging.debug("Received image for prediction.") | |
# Resize and preprocess the image | |
img = img.resize((224, 224)) | |
img_array = keras_image.img_to_array(img) | |
img_array = tf.expand_dims(img_array, 0) # Add batch dimension | |
logging.debug("Image preprocessed successfully.") | |
# Ensure the model is loaded | |
if model is None: | |
raise ValueError("Model is not loaded properly.") | |
# Make predictions | |
predictions = model.predict(img_array) | |
score = tf.nn.softmax(predictions[0]) | |
predicted_class_index = tf.argmax(score).numpy() | |
predicted_class = classes[predicted_class_index] | |
logging.debug(f"Prediction successful: {predicted_class}") | |
# Return the predicted class and the raw predictions | |
return predicted_class, predictions[0].tolist() | |
except Exception as e: | |
logging.error(f"Error during prediction: {e}") | |
# Print the error message in the output | |
return str(e), str(e) | |
# Create a Gradio interface | |
iface = gr.Interface( | |
fn=predict, | |
inputs=gr.Image(type='pil'), | |
outputs=[ | |
gr.Textbox(label="Prediction"), | |
gr.Label(label="Raw Predictions") | |
], | |
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() | |