Spaces:
Sleeping
Sleeping
Initial Commit
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ import tensorflow as tf
|
|
5 |
import mlxtend
|
6 |
import joblib
|
7 |
import cv2
|
|
|
|
|
|
|
8 |
from sklearn.preprocessing import LabelEncoder
|
9 |
from tensorflow.keras.models import save_model, load_model
|
10 |
from PIL import Image
|
@@ -58,6 +61,35 @@ ensemble_models = {
|
|
58 |
class_labels = ['COVID-19', 'Normal', 'Viral Pneumonia']
|
59 |
class_index_mapping = {i: label for i, label in enumerate(class_labels)}
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Apply the same rescaling as in the model
|
62 |
def rescale_images(img):
|
63 |
return img / 127.5 - 1
|
@@ -82,7 +114,7 @@ def predict_covid(*args):
|
|
82 |
image = args[-1]
|
83 |
|
84 |
if not any(symptoms) and image is None:
|
85 |
-
return "COVID-19 Negative", "100%", "COVID-19 Negative", "100%", "No inputs defined"
|
86 |
|
87 |
# Prepare the input data for the text-based model
|
88 |
label_encoder = LabelEncoder()
|
@@ -135,6 +167,9 @@ def predict_covid(*args):
|
|
135 |
|
136 |
text_prob_positive = np.average(probabilities, weights=weights) * 100
|
137 |
|
|
|
|
|
|
|
138 |
# Prepare the input data for the image-based model
|
139 |
image_prediction = None
|
140 |
confidence = None
|
@@ -148,7 +183,9 @@ def predict_covid(*args):
|
|
148 |
"NA",
|
149 |
"Invalid X-Ray Scan",
|
150 |
"NA",
|
151 |
-
"Your X-Ray scan does not meet the required standards. Please ensure that your scans are not blurry, pixelated, or disfigured"
|
|
|
|
|
152 |
)
|
153 |
|
154 |
image = preprocess_image(image)
|
@@ -168,6 +205,9 @@ def predict_covid(*args):
|
|
168 |
# Calculate the confidence level of the prediction
|
169 |
confidence = np.max(avg_ensemble_prob) * 100
|
170 |
|
|
|
|
|
|
|
171 |
# Provide reasoning for the prediction
|
172 |
if ensemble_prediction == 0 and text_prediction == "COVID-19 Positive":
|
173 |
reason = "Your X-Ray scan and symptoms provide conclusive indications of COVID-19"
|
@@ -195,7 +235,7 @@ def predict_covid(*args):
|
|
195 |
else:
|
196 |
reason = "Your symptoms provide conclusive indications of your diagnosis"
|
197 |
|
198 |
-
return text_prediction,
|
199 |
|
200 |
# Create the input and output components
|
201 |
symptom_components = [gr_components.Checkbox(label=label) for label in column_names[:-1]]
|
@@ -206,6 +246,8 @@ output_components = [
|
|
206 |
gr_components.Label(label="Prediction based on X-Ray Scan"),
|
207 |
gr_components.Label(label="X-Ray Scan Confidence (%)"),
|
208 |
gr_components.Textbox(label="Final Prediction"),
|
|
|
|
|
209 |
]
|
210 |
|
211 |
# Create the interface and launch
|
|
|
5 |
import mlxtend
|
6 |
import joblib
|
7 |
import cv2
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
import seaborn as sns
|
10 |
+
import tempfile
|
11 |
from sklearn.preprocessing import LabelEncoder
|
12 |
from tensorflow.keras.models import save_model, load_model
|
13 |
from PIL import Image
|
|
|
61 |
class_labels = ['COVID-19', 'Normal', 'Viral Pneumonia']
|
62 |
class_index_mapping = {i: label for i, label in enumerate(class_labels)}
|
63 |
|
64 |
+
# Create a bar chart to represent symptom weightage
|
65 |
+
def show_symptom_weightage(symptoms, weights):
|
66 |
+
plt.figure(figsize=(10, 6))
|
67 |
+
plt.bar(symptoms, weights)
|
68 |
+
plt.xlabel("Symptom")
|
69 |
+
plt.ylabel("Weight")
|
70 |
+
plt.title("Weightage/Importance of Symptoms")
|
71 |
+
plt.xticks(rotation=45, ha='right')
|
72 |
+
plt.tight_layout()
|
73 |
+
# Save the plot as an image and return the file path
|
74 |
+
file_path = tempfile.NamedTemporaryFile(suffix=".png").name
|
75 |
+
plt.savefig(file_path)
|
76 |
+
plt.close()
|
77 |
+
return file_path
|
78 |
+
|
79 |
+
# Create a heat map to represent image-based model predictions
|
80 |
+
def show_heat_map(image, prediction_probabilities):
|
81 |
+
plt.figure(figsize=(8, 6))
|
82 |
+
sns.heatmap(prediction_probabilities, annot=True, cmap="YlGnBu", xticklabels=class_labels, yticklabels=False)
|
83 |
+
plt.title("Image-based Model Prediction Heatmap")
|
84 |
+
plt.xlabel("Prediction")
|
85 |
+
plt.ylabel("Model")
|
86 |
+
plt.tight_layout()
|
87 |
+
# Save the plot as an image and return the file path
|
88 |
+
file_path = tempfile.NamedTemporaryFile(suffix=".png").name
|
89 |
+
plt.savefig(file_path)
|
90 |
+
plt.close()
|
91 |
+
return file_path
|
92 |
+
|
93 |
# Apply the same rescaling as in the model
|
94 |
def rescale_images(img):
|
95 |
return img / 127.5 - 1
|
|
|
114 |
image = args[-1]
|
115 |
|
116 |
if not any(symptoms) and image is None:
|
117 |
+
return "COVID-19 Negative", "100%", "COVID-19 Negative", "100%", "No inputs defined", None, None
|
118 |
|
119 |
# Prepare the input data for the text-based model
|
120 |
label_encoder = LabelEncoder()
|
|
|
167 |
|
168 |
text_prob_positive = np.average(probabilities, weights=weights) * 100
|
169 |
|
170 |
+
# Visualize the symptom weightage/importance
|
171 |
+
symptom_weightage_img = show_symptom_weightage(column_names[:-1], weights)
|
172 |
+
|
173 |
# Prepare the input data for the image-based model
|
174 |
image_prediction = None
|
175 |
confidence = None
|
|
|
183 |
"NA",
|
184 |
"Invalid X-Ray Scan",
|
185 |
"NA",
|
186 |
+
"Your X-Ray scan does not meet the required standards. Please ensure that your scans are not blurry, pixelated, or disfigured",
|
187 |
+
None,
|
188 |
+
None
|
189 |
)
|
190 |
|
191 |
image = preprocess_image(image)
|
|
|
205 |
# Calculate the confidence level of the prediction
|
206 |
confidence = np.max(avg_ensemble_prob) * 100
|
207 |
|
208 |
+
# Visualize the heatmap
|
209 |
+
heatmap_img = show_heat_map(image, [prediction for prediction, _ in ensemble_predictions])
|
210 |
+
|
211 |
# Provide reasoning for the prediction
|
212 |
if ensemble_prediction == 0 and text_prediction == "COVID-19 Positive":
|
213 |
reason = "Your X-Ray scan and symptoms provide conclusive indications of COVID-19"
|
|
|
235 |
else:
|
236 |
reason = "Your symptoms provide conclusive indications of your diagnosis"
|
237 |
|
238 |
+
return text_prediction, f"{text_prob_positive:.2f}%", image_prediction, f"{confidence:.2f}%", reason, heatmap_img, symptom_weightage_img
|
239 |
|
240 |
# Create the input and output components
|
241 |
symptom_components = [gr_components.Checkbox(label=label) for label in column_names[:-1]]
|
|
|
246 |
gr_components.Label(label="Prediction based on X-Ray Scan"),
|
247 |
gr_components.Label(label="X-Ray Scan Confidence (%)"),
|
248 |
gr_components.Textbox(label="Final Prediction"),
|
249 |
+
gr_components.Image(label="X-Ray Prediction Heatmap"),
|
250 |
+
gr_components.Image(label="Weightage assigned to Symptoms")
|
251 |
]
|
252 |
|
253 |
# Create the interface and launch
|