|
|
from fastapi import FastAPI |
|
|
from asgiref.wsgi import WsgiToAsgi |
|
|
from flask import Flask, render_template, request, jsonify |
|
|
import pickle |
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
from fastapi.middleware.wsgi import WSGIMiddleware |
|
|
|
|
|
flask_app = Flask(__name__) |
|
|
|
|
|
|
|
|
with open('model.pkl', 'rb') as model_file: |
|
|
linear_model = pickle.load(model_file) |
|
|
|
|
|
with open('scaler.pkl', 'rb') as scaler_file: |
|
|
scaler = pickle.load(scaler_file) |
|
|
|
|
|
|
|
|
def predict_7_days(sell_features, buy_features, linear_model, scaler): |
|
|
|
|
|
|
|
|
sell_1 = float(sell_features[6]) |
|
|
sell_2 = float(sell_features[5]) |
|
|
sell_3 = float(sell_features[4]) |
|
|
sell_4 = float(sell_features[3]) |
|
|
sell_5 = float(sell_features[2]) |
|
|
sell_6 = float(sell_features[1]) |
|
|
sell_7 = float(sell_features[0]) |
|
|
|
|
|
buy_1 = float(buy_features[6]) |
|
|
buy_2 = float(buy_features[5]) |
|
|
buy_3 = float(buy_features[4]) |
|
|
buy_4 = float(buy_features[3]) |
|
|
buy_5 = float(buy_features[2]) |
|
|
buy_6 = float(buy_features[1]) |
|
|
buy_7 = float(buy_features[0]) |
|
|
|
|
|
|
|
|
data = np.array([ |
|
|
[sell_1, buy_1], |
|
|
[sell_2, buy_2], |
|
|
[sell_3, buy_3], |
|
|
[sell_4, buy_4], |
|
|
[sell_5, buy_5], |
|
|
[sell_6, buy_6], |
|
|
[sell_7, buy_7] |
|
|
]) |
|
|
|
|
|
|
|
|
normalized_data = scaler.transform(data) |
|
|
|
|
|
|
|
|
|
|
|
last_row = pd.DataFrame( |
|
|
normalized_data.flatten().reshape(1, -1), |
|
|
columns=['sell-1', 'sell-2', 'sell-3', 'sell-4', 'sell-5', 'sell-6', 'sell-7', 'buy-1', 'buy-2', 'buy-3', 'buy-4', 'buy-5', 'buy-6', 'buy-7'] |
|
|
) |
|
|
|
|
|
predictions = [] |
|
|
|
|
|
|
|
|
for _ in range(7): |
|
|
|
|
|
predicted_value_normalized = linear_model.predict(last_row) |
|
|
predicted_value = scaler.inverse_transform(predicted_value_normalized.reshape(-1, 2)) |
|
|
|
|
|
|
|
|
predictions.append(predicted_value[0]) |
|
|
|
|
|
|
|
|
new_row_normalized = np.hstack([last_row.iloc[0, 2:], predicted_value_normalized[0]]) |
|
|
last_row = pd.DataFrame(new_row_normalized.reshape(1, -1), columns=last_row.columns) |
|
|
|
|
|
|
|
|
predictions_df = pd.DataFrame( |
|
|
predictions, |
|
|
columns=['sell', 'buy'], |
|
|
index=pd.date_range(start=pd.Timestamp.today() + pd.Timedelta(days=1), periods=7) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
last_price = [sell_features[7], buy_features[7]] |
|
|
|
|
|
|
|
|
|
|
|
predictions_df['sell_change'] = predictions_df['sell'].pct_change() * 100 |
|
|
predictions_df['buy_change'] = predictions_df['buy'].pct_change() * 100 |
|
|
|
|
|
|
|
|
predictions_df.loc[predictions_df.index[0], 'sell_change'] = ((predictions_df['sell'].iloc[0] - last_price[0]) / last_price[0]) * 100 |
|
|
predictions_df.loc[predictions_df.index[0], 'buy_change'] = ((predictions_df['buy'].iloc[0] - last_price[1]) / last_price[1]) * 100 |
|
|
|
|
|
|
|
|
predictions_df['sell_change'] = predictions_df['sell_change'].fillna(0) |
|
|
predictions_df['buy_change'] = predictions_df['buy_change'].fillna(0) |
|
|
|
|
|
|
|
|
total_sell_change = ((predictions_df['sell'].iloc[-1] - last_price[0]) / last_price[0]) * 100 |
|
|
total_buy_change = ((predictions_df['buy'].iloc[-1] - last_price[1]) / last_price[1]) * 100 |
|
|
|
|
|
return predictions_df, total_sell_change, total_buy_change, last_price |
|
|
|
|
|
|
|
|
@flask_app.route('/') |
|
|
def home(): |
|
|
return render_template('index.html') |
|
|
|
|
|
@flask_app.route('/predict', methods=['POST']) |
|
|
def predict(): |
|
|
data = request.get_json() |
|
|
|
|
|
|
|
|
sell_features = data['sell_features'] |
|
|
buy_features = data['buy_features'] |
|
|
|
|
|
|
|
|
predictions_df, total_sell_change, total_buy_change, last_price = predict_7_days(sell_features, buy_features, linear_model, scaler) |
|
|
|
|
|
|
|
|
predictions_result = [] |
|
|
for date, (sell, buy, sell_change, buy_change) in predictions_df.iterrows(): |
|
|
predictions_result.append({ |
|
|
'date': date.strftime('%Y-%m-%d'), |
|
|
'sell': round(sell, 2), |
|
|
'buy': round(buy, 2), |
|
|
'sell_change': round(sell_change, 2), |
|
|
'buy_change': round(buy_change, 2) |
|
|
}) |
|
|
|
|
|
|
|
|
result = { |
|
|
'last_price': { |
|
|
'sell': round(last_price[0], 2), |
|
|
'buy': round(last_price[1], 2) |
|
|
}, |
|
|
'predictions': predictions_result, |
|
|
'total_changes': { |
|
|
'sell_change': round(total_sell_change, 2), |
|
|
'buy_change': round(total_buy_change, 2) |
|
|
} |
|
|
} |
|
|
|
|
|
return jsonify(result) |
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
app.mount("/", WSGIMiddleware(flask_app)) |
|
|
|