kkhushisaid commited on
Commit
16a92ad
Β·
verified Β·
1 Parent(s): f68f26d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -38
app.py CHANGED
@@ -1,50 +1,41 @@
1
  import gradio as gr
 
 
2
  import numpy as np
3
- from tensorflow.keras.models import load_model
4
- from PIL import Image
5
- import traceback
6
 
7
- # Load the trained model
8
- try:
9
- model = load_model("pneumonia_cnn_model.h5", compile=False)
10
- print("βœ… Model loaded successfully!")
11
- except Exception as e:
12
- print("❌ Failed to load model:", e)
13
- raise
14
-
15
- # Preprocessing function for uploaded X-ray images
16
- def preprocess_image(image):
17
- image = image.convert("L") # Convert to grayscale
18
- image = image.resize((150, 150)) # Resize to model's expected input
19
- image_array = np.array(image) / 255.0 # Normalize pixel values
20
- image_array = np.expand_dims(image_array, axis=-1) # Add channel dimension
21
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
22
- print("πŸ“· Preprocessed image shape:", image_array.shape)
23
- return image_array
24
 
25
  # Prediction function
26
  def predict(image):
27
  try:
28
- image_array = preprocess_image(image)
29
- prediction = model.predict(image_array)[0][0]
30
- label = "🦠 Pneumonia" if prediction > 0.5 else "βœ… Normal"
31
- confidence = prediction if prediction > 0.5 else 1 - prediction
32
- return f"{label} ({confidence * 100:.2f}% confidence)"
 
 
 
 
 
 
 
 
 
 
 
 
33
  except Exception as e:
34
- traceback_str = traceback.format_exc()
35
- print("❌ Prediction error:\n", traceback_str)
36
  return f"Error during prediction: {str(e)}"
37
 
38
- # Gradio interface
39
- interface = gr.Interface(
40
- fn=predict,
41
- inputs=gr.Image(type="pil", label="Upload Chest X-ray"),
42
- outputs=gr.Textbox(label="Prediction"),
43
- title="🫁 Pneumonia Detection from Chest X-rays",
44
- description="Upload a chest X-ray image to detect if the person has pneumonia using a deep learning model.",
45
- theme="default",
46
- allow_flagging="never"
47
  )
48
 
49
- # Launch interface (for Hugging Face Spaces)
50
- interface.launch(debug=True)
 
1
  import gradio as gr
2
+ import tensorflow as tf
3
+ from PIL import Image, ImageOps
4
  import numpy as np
 
 
 
5
 
6
+ # Load the model
7
+ model = tf.keras.models.load_model("pneumonia_cnn_model.h5")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Prediction function
10
  def predict(image):
11
  try:
12
+ # Convert to grayscale
13
+ img = ImageOps.grayscale(image)
14
+
15
+ # Resize image to match the model's expected input size (280x280)
16
+ img = img.resize((280, 280)) # Adjusted the size to 280x280
17
+
18
+ # Convert to numpy array and normalize the pixel values
19
+ img_array = np.array(img).reshape(1, 280, 280, 1) / 255.0
20
+
21
+ # Make prediction
22
+ prediction = model.predict(img_array)
23
+
24
+ # Interpret prediction
25
+ if prediction >= 0.5:
26
+ return "Pneumonia detected"
27
+ else:
28
+ return "No pneumonia detected"
29
  except Exception as e:
 
 
30
  return f"Error during prediction: {str(e)}"
31
 
32
+ # Create the Gradio interface
33
+ iface = gr.Interface(
34
+ fn=predict, # Function to call on image input
35
+ inputs=gr.Image(type="pil", label="Upload Chest X-ray Image"),
36
+ outputs="text", # Output is the prediction result (text)
37
+ live=True # Optional: set to False if you don't want to update results live
 
 
 
38
  )
39
 
40
+ # Launch the app
41
+ iface.launch()