-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (45 loc) · 1.58 KB
/
app.py
File metadata and controls
55 lines (45 loc) · 1.58 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
51
52
53
54
55
import pickle
from flask import Flask, request, jsonify
from helper import file_processing
app = Flask(__name__)
with open("./model/anemia.pkl", "rb") as f:
lod_pickle = pickle.load(f)
@app.route("/")
def hello_world():
return "Hello, World!"
@app.route("/predict",methods=["POST"])
def predict():
json = request.get_json()
gender = json["Gender"]
hemoglobin = json["Hemoglobin"]
mch = json["MCH"]
mchc = json["MCHC"]
mcv = json["MCV"]
to_predict = [gender, hemoglobin, mch, mchc, mcv]
to_predict = [to_predict]
prediction = lod_pickle.predict(to_predict)
return jsonify(prediction=prediction.tolist()[0])
@app.route("/predict_file", methods=["POST"])
def predict_file():
file = request.files["file"]
file_extension = file.filename.split(".")[-1]
file_name = file.filename.split(".")[0]
file.save(f"{file_name}.{file_extension}")
print(f"{file_name}.{file_extension}")
prediction = file_processing(f"{file_name}.{file_extension}")
if isinstance(prediction, str):
return jsonify(prediction=prediction)
req_data = {
"gender": 1 if prediction['Gender'] == 'Male' else 0,
"Hemoglobin": prediction["Hemoglobin"],
"MCH": prediction["MCH"],
"MCHC": prediction["MCHC"],
"MCV": prediction["MCV"],
}
to_predict = list(req_data.values())
to_predict = [float(i) for i in to_predict]
to_predict = [to_predict]
prediction = lod_pickle.predict(to_predict)
return jsonify(prediction=prediction.tolist()[0], data=req_data)
if __name__ == "__main__":
app.run(debug=True)