-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (44 loc) · 1.7 KB
/
app.py
File metadata and controls
50 lines (44 loc) · 1.7 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
if file:
image = _read_image(Image.open(file))
model_path = "models/best_model_C-1_gamma-0.001.joblib"
model_path = "./model/best_model_C-1_gamma-10.joblib"
model = joblib.load(model_path)
prediction = model.predict(image)
return jsonify({"prediction": str(prediction[0])})
else:
return jsonify({"error": "Invalid file format"})
@app.route("/prediction", methods=["POST"])
def prediction():
@app.route("/prediction/<model_type>", methods=["POST"])
def prediction(model_type):
if model_type not in ["svm", "tree", "lr"]:
return jsonify({"error": "Invalid model type"})
else:
model = load_model(model_type)
data_json = request.json
if data_json:
data_dict = json.loads(data_json)
image = np.array([data_dict["image"]])
model_path = "models/best_model_C-1_gamma-0.001.joblib"
model = joblib.load(model_path)
# model_path = "models/best_model_C-1_gamma-0.001.joblib"
# model = joblib.load(model_path)
try:
prediction = model.predict(image)
return jsonify({"prediction": str(prediction[0])})
@@ -123,7 +127,18 @@ def prediction():
return jsonify({"error": "Invalid data format"})
def load_model(model_type="svm"):
if model_type == "svm":
model_path = "./models/best_model_C-1_gamma-10.joblib"
elif model_type == "tree":
model_path = "./models/best_model_max_depth-15.joblib"
elif model_type == "lr":
model_path = "./models/best_model_solver-lbfgs.joblib"
model = joblib.load(model_path)
return model
if __name__ == "__main__":
print("server is running")
#check
# check
app.run(host="0.0.0.0", port=8000)