-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
175 lines (138 loc) · 5.71 KB
/
app.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from flask import Flask, request, jsonify
from flask_cors import CORS
import pandas as pd
import pickle
from sklearn.preprocessing import LabelEncoder
import shutil
app = Flask(__name__)
CORS(app)
# Load the pre-trained models
pickle_file_path_model1 = 'model/best_random_forest_model.pkl'
pickle_file_path_model2 = 'model/10M.pkl'
pickle_file_path_model3 = 'model/TugRight.pkl'
pickle_file_path_model4 = 'model/10Right.pkl'
with open(pickle_file_path_model1, 'rb') as file1:
model1 = pickle.load(file1)
with open(pickle_file_path_model2, 'rb') as file2:
model2 = pickle.load(file2)
with open(pickle_file_path_model3, 'rb') as file3:
model3 = pickle.load(file3)
with open(pickle_file_path_model4, 'rb') as file4:
model4 = pickle.load(file4)
# LabelEncoder for Gender column
label_encoder = LabelEncoder()
# Function to update SCONE integration_accuracy
def update_integration_accuracy(file_path, new_accuracy, new_file_name):
try:
# Read the SCONE file content
with open(file_path, 'r') as file:
content = file.readlines()
# Update the 'integration_accuracy' parameter
updated_content = []
for line in content:
if 'integration_accuracy' in line:
line = f'integration_accuracy = {new_accuracy}\n'
updated_content.append(line)
# Construct new file path for saving the updated file
directory = r"C:\python\configs\Tutorials2"
new_file_path = f"{directory}\\{new_file_name}"
# Write the updated content to the new file
with open(new_file_path, 'w') as file:
file.writelines(updated_content)
print(f"Updated 'integration_accuracy' to {new_accuracy} and saved as {new_file_name} in {directory}")
except FileNotFoundError:
print(f"Error: File not found at {file_path}. Please check the path.")
except Exception as e:
print(f"An error occurred: {e}")
# SCONE configuration file path
scone_file_path = r"C:\python\configs\Tutorials2\Tutorial 4a - Gait - OpenSim.scone"
new_file_name = "changed-gate.scone" # Set the new file name
# Model 1 Prediction
@app.route('/predict_model1', methods=['POST'])
def predict_model1():
try:
data = request.json
print("Received data for model 1:", data)
model1_data = data.get("model1_inputs")
if model1_data:
model1_df = pd.DataFrame([model1_data])
model1_df.rename(columns={"DaysOfTreatment": "Days Of Treatment"}, inplace=True)
model1_df['Gender'] = label_encoder.fit_transform(model1_df['Gender'])
model1_prediction = model1.predict(model1_df)[0]
# Set integration_accuracy based on prediction value
if model1_prediction < 0:
integration_accuracy_value = 0.0013
else:
integration_accuracy_value = model1_prediction
# Update the integration_accuracy in the SCONE configuration file
update_integration_accuracy(scone_file_path, integration_accuracy_value, new_file_name)
else:
model1_prediction = None
return jsonify({
"result1": model1_prediction
})
except Exception as e:
print("Error:", e) # Log the error for debugging
return jsonify({"error": str(e)}), 400
# Model 2 Prediction
@app.route('/predict_model2', methods=['POST'])
def predict_model2():
try:
data = request.json
print("Received data for model 2:", data)
model2_data = data.get("model2_inputs")
if model2_data:
model2_df = pd.DataFrame([model2_data])
model2_df.rename(columns={"DaysOfTreatment": "Days Of Treatment"}, inplace=True)
model2_df['Gender'] = label_encoder.fit_transform(model2_df['Gender'])
model2_prediction = model2.predict(model2_df)[0]
else:
model2_prediction = None
return jsonify({
"result2": model2_prediction
})
except Exception as e:
print("Error:", e) # Log the error for debugging
return jsonify({"error": str(e)}), 400
# Model 3 Prediction
@app.route('/predict_model3', methods=['POST'])
def predict_model3():
try:
data = request.json
print("Received data for model 3:", data)
model3_data = data.get("model3_inputs")
if model3_data:
model3_df = pd.DataFrame([model3_data])
model3_df.rename(columns={"DaysOfTreatment": "Days Of Treatment"}, inplace=True)
model3_df['Gender'] = label_encoder.fit_transform(model3_df['Gender'])
model3_prediction = model3.predict(model3_df)[0]
else:
model3_prediction = None
return jsonify({
"result3": model3_prediction
})
except Exception as e:
print("Error:", e) # Log the error for debugging
return jsonify({"error": str(e)}), 400
# Model 4 Prediction
@app.route('/predict_model4', methods=['POST'])
def predict_model4():
try:
data = request.json
print("Received data for model 4:", data)
model4_data = data.get("model4_inputs")
if model4_data:
model4_df = pd.DataFrame([model4_data])
model4_df.rename(columns={"DaysOfTreatment": "Days Of Treatment"}, inplace=True)
model4_df['Gender'] = label_encoder.fit_transform(model4_df['Gender'])
model4_prediction = model4.predict(model4_df)[0]
else:
model4_prediction = None
return jsonify({
"result4": model4_prediction
})
except Exception as e:
print("Error:", e) # Log the error for debugging
return jsonify({"error": str(e)}), 400
if __name__ == '__main__':
app.run(debug=True)