viveksardey commited on
Commit
c90ae07
·
verified ·
1 Parent(s): 519c923

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -7,14 +7,13 @@ os.environ["TF_USE_LEGACY_KERAS"] = "1"
7
  import numpy as np
8
  import tensorflow as tf
9
  from flask import Flask, request, jsonify
10
- from tensorflow.keras.models import load_model
11
  from model import build_model
12
 
13
  pneumonia_detector_api = Flask(__name__)
14
 
15
  IMG_SIZE = 224
16
- #MODEL_PATH = "pneumonia_resnet_model.keras"
17
  MODEL_PATH = "pneumonia_resnet.weights.h5"
 
18
  print("TensorFlow Version:", tf.__version__)
19
 
20
  # -----------------------------
@@ -24,12 +23,14 @@ def load_trained_model():
24
 
25
  try:
26
 
27
- print("TensorFlow Version:", tf.__version__)
28
- print("Loading model...")
29
 
30
  model = build_model()
 
 
 
31
  model.load_weights(
32
- "pneumonia_resnet.weights.h5",
33
  skip_mismatch=True
34
  )
35
 
@@ -42,6 +43,7 @@ def load_trained_model():
42
  print("Model loading failed:", e)
43
  raise RuntimeError("Failed to load model")
44
 
 
45
  model = load_trained_model()
46
 
47
 
@@ -58,16 +60,23 @@ def home():
58
  # -----------------------------
59
  def preprocess_image(img_array):
60
 
61
- img = tf.convert_to_tensor(img_array, dtype=tf.float32)
62
 
63
- img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE))
 
 
64
 
65
- img = img / 255.0
 
66
 
67
- # Convert grayscale to RGB
 
 
 
68
  if img.shape[-1] == 1:
69
- img = tf.repeat(img, repeats=3, axis=-1)
70
 
 
71
  img = np.expand_dims(img, axis=0)
72
 
73
  return img
@@ -86,16 +95,18 @@ def predict():
86
 
87
  file = request.files["file"]
88
 
89
- # Expecting .npy file
90
  img_array = np.load(file)
91
 
92
  processed_img = preprocess_image(img_array)
93
 
 
 
94
  prediction = model.predict(processed_img)
95
 
96
  probability = float(prediction[0][0])
97
 
98
- label = "Pneumonia" if probability > 0.5 else "Normal"
99
 
100
  return jsonify({
101
  "Predicted_Class": label,
@@ -104,6 +115,8 @@ def predict():
104
 
105
  except Exception as e:
106
 
 
 
107
  return jsonify({
108
  "error": str(e)
109
  }), 500
@@ -117,5 +130,5 @@ if __name__ == "__main__":
117
  pneumonia_detector_api.run(
118
  host="0.0.0.0",
119
  port=7860,
120
- debug=True
121
  )
 
7
  import numpy as np
8
  import tensorflow as tf
9
  from flask import Flask, request, jsonify
 
10
  from model import build_model
11
 
12
  pneumonia_detector_api = Flask(__name__)
13
 
14
  IMG_SIZE = 224
 
15
  MODEL_PATH = "pneumonia_resnet.weights.h5"
16
+
17
  print("TensorFlow Version:", tf.__version__)
18
 
19
  # -----------------------------
 
23
 
24
  try:
25
 
26
+ print("Building model architecture...")
 
27
 
28
  model = build_model()
29
+
30
+ print("Loading weights...")
31
+
32
  model.load_weights(
33
+ MODEL_PATH,
34
  skip_mismatch=True
35
  )
36
 
 
43
  print("Model loading failed:", e)
44
  raise RuntimeError("Failed to load model")
45
 
46
+
47
  model = load_trained_model()
48
 
49
 
 
60
  # -----------------------------
61
  def preprocess_image(img_array):
62
 
63
+ img = np.array(img_array)
64
 
65
+ # Ensure image has channel dimension
66
+ if len(img.shape) == 2:
67
+ img = np.expand_dims(img, axis=-1)
68
 
69
+ # Resize image
70
+ img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE)).numpy()
71
 
72
+ # Normalize
73
+ img = img.astype("float32") / 255.0
74
+
75
+ # Convert grayscale → RGB
76
  if img.shape[-1] == 1:
77
+ img = np.repeat(img, 3, axis=-1)
78
 
79
+ # Add batch dimension
80
  img = np.expand_dims(img, axis=0)
81
 
82
  return img
 
95
 
96
  file = request.files["file"]
97
 
98
+ # Load .npy file
99
  img_array = np.load(file)
100
 
101
  processed_img = preprocess_image(img_array)
102
 
103
+ print("Input shape to model:", processed_img.shape)
104
+
105
  prediction = model.predict(processed_img)
106
 
107
  probability = float(prediction[0][0])
108
 
109
+ label = "Pneumonia" if probability >= 0.5 else "Normal"
110
 
111
  return jsonify({
112
  "Predicted_Class": label,
 
115
 
116
  except Exception as e:
117
 
118
+ print("Prediction error:", str(e))
119
+
120
  return jsonify({
121
  "error": str(e)
122
  }), 500
 
130
  pneumonia_detector_api.run(
131
  host="0.0.0.0",
132
  port=7860,
133
+ debug=False
134
  )