Implements Rest API. Updates Web Interface.

This commit is contained in:
Azee 2020-05-03 15:15:31 -05:00
parent b271b80aab
commit 2f9657879e
16 changed files with 235 additions and 12 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -102,8 +102,8 @@ print(data)
# Pre processing
input_filter = ['home_encoded', 'away_encoded', 'HS',
'AS', 'HST', 'AST', 'HTCT', 'ATCT', 'HR', 'AR']
input_filter = ['home_encoded', 'away_encoded', 'HTHG', 'HTAG', 'HS',
'AS', 'HST', 'AST', 'HR', 'AR']
output_filter = ['FTR']
cols_to_consider = input_filter + output_filter
@ -120,11 +120,12 @@ away_encoded_mapping = dict(
zip(encoder.classes_, encoder.transform(encoder.classes_).tolist()))
data['away_encoded'] = away_encoded
htg_df = data[['HTHG', 'HTAG']]
cs_data = derive_clean_sheet(htg_df)
cs_df = pd.DataFrame(cs_data, columns=['HTCT', 'ATCT'])
# Deriving Clean Sheet
# htg_df = data[['HTHG', 'HTAG']]
# cs_data = derive_clean_sheet(htg_df)
# cs_df = pd.DataFrame(cs_data, columns=['HTCS', 'ATCS'])
data = pd.concat([data, cs_df], axis=1)
# data = pd.concat([data, cs_df], axis=1)
data = data[cols_to_consider]

0
server/api/__init__.py Normal file
View File

3
server/api/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
server/api/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'

View File

3
server/api/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
server/api/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

22
server/api/urls.py Normal file
View File

