kkhushisaid commited on
Commit
a7548e4
Β·
verified Β·
1 Parent(s): f643d7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -1,11 +1,16 @@
1
  import gradio as gr
2
  import numpy as np
3
- from tensorflow.keras.models import load_model # βœ… switched to tf.keras
4
  from PIL import Image
 
5
 
6
-
7
- # Load the trained model (no custom_objects needed if using built-in metrics only)
8
- model = load_model("pneumonia_cnn_model.h5")
 
 
 
 
9
 
10
  # Preprocessing function for uploaded X-ray images
11
  def preprocess_image(image):
@@ -13,7 +18,8 @@ def preprocess_image(image):
13
  image = image.resize((150, 150)) # Resize to model's expected input
14
  image_array = np.array(image) / 255.0 # Normalize pixel values
15
  image_array = np.expand_dims(image_array, axis=-1) # Add channel dimension
16
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
 
17
  return image_array
18
 
19
  # Prediction function
@@ -21,11 +27,13 @@ def predict(image):
21
  try:
22
  image_array = preprocess_image(image)
23
  prediction = model.predict(image_array)[0][0]
24
- label = "Pneumonia" if prediction > 0.5 else "Normal"
25
  confidence = prediction if prediction > 0.5 else 1 - prediction
26
  return f"{label} ({confidence * 100:.2f}% confidence)"
27
  except Exception as e:
28
- return f"Error: {str(e)}"
 
 
29
 
30
  # Gradio interface
31
  interface = gr.Interface(
@@ -38,5 +46,5 @@ interface = gr.Interface(
38
  allow_flagging="never"
39
  )
40
 
41
- # Important: Always call launch() outside of __main__ block on Hugging Face Spaces
42
  interface.launch(debug=True)
 
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):
 
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
 
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(
 
46
  allow_flagging="never"
47
  )
48
 
49
+ # Launch interface (for Hugging Face Spaces)
50
  interface.launch(debug=True)