Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,21 +6,15 @@ from tensorflow.keras.models import load_model
|
|
| 6 |
pneumonia_detector_api = Flask(__name__)
|
| 7 |
|
| 8 |
IMG_SIZE = 224
|
| 9 |
-
model = None
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def get_model():
|
| 13 |
-
global model
|
| 14 |
-
if model is None:
|
| 15 |
-
print("Loading model...")
|
| 16 |
-
model = load_model("pneumonia_resnet_model.h5")
|
| 17 |
-
print("Model loaded successfully")
|
| 18 |
-
return model
|
| 19 |
|
| 20 |
-
|
| 21 |
-
@pneumonia_detector_api.get("/")
|
| 22 |
def home():
|
| 23 |
-
return "
|
| 24 |
|
| 25 |
|
| 26 |
def preprocess_image(img_array):
|
|
@@ -36,7 +30,7 @@ def preprocess_image(img_array):
|
|
| 36 |
return img
|
| 37 |
|
| 38 |
|
| 39 |
-
@pneumonia_detector_api.
|
| 40 |
def predict():
|
| 41 |
|
| 42 |
if "file" not in request.files:
|
|
@@ -44,27 +38,17 @@ def predict():
|
|
| 44 |
|
| 45 |
file = request.files["file"]
|
| 46 |
|
| 47 |
-
|
| 48 |
-
img_array = np.load(file)
|
| 49 |
-
|
| 50 |
-
processed_img = preprocess_image(img_array)
|
| 51 |
-
|
| 52 |
-
model = get_model()
|
| 53 |
-
|
| 54 |
-
prediction = model.predict(processed_img)[0][0]
|
| 55 |
-
|
| 56 |
-
probability = float(prediction)
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
"Predicted_Class": label,
|
| 62 |
-
"Probability_Pneumonia": round(probability, 4)
|
| 63 |
-
})
|
| 64 |
|
| 65 |
-
|
| 66 |
-
return jsonify({"error": str(e)}), 500
|
| 67 |
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
| 6 |
pneumonia_detector_api = Flask(__name__)
|
| 7 |
|
| 8 |
IMG_SIZE = 224
|
|
|
|
| 9 |
|
| 10 |
+
print("Loading model...")
|
| 11 |
+
model = load_model("pneumonia_resnet_model.h5")
|
| 12 |
+
print("Model loaded")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
@pneumonia_detector_api.route("/")
|
|
|
|
| 16 |
def home():
|
| 17 |
+
return "Pneumonia Detection API Running"
|
| 18 |
|
| 19 |
|
| 20 |
def preprocess_image(img_array):
|
|
|
|
| 30 |
return img
|
| 31 |
|
| 32 |
|
| 33 |
+
@pneumonia_detector_api.route("/v1/predict", methods=["POST"])
|
| 34 |
def predict():
|
| 35 |
|
| 36 |
if "file" not in request.files:
|
|
|
|
| 38 |
|
| 39 |
file = request.files["file"]
|
| 40 |
|
| 41 |
+
img_array = np.load(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
processed_img = preprocess_image(img_array)
|
| 44 |
|
| 45 |
+
prediction = model.predict(processed_img)[0][0]
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
probability = float(prediction)
|
|
|
|
| 48 |
|
| 49 |
+
label = "Pneumonia" if probability > 0.5 else "Normal"
|
| 50 |
|
| 51 |
+
return jsonify({
|
| 52 |
+
"Predicted_Class": label,
|
| 53 |
+
"Probability_Pneumonia": round(probability, 4)
|
| 54 |
+
})
|