-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
76 lines (66 loc) · 2.07 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
import os
import requests
import json
from flask import Flask, render_template, request
from datetime import datetime
from constants import TEAMS, LEAGUES
API_KEY = os.getenv("DATAROBOT_API_KEY")
DEPLOYMENT_SERVER_URL = os.getenv("DATAROBOT_DEPLOYMENT_SERVER_URL")
DEPLOYMENT_ID = os.getenv("DATAROBOT_DEPLOYMENT_ID")
PREDICTION_HEADERS = {
"Authorization": "Bearer {}".format(API_KEY),
"Content-Type": "application/json"
}
app = Flask(__name__)
def normalize_score(score):
if score is None:
return None
elif(score < 0.7):
return 0
else:
return round(score)
@app.route('/')
def root():
spi1 = spi2 = score1 = score2 = None
team1 = request.args.get('team1', None)
team2 = request.args.get('team2', None)
league = request.args.get('league', None)
if team1 and team2 and league:
spi1 = TEAMS[team1]
spi2 = TEAMS[team2]
matches = [
{
'date': datetime.now().strftime("%Y-%m-%d"),
'league': league,
'team1': team1,
'team2': team2,
'spi1': spi1,
'spi2': spi2
},
{
'date': datetime.now().strftime("%Y-%m-%d"),
'league': league,
'team1': team2,
'team2': team1,
'spi1': spi2,
'spi2': spi1
}
]
predictions = requests.post(
"{server_url}/api/v2/deployments/{deployment_id}/predictions".format(server_url=DEPLOYMENT_SERVER_URL, deployment_id=DEPLOYMENT_ID),
headers=PREDICTION_HEADERS,
data=json.dumps(matches),
)
print(predictions.json())
score1 = float(predictions.json()['data'][0]['prediction'])
score2 = float(predictions.json()['data'][1]['prediction'])
return render_template(
'index.html',
teams=TEAMS.keys(),
leagues=LEAGUES,
team1=team1,
team2=team2,
league=league,
score1=normalize_score(score1),
score2=normalize_score(score2)
)