Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,63 +1,70 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Force CPU mode for TensorFlow
|
| 3 |
-
|
| 4 |
-
from flask import Flask, request, jsonify
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import cv2
|
| 7 |
import pydicom
|
|
|
|
| 8 |
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
#
|
| 14 |
model = load_model("model_ft_tf4_2.keras")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
@
|
| 18 |
def home():
|
| 19 |
return "Welcome to the Pneumonia Prediction API!"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
@
|
| 23 |
def predict_pneumonia():
|
|
|
|
|
|
|
| 24 |
if 'file' not in request.files:
|
| 25 |
return jsonify({'error': 'No DICOM file uploaded'}), 400
|
| 26 |
|
| 27 |
dcm_file = request.files['file']
|
| 28 |
-
dcm_file.seek(0) # ensure pointer at
|
| 29 |
|
| 30 |
-
# ---
|
| 31 |
try:
|
| 32 |
-
dicom_data = pydicom.dcmread(dcm_file, force=True)
|
| 33 |
image = dicom_data.pixel_array.astype(np.float32)
|
| 34 |
except Exception as e:
|
| 35 |
return jsonify({"error": f"Invalid DICOM file: {str(e)}"}), 400
|
| 36 |
|
| 37 |
-
# ---
|
| 38 |
image = cv2.resize(image, (224, 224))
|
| 39 |
-
if len(image.shape) == 2: # grayscale → RGB
|
| 40 |
-
image = np.stack([image]*3, axis=-1)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
max_val = np.max(image)
|
| 43 |
if max_val > 0:
|
| 44 |
image = image / max_val
|
| 45 |
|
| 46 |
-
image = np.expand_dims(image, axis=0) #
|
| 47 |
|
| 48 |
-
# ---
|
| 49 |
try:
|
| 50 |
-
prob = float(model.predict(image)[0][0])
|
| 51 |
-
pred_class = int(prob >= 0.5)
|
| 52 |
except Exception as e:
|
| 53 |
return jsonify({"error": f"Model prediction failed: {str(e)}"}), 500
|
| 54 |
-
|
| 55 |
return jsonify({
|
| 56 |
"Predicted_Class": pred_class,
|
| 57 |
"Probability": prob,
|
| 58 |
"Message": "Prediction completed"
|
| 59 |
})
|
| 60 |
|
| 61 |
-
|
|
|
|
| 62 |
if __name__ == '__main__':
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
import cv2
|
| 4 |
import pydicom
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
from tensorflow.keras.models import load_model
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Force CPU mode for TensorFlow
|
| 10 |
|
| 11 |
+
# Initialize Flask app
|
| 12 |
+
Pneumonia_predictor_api = Flask("Pneumonia predictor")
|
| 13 |
|
| 14 |
+
# Load trained model (must match TF/Keras version used during training)
|
| 15 |
model = load_model("model_ft_tf4_2.keras")
|
| 16 |
|
| 17 |
+
|
| 18 |
+
@Pneumonia_predictor_api.get('/')
|
| 19 |
def home():
|
| 20 |
return "Welcome to the Pneumonia Prediction API!"
|
| 21 |
|
| 22 |
+
|
| 23 |
+
@Pneumonia_predictor_api.route('/v1/Pneumonia_prediction', methods=['POST'])
|
| 24 |
def predict_pneumonia():
|
| 25 |
+
|
| 26 |
+
# --------- STEP 1: CHECK FILE ---------
|
| 27 |
if 'file' not in request.files:
|
| 28 |
return jsonify({'error': 'No DICOM file uploaded'}), 400
|
| 29 |
|
| 30 |
dcm_file = request.files['file']
|
| 31 |
+
dcm_file.seek(0) # ensure pointer at beginning
|
| 32 |
|
| 33 |
+
# --------- STEP 2: READ DICOM SAFELY ---------
|
| 34 |
try:
|
| 35 |
+
dicom_data = pydicom.dcmread(dcm_file, force=True) # force=True fixes header issues
|
| 36 |
image = dicom_data.pixel_array.astype(np.float32)
|
| 37 |
except Exception as e:
|
| 38 |
return jsonify({"error": f"Invalid DICOM file: {str(e)}"}), 400
|
| 39 |
|
| 40 |
+
# --------- STEP 3: PREPROCESS IMAGE ---------
|
| 41 |
image = cv2.resize(image, (224, 224))
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Convert grayscale → RGB 3-channel
|
| 44 |
+
if len(image.shape) == 2:
|
| 45 |
+
image = np.stack([image] * 3, axis=-1)
|
| 46 |
+
|
| 47 |
+
# Normalize 0–1
|
| 48 |
max_val = np.max(image)
|
| 49 |
if max_val > 0:
|
| 50 |
image = image / max_val
|
| 51 |
|
| 52 |
+
image = np.expand_dims(image, axis=0) # (1,224,224,3)
|
| 53 |
|
| 54 |
+
# --------- STEP 4: PREDICT ---------
|
| 55 |
try:
|
| 56 |
+
prob = float(model.predict(image)[0][0]) # probability
|
| 57 |
+
pred_class = int(prob >= 0.5) # predicted class
|
| 58 |
except Exception as e:
|
| 59 |
return jsonify({"error": f"Model prediction failed: {str(e)}"}), 500
|
| 60 |
+
|
| 61 |
return jsonify({
|
| 62 |
"Predicted_Class": pred_class,
|
| 63 |
"Probability": prob,
|
| 64 |
"Message": "Prediction completed"
|
| 65 |
})
|
| 66 |
|
| 67 |
+
|
| 68 |
+
# Run Flask locally
|
| 69 |
if __name__ == '__main__':
|
| 70 |
+
Pneumonia_predictor_api.run(debug=True)
|