anabury commited on
Commit
ae94400
·
verified ·
1 Parent(s): 6977dfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -17
app.py CHANGED
@@ -3,32 +3,61 @@ import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
 
6
- # ---------------- LOAD PRE-TRAINED MODEL ---------------- #
7
- model = tf.keras.models.load_model("chest_xray_model.h5")
8
  class_labels = ["Normal", "Pneumonia"]
9
 
10
  # ---------------- PREDICTION FUNCTION ---------------- #
11
- def predict_xrays(images):
12
- results = []
13
- for img in images:
14
- img = img.resize((224, 224))
15
- img_array = np.array(img) / 255.0
16
- img_array = np.expand_dims(img_array, axis=0)
17
- prediction = model.predict(img_array, verbose=0)[0][0]
18
- label = class_labels[int(prediction > 0.5)]
19
- confidence = prediction if prediction > 0.5 else 1 - prediction
20
- results.append(f"Prediction: {label} ({confidence*100:.2f}% confidence)")
21
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # ---------------- GRADIO INTERFACE ---------------- #
24
  interface = gr.Interface(
25
- fn=predict_xrays,
26
- inputs=gr.Gallery(label="Upload X-ray Images", type="pil"), # removed .style()
27
- outputs=gr.Textbox(label="Predictions"),
28
  title="Chest X-Ray Pneumonia Classifier",
29
- description="Upload multiple X-ray images to get predictions at once."
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