-
Notifications
You must be signed in to change notification settings - Fork 78
/
app.py
43 lines (32 loc) · 1.29 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
import json
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
app = Flask(__name__)
def update_data():
"""Download price data, format data and train model."""
files = download_data(TOKEN, TRAINING_DAYS, REGION, DATA_PROVIDER)
format_data(files, DATA_PROVIDER)
train_model(TIMEFRAME)
@app.route("/inference/<string:token>")
def generate_inference(token):
"""Generate inference for given token."""
if not token or token.upper() != TOKEN:
error_msg = "Token is required" if not token else "Token not supported"
return Response(json.dumps({"error": error_msg}), status=400, mimetype='application/json')
try:
inference = get_inference(token.upper(), TIMEFRAME, REGION, DATA_PROVIDER)
return Response(str(inference), status=200)
except Exception as e:
return Response(json.dumps({"error": str(e)}), status=500, mimetype='application/json')
@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)