Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 3 |
|
| 4 |
import numpy as np
|
|
@@ -9,49 +12,105 @@ from tensorflow.keras.models import load_model
|
|
| 9 |
pneumonia_detector_api = Flask(__name__)
|
| 10 |
|
| 11 |
IMG_SIZE = 224
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
model = load_model("pneumonia_resnet_model.keras", compile=False)
|
| 15 |
-
print("Model loaded")
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
@pneumonia_detector_api.route("/")
|
| 19 |
def home():
|
| 20 |
return "Pneumonia Detection API Running"
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
def preprocess_image(img_array):
|
| 24 |
|
| 25 |
-
img = tf.
|
|
|
|
|
|
|
|
|
|
| 26 |
img = img / 255.0
|
| 27 |
|
|
|
|
| 28 |
if img.shape[-1] == 1:
|
| 29 |
-
img =
|
| 30 |
|
| 31 |
img = np.expand_dims(img, axis=0)
|
| 32 |
|
| 33 |
return img
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
@pneumonia_detector_api.route("/v1/predict", methods=["POST"])
|
| 37 |
def predict():
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
probability = float(prediction)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
"
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
| 1 |
+
|
| 2 |
import os
|
| 3 |
+
|
| 4 |
+
# Force TensorFlow Keras compatibility
|
| 5 |
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 6 |
|
| 7 |
import numpy as np
|
|
|
|
| 12 |
pneumonia_detector_api = Flask(__name__)
|
| 13 |
|
| 14 |
IMG_SIZE = 224
|
| 15 |
+
MODEL_PATH = "pneumonia_resnet_model.keras"
|
| 16 |
+
|
| 17 |
+
print("TensorFlow Version:", tf.__version__)
|
| 18 |
+
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# SAFE MODEL LOADING
|
| 21 |
+
# -----------------------------
|
| 22 |
+
def load_trained_model():
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
print("Loading model...")
|
| 26 |
+
|
| 27 |
+
model = load_model(
|
| 28 |
+
MODEL_PATH,
|
| 29 |
+
compile=False,
|
| 30 |
+
safe_mode=False
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
print("Model loaded successfully")
|
| 34 |
+
return model
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print("Model loading failed:", str(e))
|
| 38 |
+
raise RuntimeError("Failed to load model")
|
| 39 |
|
| 40 |
+
model = load_trained_model()
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
+
# -----------------------------
|
| 44 |
+
# HOME ROUTE
|
| 45 |
+
# -----------------------------
|
| 46 |
@pneumonia_detector_api.route("/")
|
| 47 |
def home():
|
| 48 |
return "Pneumonia Detection API Running"
|
| 49 |
|
| 50 |
|
| 51 |
+
# -----------------------------
|
| 52 |
+
# IMAGE PREPROCESSING
|
| 53 |
+
# -----------------------------
|
| 54 |
def preprocess_image(img_array):
|
| 55 |
|
| 56 |
+
img = tf.convert_to_tensor(img_array, dtype=tf.float32)
|
| 57 |
+
|
| 58 |
+
img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE))
|
| 59 |
+
|
| 60 |
img = img / 255.0
|
| 61 |
|
| 62 |
+
# Convert grayscale to RGB
|
| 63 |
if img.shape[-1] == 1:
|
| 64 |
+
img = tf.repeat(img, repeats=3, axis=-1)
|
| 65 |
|
| 66 |
img = np.expand_dims(img, axis=0)
|
| 67 |
|
| 68 |
return img
|
| 69 |
|
| 70 |
|
| 71 |
+
# -----------------------------
|
| 72 |
+
# PREDICTION API
|
| 73 |
+
# -----------------------------
|
| 74 |
@pneumonia_detector_api.route("/v1/predict", methods=["POST"])
|
| 75 |
def predict():
|
| 76 |
|
| 77 |
+
try:
|
| 78 |
+
|
| 79 |
+
if "file" not in request.files:
|
| 80 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 81 |
+
|
| 82 |
+
file = request.files["file"]
|
| 83 |
+
|
| 84 |
+
# Expecting .npy file
|
| 85 |
+
img_array = np.load(file)
|
| 86 |
+
|
| 87 |
+
processed_img = preprocess_image(img_array)
|
| 88 |
+
|
| 89 |
+
prediction = model.predict(processed_img)
|
| 90 |
+
|
| 91 |
+
probability = float(prediction[0][0])
|
| 92 |
|
| 93 |
+
label = "Pneumonia" if probability > 0.5 else "Normal"
|
| 94 |
|
| 95 |
+
return jsonify({
|
| 96 |
+
"Predicted_Class": label,
|
| 97 |
+
"Probability_Pneumonia": round(probability, 4)
|
| 98 |
+
})
|
| 99 |
|
| 100 |
+
except Exception as e:
|
| 101 |
|
| 102 |
+
return jsonify({
|
| 103 |
+
"error": str(e)
|
| 104 |
+
}), 500
|
| 105 |
|
|
|
|
| 106 |
|
| 107 |
+
# -----------------------------
|
| 108 |
+
# RUN SERVER
|
| 109 |
+
# -----------------------------
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
|
| 112 |
+
pneumonia_detector_api.run(
|
| 113 |
+
host="0.0.0.0",
|
| 114 |
+
port=5000,
|
| 115 |
+
debug=True
|
| 116 |
+
)
|