psinha823 commited on
Commit
6b0fbd1
·
verified ·
1 Parent(s): 3f70f9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -31
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
- model_path = r'./model1_kera.h5'
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
- # Making predictions
123
- model_h5 = tf.keras.models.load_model(model_path)
124
- predictions = model_h5.predict(img_array)
 
 
 
 
 
125
 
126
- # Getting the class with the highest probability
127
- class_idx = np.argmax(predictions)
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
- return f"Prediction: {class_label}, Confidence: {confidence:.2f}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  except Exception as e:
133
- return str(e)
 
 
134
 
135
- # Creating a Gradio interface
136
  iface = gr.Interface(
137
- fn=classify_LC_image,
138
- inputs="image",
139
- outputs="text",
140
- title="Alzheimer's Disease Classification",
141
- description=" Upload an MRI Image for classification.",
142
- # flagging_options = ["Wrong Prediction"],
143
- # theme = 'darkhuggingface'
 
144
  )
145
 
146
- # Launching the Gradio interface
147
- iface.launch(inline = False)
 
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()