@ -0,0 +1,22 @@
"""server URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('predict', views.index),
]

65
server/api/views.py Normal file
View File

@ -0,0 +1,65 @@
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, JsonResponse
from os import path
from joblib import dump, load
from time import time
import pandas as pd
import numpy as np
import json
# Create your views here.
def index(request: HttpRequest):
result = {
'success': False,
'data': {},
'msg': '',
}
classifierModels = {
'lr': 'lr_classifier.model',
'nb': 'nb_classifier.model',
'rf': 'rf_classifier.model',
}
selectedClassifier = request.GET['classifier']
if selectedClassifier not in classifierModels:
return HttpResponse(json.dumps(result))
exportedModelFilePath = path.abspath(path.dirname(
__name__)) + '/../exportedModels/' + classifierModels[selectedClassifier]
# print(exportMetaDataFilePath)
X = pd.DataFrame({
'home_encoded': request.GET['homeTeam'],
'away_encoded': request.GET['awayTeam'],
'HTHG': request.GET['homeGoals'],
'HTAG': request.GET['awayGoals'],
'HS': request.GET['homeShots'],
'AS': request.GET['awayShots'],
'HST': request.GET['homeShotsOnTarget'],
'AST': request.GET['awayShotsOnTarget'],
'HR': request.GET['homeRedCards'],
'AR': request.GET['awayRedCards'],
}, index = [0])
if path.exists(exportedModelFilePath):
clf = load(exportedModelFilePath)
# print(exportMetaData)
start = time()
pred_probs = clf.predict_proba(X)[0]
y = dict(zip(clf.classes_, pred_probs))
end = time()
print("Made Predictions in {:2f} seconds".format(end-start))
print("Result: ")
print(y)
result['data']['homeWin'] = y['H']
result['data']['draw'] = y['D']
result['data']['awayWin'] = y['A']
return JsonResponse(result)

View File

@ -32,6 +32,7 @@ ALLOWED_HOSTS = []
INSTALLED_APPS = [
'web.apps.WebConfig',
'api.apps.ApiConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',

View File

@ -19,4 +19,5 @@ from django.urls import path, include
urlpatterns = [
# path('admin/', admin.site.urls),
path('', include('web.urls')),
path('api/', include('api.urls')),
]

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -44,4 +44,18 @@ form.predictionForm table tr {
.btn:hover {
background-color: #333;
}
.prob-container {
height: 100%;
}
.prob-container .prob-title {
color: white;
line-height: 36px;
}
.prob-container .prob-value {
color: white;
font-size: 24px;
}

View File

@ -32,12 +32,18 @@
<div class="container section">
<div class="card center">
<div class="card-image">
<img style="background-size: cover; background-position: center;
background-image: url({% static "web/flat-card-image.jpg" %});" height="200px"
/>
<span class="card-title">Select Half-Time Stats</span>
</div>
<form class="card-content predictionForm" action="">
<table>
<tr>
<td>
<div class="input-field col s12">
<select required class="validate">
<select id="homeTeam" required>
<option value="" disabled selected>Select Home Team</option>
{% for team, index in home_teams.items %}
<option value="{{ index }}">{{ team }}</option>
@ -48,7 +54,7 @@
</td>
<td>
<div class="input-field col s12">
<select required>
<select id="awayTeam" required>
<option value="" disabled selected>Select Away Team</option>
{% for team, index in away_teams.items %}
<option value="{{ index }}">{{ team }}</option>
@ -58,14 +64,113 @@
</div>
</td>
</tr>
<tr>
<td>
<div class="input-field col s12">
<input
id="homeShots"
type="number"
class="validate"
value="0"
/>
<label for="homeShots">Shots</label>
</div>
</td>
<td>
<div class="input-field col s12">
<input
id="awayShots"
type="number"
class="validate"
value="0"
/>
<label for="awayShots">Shots</label>
</div>
</td>
</tr>
<tr>
<td>
<div class="input-field col s12">
<input
id="homeShotsOnTarget"
type="number"
class="validate"
value="0"
/>
<label for="homeShotsOnTarget">Shots on target</label>
</div>
</td>
<td>
<div class="input-field col s12">
<input
id="awayShotsOnTarget"
type="number"
class="validate"
value="0"
/>
<label for="awayShotsOnTarget">Shots on target</label>
</div>
</td>
</tr>
<tr>
<td>
<div class="input-field col s12">
<input
id="homeRedCards"
type="number"
class="validate"
value="0"
/>
<label for="homeRedCards">Red Cards</label>
</div>
</td>
<td>
<div class="input-field col s12">
<input
id="awayRedCards"
type="number"
class="validate"
value="0"
/>
<label for="awayRedCards">Red Cards</label>
</div>
</td>
</tr>
</table>
<br />
<button class="btn waves-effect waves-light" type="submit" name="action">
Submit
Predict Result
<i class="material-icons right">send</i>
</button>
<div class="row" style="height: 80px; display: block">
<br />
<br />
<div id="homeWinProbability" class="col blue prob-container" style="width: 33.3%;">
<span class="prob-title">Home Win</span>
<br/>
<span class="prob-value">33.3%</span>
</div>
<div id="drawProbability" class="col green prob-container" style="width: 33.3%;">
<span class="prob-title">Draw</span>
<br/>
<span class="prob-value">33.3%</span>
</div>
<div id="awayWinProbability" class="col red prob-container" style="width: 33.3%;">
<span class="prob-title">Away Win</span>
<br/>
<span class="prob-value">33.3%</span>
</div>
</div>
<br/>
</form>
</div>
</div>
@ -75,7 +180,7 @@
style="text-decoration: underline;"
href="https://github.com/AzeeSoft/football-match-predictor"
target="blank"
>
>
View Source on GitHub</a
>
</center>

View File

@ -8,7 +8,7 @@ import json
def index(request):
exportMetaDataFilePath = path.abspath(path.dirname(__name__)) + '/../exportedModels/metaData.json'
print(exportMetaDataFilePath)
# print(exportMetaDataFilePath)
exportMetaData = {
'home_teams': {},
@ -17,7 +17,7 @@ def index(request):
if path.exists(exportMetaDataFilePath):
exportMetaDataFile = open(exportMetaDataFilePath, 'r')
exportMetaData = json.load(exportMetaDataFile)
print(exportMetaData)
# print(exportMetaData)
template = loader.get_template('web/index.html')
context = {