File size: 6,144 Bytes
d652d7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e21dd3
 
 
 
 
a2b62f9
5e21dd3
 
380299e
5e21dd3
 
74d5c82
5e21dd3
 
 
 
 
 
 
 
b7791dd
5e21dd3
 
380299e
5e21dd3
 
 
 
 
a2b62f9
5e21dd3
 
 
 
17d5a74
5e21dd3
a2b62f9
5e21dd3
 
 
74d5c82
5e21dd3
 
 
 
17d5a74
5e21dd3
a2b62f9
5e21dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b0fbd1
 
 
 
b3ddd7a
5e21dd3
6b0fbd1
 
5e21dd3
6b0fbd1
 
5e21dd3
b3ddd7a
 
 
290da2e
b3ddd7a
 
 
 
 
 
 
 
 
 
 
 
 
 
5e21dd3
6b0fbd1
 
5e21dd3
6b0fbd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17d5a74
6b0fbd1
 
 
8ffc381
6b0fbd1
60e6d39
6b0fbd1
 
 
 
 
 
 
 
60e6d39
8ffc381
6b0fbd1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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()