Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -103,45 +103,73 @@
|
|
103 |
# # Launch the interface
|
104 |
# iface.launch()
|
105 |
import gradio as gr
|
106 |
-
from transformers import pipeline
|
107 |
-
import numpy as np
|
108 |
-
from PIL import Image
|
109 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
# Function to preprocess the image and make predictions
|
114 |
-
def classify_LC_image(img):
|
115 |
-
try:
|
116 |
-
# Preprocess the image (resize, normalize, etc.)
|
117 |
-
img = image.resize((224,224))
|
118 |
-
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
119 |
-
img_array = tf.expand_dims(img_array, 0)
|
120 |
|
|
|
|
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
class_label = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma'][class_idx]
|
129 |
-
confidence = predictions[0][class_idx]
|
130 |
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
except Exception as e:
|
133 |
-
|
|
|
|
|
134 |
|
135 |
-
#
|
136 |
iface = gr.Interface(
|
137 |
-
fn=
|
138 |
-
inputs=
|
139 |
-
outputs=
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
|
|
144 |
)
|
145 |
|
146 |
-
#
|
147 |
-
iface.launch(
|
|
|
103 |
# # Launch the interface
|
104 |
# iface.launch()
|
105 |
import gradio as gr
|
|
|
|
|
|
|
106 |
import tensorflow as tf
|
107 |
+
from tensorflow.keras.preprocessing import image as keras_image
|
108 |
+
from PIL import Image
|
109 |
+
import numpy as np
|
110 |
+
import logging
|
111 |
|
112 |
+
# Set up logging
|
113 |
+
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
# Initialize the model variable
|
116 |
+
model = None
|
117 |
|
118 |
+
# Load the trained model
|
119 |
+
try:
|
120 |
+
model_path = './model1_kera.h5' # Replace with the actual path to your model file
|
121 |
+
logging.info(f"Loading model from: {model_path}")
|
122 |
+
model = tf.keras.models.load_model(model_path)
|
123 |
+
logging.info("Model loaded successfully.")
|
124 |
+
except Exception as e:
|
125 |
+
logging.error(f"Error loading model: {e}")
|
126 |
|
127 |
+
# Define the class names
|
128 |
+
classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
|
|
|
|
|
129 |
|
130 |
+
# Function to preprocess the uploaded image and make predictions
|
131 |
+
def predict(img):
|
132 |
+
global model
|
133 |
+
try:
|
134 |
+
logging.debug("Received image for prediction.")
|
135 |
+
|
136 |
+
# Resize and preprocess the image
|
137 |
+
img = img.resize((224, 224))
|
138 |
+
img_array = keras_image.img_to_array(img)
|
139 |
+
img_array = tf.expand_dims(img_array, 0) # Add batch dimension
|
140 |
+
|
141 |
+
logging.debug("Image preprocessed successfully.")
|
142 |
+
|
143 |
+
# Ensure the model is loaded
|
144 |
+
if model is None:
|
145 |
+
raise ValueError("Model is not loaded properly.")
|
146 |
+
|
147 |
+
# Make predictions
|
148 |
+
predictions = model.predict(img_array)
|
149 |
+
score = tf.nn.softmax(predictions[0])
|
150 |
+
predicted_class_index = tf.argmax(score).numpy()
|
151 |
+
predicted_class = classes[predicted_class_index]
|
152 |
+
|
153 |
+
logging.debug(f"Prediction successful: {predicted_class}")
|
154 |
+
|
155 |
+
# Return the predicted class and the raw predictions
|
156 |
+
return predicted_class, predictions[0].tolist()
|
157 |
except Exception as e:
|
158 |
+
logging.error(f"Error during prediction: {e}")
|
159 |
+
# Print the error message in the output
|
160 |
+
return str(e), str(e)
|
161 |
|
162 |
+
# Create a Gradio interface
|
163 |
iface = gr.Interface(
|
164 |
+
fn=predict,
|
165 |
+
inputs=gr.Image(type='pil'),
|
166 |
+
outputs=[
|
167 |
+
gr.Textbox(label="Prediction"),
|
168 |
+
gr.Label(label="Raw Predictions")
|
169 |
+
],
|
170 |
+
title="Lung and Colon Cancer Detection",
|
171 |
+
description="Upload an image of histopathological tissue to detect if it is a type of lung or colon cancer."
|
172 |
)
|
173 |
|
174 |
+
# Launch the interface
|
175 |
+
iface.launch()
|