[15a331]: / src / api / predict.py

Download this file

51 lines (42 with data), 1.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""API endpoints for stroke risk prediction."""
from flask import Blueprint, request, jsonify
from src.services.model_service import predict_stroke_risk, get_input_features
predict_bp = Blueprint("predict", __name__)
@predict_bp.route("/predict", methods=["POST"])
def predict():
"""Endpoint to predict stroke risk.
Returns:
A JSON object containing the prediction result and status.
"""
data = request.json
try:
prediction = predict_stroke_risk(data)
return (
jsonify(
{
"success": True,
"prediction": prediction,
"message": "Prediction successful",
}
),
200,
)
except Exception as e: # pylint: disable=broad-except
print(e)
return (
jsonify(
{
"success": False,
"error": str(e),
"message": "Error occurred during prediction",
}
),
400,
)
@predict_bp.route("/features", methods=["GET"])
def features():
"""Endpoint to get input features.
Returns:
A JSON array of input feature names.
"""
return jsonify(get_input_features())