2024-03-06 18:53:48 +03:00
|
|
|
import json
|
2024-09-04 21:24:42 +03:00
|
|
|
from flask import Flask, Response
|
|
|
|
from model import download_data, format_data, train_model, get_inference
|
|
|
|
from config import model_file_path, TOKEN, TIMEFRAME, TRAINING_DAYS, REGION, DATA_PROVIDER
|
|
|
|
|
2024-03-06 18:53:48 +03:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def update_data():
|
|
|
|
"""Download price data, format data and train model."""
|
2024-09-04 21:24:42 +03:00
|
|
|
files = download_data(TOKEN, TRAINING_DAYS, REGION, DATA_PROVIDER)
|
|
|
|
format_data(files, DATA_PROVIDER)
|
|
|
|
train_model(TIMEFRAME)
|
2024-03-06 18:53:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/inference/<string:token>")
|
|
|
|
def generate_inference(token):
|
|
|
|
"""Generate inference for given token."""
|
2024-09-04 21:24:42 +03:00
|
|
|
if not token or token.upper() != TOKEN:
|
2024-03-06 18:53:48 +03:00
|
|
|
error_msg = "Token is required" if not token else "Token not supported"
|
2024-06-17 11:37:20 +03:00
|
|
|
return Response(json.dumps({"error": error_msg}), status=400, mimetype='application/json')
|
2024-03-06 18:53:48 +03:00
|
|
|
|
|
|
|
try:
|
2024-09-04 21:24:42 +03:00
|
|
|
inference = get_inference(token.upper(), TIMEFRAME, REGION, DATA_PROVIDER)
|
2024-06-17 11:37:20 +03:00
|
|
|
return Response(str(inference), status=200)
|
2024-03-06 18:53:48 +03:00
|
|
|
except Exception as e:
|
2024-06-17 11:37:20 +03:00
|
|
|
return Response(json.dumps({"error": str(e)}), status=500, mimetype='application/json')
|
2024-03-06 18:53:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/update")
|
|
|
|
def update():
|
|
|
|
"""Update data and return status."""
|
|
|
|
try:
|
|
|
|
update_data()
|
|
|
|
return "0"
|
|
|
|
except Exception:
|
|
|
|
return "1"
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
update_data()
|
|
|
|
app.run(host="0.0.0.0", port=8000)
|