Update app.py
Browse files
app.py
CHANGED
|
@@ -3,32 +3,61 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# ---------------- LOAD
|
| 7 |
-
model = tf.keras.models.load_model("chest_xray_model.h5")
|
| 8 |
class_labels = ["Normal", "Pneumonia"]
|
| 9 |
|
| 10 |
# ---------------- PREDICTION FUNCTION ---------------- #
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# ---------------- GRADIO INTERFACE ---------------- #
|
| 24 |
interface = gr.Interface(
|
| 25 |
-
fn=
|
| 26 |
-
inputs=gr.
|
| 27 |
-
outputs=
|
| 28 |
title="Chest X-Ray Pneumonia Classifier",
|
| 29 |
-
description="Upload
|
| 30 |
)
|
| 31 |
|
|
|
|
| 32 |
if __name__ == "__main__":
|
| 33 |
interface.launch()
|
| 34 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# ---------------- LOAD TRAINED MODEL ---------------- #
|
| 7 |
+
model = tf.keras.models.load_model("chest_xray_model.h5") # Make sure this file is uploaded
|
| 8 |
class_labels = ["Normal", "Pneumonia"]
|
| 9 |
|
| 10 |
# ---------------- PREDICTION FUNCTION ---------------- #
|
| 11 |
+
def predict_xray(img):
|
| 12 |
+
# Preprocess
|
| 13 |
+
img = img.resize((224, 224))
|
| 14 |
+
img_array = np.array(img) / 255.0
|
| 15 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 16 |
+
|
| 17 |
+
# Model prediction
|
| 18 |
+
prediction = model.predict(img_array, verbose=0)[0][0]
|
| 19 |
+
label = class_labels[int(prediction > 0.5)]
|
| 20 |
+
confidence = prediction if prediction > 0.5 else 1 - prediction
|
| 21 |
+
|
| 22 |
+
# Detailed preliminary radiology report and first aid
|
| 23 |
+
if label == "Pneumonia":
|
| 24 |
+
report = (
|
| 25 |
+
"Preliminary Radiology Report:\n"
|
| 26 |
+
"- The chest X-ray shows opacities or infiltrates consistent with pneumonia.\n"
|
| 27 |
+
"- Findings suggest possible lung inflammation or infection.\n"
|
| 28 |
+
"- Further diagnostic tests (blood tests, sputum culture, oxygen saturation) recommended.\n\n"
|
| 29 |
+
"First Aid / Immediate Actions:\n"
|
| 30 |
+
"1. Seek medical attention immediately for confirmation and treatment.\n"
|
| 31 |
+
"2. Monitor for severe symptoms: high fever, shortness of breath, chest pain, confusion.\n"
|
| 32 |
+
"3. Ensure hydration and rest.\n"
|
| 33 |
+
"4. Avoid self-medicating with antibiotics without doctor supervision.\n"
|
| 34 |
+
"5. Use a mask and maintain good hygiene to prevent spread if contagious.\n"
|
| 35 |
+
"6. Keep a pulse oximeter if available; seek emergency care if oxygen saturation < 94%.\n"
|
| 36 |
+
"7. Note any worsening symptoms and report them to healthcare providers promptly.\n"
|
| 37 |
+
)
|
| 38 |
+
else:
|
| 39 |
+
report = (
|
| 40 |
+
"Preliminary Radiology Report:\n"
|
| 41 |
+
"- No visible signs of pneumonia detected on this X-ray.\n"
|
| 42 |
+
"- Lungs appear clear, but clinical correlation is advised.\n\n"
|
| 43 |
+
"General Advice:\n"
|
| 44 |
+
"1. Maintain healthy habits: good hydration, nutrition, and regular exercise.\n"
|
| 45 |
+
"2. Seek medical attention if respiratory symptoms develop.\n"
|
| 46 |
+
"3. Continue monitoring for cough, fever, or shortness of breath.\n"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
return f"Prediction: {label} ({confidence*100:.2f}% confidence)\n\n{report}"
|
| 50 |
|
| 51 |
# ---------------- GRADIO INTERFACE ---------------- #
|
| 52 |
interface = gr.Interface(
|
| 53 |
+
fn=predict_xray,
|
| 54 |
+
inputs=gr.Image(type="pil"),
|
| 55 |
+
outputs="text",
|
| 56 |
title="Chest X-Ray Pneumonia Classifier",
|
| 57 |
+
description="Upload a chest X-ray to get a detailed preliminary report and first-aid recommendations."
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# Launch the app
|
| 61 |
if __name__ == "__main__":
|
| 62 |
interface.launch()
|
| 63 |
|