Commit e12df198 authored by Bandara K.M.A.P.'s avatar Bandara K.M.A.P.

Upload function 4 model

parent bf57cadd
from flask import Flask, request, jsonify
import numpy as np
import joblib
app = Flask(__name__)
time_model_card = joblib.load('time_model_card.pkl')
score_model_card = joblib.load('score_model_card.pkl')
# Endpoint to predict score and time for the next 3 months
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
score_data = data['score_data']
time_data = data['time_data']
# Convert lists to arrays
new_score_array = np.array([score_data])
new_time_array = np.array([time_data])
# Predict using the loaded models
new_time_predictions = time_model_card.predict(new_time_array)
new_score_predictions = score_model_card.predict(new_score_array)
# Prepare output
output = {
'predicted_score': new_score_predictions.tolist(),
'predicted_time': new_time_predictions.tolist()
}
return jsonify(output)
# Endpoint to predict score and time for the next 3 months with steps
@app.route('/predict_with_steps', methods=['POST'])
def predict_with_steps():
data = request.get_json()
score_data = data['score_data']
time_data = data['time_data']
num_steps = 3
# Initialize lists
x_new_score = []
x_new_time = []
x_new_2_score = score_data.copy()
x_new_2_time = time_data.copy()
score_predictions = []
time_predictions = []
for _ in range(num_steps):
if len(x_new_score) > 0:
x_new_2_score = x_new_score
x_new_2_time = x_new_time
# Predict score
y_pred_score = score_model_card.predict(np.array([x_new_2_score]))
score_predictions.append(y_pred_score.tolist())
# Update score data
temp_score = np.where(y_pred_score > x_new_2_score[-1], 2, np.where(y_pred_score == x_new_2_score[-1], 1, 0))
x_new_2_score = np.append(x_new_2_score[1:3], x_new_2_score[3:])
x_new_2_score[2] = temp_score
x_new_2_score = np.append(x_new_2_score, y_pred_score, axis=0)
# Predict time
y_pred_time = time_model_card.predict(np.array([x_new_2_time]))
time_predictions.append(y_pred_time.tolist())
# Update time data
temp_time = np.where(y_pred_time > x_new_2_time[-1], 2, np.where(y_pred_time == x_new_2_time[-1], 1, 0))
x_new_2_time = np.append(x_new_2_time[1:3], x_new_2_time[3:])
x_new_2_time[2] = temp_time
x_new_2_time = np.append(x_new_2_time, y_pred_time, axis=0)
# Prepare output
output = {
'score_predictions': score_predictions,
'time_predictions': time_predictions
}
return jsonify(output)
if __name__ == '__main__':
app.run(debug=True)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment