psinha823 commited on
Commit
78d84ec
·
verified ·
1 Parent(s): 97d56f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -163
app.py CHANGED
@@ -1,186 +1,34 @@
1
- # import gradio as gr
2
- # import tensorflow as tf
3
- # from tensorflow.keras.preprocessing import image
4
- # import numpy as np
5
-
6
- # # Load the trained model
7
- # model = tf.keras.models.load_model('./model12_acc99_kera.h5')
8
-
9
- # # Define the class names
10
- # classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
11
-
12
- # # Function to preprocess the uploaded image and make predictions
13
- # def predict(img):
14
- # img = img.resize((224, 224))
15
- # img_array = image.img_to_array(img)
16
- # img_array = np.expand_dims(img_array, axis=0)
17
- # img_array = img_array / 255.0 # Normalize the image
18
-
19
- # predictions = model.predict(img_array)
20
- # predicted_class = classes[np.argmax(predictions[0])]
21
-
22
- # return predicted_class
23
-
24
- # # Create a Gradio interface
25
- # iface = gr.Interface(
26
- # fn=predict,
27
- # inputs=gr.Image(type='pil'),
28
- # outputs=gr.Textbox(label="Prediction"),
29
- # title="Lung and Colon Cancer Detection",
30
- # description="Upload an image of histopathological tissue to detect if it is a type of lung or colon cancer."
31
- # )
32
-
33
- # # Launch the interface
34
- # iface.launch()
35
-
36
  import gradio as gr
37
  import tensorflow as tf
38
  from tensorflow.keras.preprocessing import image
39
  import numpy as np
40
- import logging
41
-
42
- # Set up logging
43
- logging.basicConfig(level=logging.DEBUG)
44
-
45
- # Initialize the model variable
46
- model = None
47
 
48
  # Load the trained model
49
- try:
50
- model_path = './Model1_kera.h5'
51
- logging.info(f"Loading model from: {model_path}")
52
- model = tf.keras.models.load_model(model_path)
53
- logging.info("Model loaded successfully.")
54
- except Exception as e:
55
- logging.error(f"Error loading model: {e}")
56
 
57
  # Define the class names
58
  classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
59
 
60
  # Function to preprocess the uploaded image and make predictions
61
  def predict(img):
62
- global model
63
- try:
64
- logging.debug("Received image for prediction.")
65
-
66
- # Resize and preprocess the image
67
- img = img.resize((224, 224))
68
- img_array = image.img_to_array(img)
69
- img_array = np.expand_dims(img_array, axis=0)
70
-
71
- logging.debug("Image preprocessed successfully.")
72
-
73
- # Ensure the model is loaded
74
- if model is None:
75
- raise ValueError("Model is not loaded properly.")
76
-
77
- # Make predictions
78
- predictions = model.predict(img_array)
79
- predicted_class_index = np.argmax(predictions[0])
80
- predicted_class = classes[predicted_class_index]
81
-
82
- logging.debug(f"Prediction successful: {predicted_class}")
83
-
84
- # Return the predicted class and the raw predictions
85
- return predicted_class, predictions[0].tolist()
86
- except Exception as e:
87
- logging.error(f"Error during prediction: {e}")
88
- # Print the error message in the output
89
- return str(e), str(e)
90
 
91
  # Create a Gradio interface
92
  iface = gr.Interface(
93
  fn=predict,
94
  inputs=gr.Image(type='pil'),
95
- outputs=[
96
- gr.Textbox(label="Prediction"),
97
- gr.Label(label="Raw Predictions")
98
- ],
99
  title="Lung and Colon Cancer Detection",
100
  description="Upload an image of histopathological tissue to detect if it is a type of lung or colon cancer."
101
  )
102
 
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
- # import os
112
-
113
- # # Set up logging
114
- # logging.basicConfig(level=logging.DEBUG)
115
-
116
- # # Initialize the model variable
117
- # model = None
118
-
119
- # # Function to load the model
120
- # def load_model():
121
- # global model
122
- # model_path = r'Model1_kera.h5' # Replace with the actual path to your model file
123
- # if not os.path.exists(model_path):
124
- # logging.error(f"Model file does not exist at path: {model_path}")
125
- # return False
126
- # try:
127
- # logging.info(f"Loading model from: {model_path}")
128
- # model = tf.keras.models.load_model(model_path)
129
- # logging.info("Model loaded successfully.")
130
- # return True
131
- # except Exception as e:
132
- # logging.error(f"Error loading model: {e}")
133
- # return False
134
-
135
- # # Load the model when the script starts
136
- # model_loaded = load_model()
137
-
138
- # # Define the class names
139
- # classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
140
-
141
- # # Function to preprocess the uploaded image and make predictions
142
- # def predict(img):
143
- # global model
144
- # try:
145
- # logging.debug("Received image for prediction.")
146
-
147
- # # Resize and preprocess the image
148
- # img = img.resize((224, 224))
149
- # img_array = keras_image.img_to_array(img)
150
- # img_array = tf.expand_dims(img_array, 0) # Add batch dimension
151
-
152
- # logging.debug("Image preprocessed successfully.")
153
-
154
- # # Ensure the model is loaded
155
- # if model is None:
156
- # raise ValueError("Model is not loaded properly.")
157
-
158
- # # Make predictions
159
- # predictions = model.predict(img_array)
160
- # score = tf.nn.softmax(predictions[0])
161
- # predicted_class_index = tf.argmax(score).numpy()
162
- # predicted_class = classes[predicted_class_index]
163
-
164
- # logging.debug(f"Prediction successful: {predicted_class}")
165
-
166
- # # Return the predicted class and the raw predictions
167
- # return predicted_class, predictions[0].tolist()
168
- # except Exception as e:
169
- # logging.error(f"Error during prediction: {e}")
170
- # # Print the error message in the output
171
- # return str(e), str(e)
172
-
173
- # # Create a Gradio interface
174
- # iface = gr.Interface(
175
- # fn=predict,
176
- # inputs=gr.Image(type='pil'),
177
- # outputs=[
178
- # gr.Textbox(label="Prediction"),
179
- # gr.Label(label="Raw Predictions")
180
- # ],
181
- # title="Lung and Colon Cancer Detection",
182
- # description="Upload an image of histopathological tissue to detect if it is a type of lung or colon cancer."
183
- # )
184
-
185
- # # Launch the interface
186
- # iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  from tensorflow.keras.preprocessing import image
4
  import numpy as np
 
 
 
 
 
 
 
5
 
6
  # Load the trained model
7
+ model = tf.keras.models.load_model('Model1_kera.h5')
 
 
 
 
 
 
8
 
9
  # Define the class names
10
  classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
11
 
12
  # Function to preprocess the uploaded image and make predictions
13
  def predict(img):
14
+ img = img.resize((224, 224))
15
+ img_array = image.img_to_array(img)
16
+ img_array = np.expand_dims(img_array, axis=0)
17
+ img_array = img_array / 255.0 # Normalize the image
18
+
19
+ predictions = model.predict(img_array)
20
+ predicted_class = classes[np.argmax(predictions[0])]
21
+
22
+ return predicted_class
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  # Create a Gradio interface
25
  iface = gr.Interface(
26
  fn=predict,
27
  inputs=gr.Image(type='pil'),
28
+ outputs=gr.Textbox(label="Prediction"),
 
 
 
29
  title="Lung and Colon Cancer Detection",
30
  description="Upload an image of histopathological tissue to detect if it is a type of lung or colon cancer."
31
  )
32
 
33
  # Launch the interface
34
  iface.launch()