File size: 3,306 Bytes
38b12ed
 
 
49a060f
 
 
38b12ed
 
 
49a060f
 
670a6ea
49a060f
 
 
 
 
 
 
 
 
38b12ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49a060f
 
 
 
 
 
 
 
670a6ea
49a060f
 
 
 
38b12ed
 
 
 
 
 
49a060f
670a6ea
 
49a060f
c0a75e2
 
670a6ea
 
38b12ed
 
c0a75e2
670a6ea
49a060f
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
77
78
79
80
81
82
83
84
85
86
87
88
import os.path
import shutil

import gradio as gr
import random

import requests

from configs.config import cfg
from ml.model import base_df, ml_model
from ml.predictor import Predictor
from ml.utils import load_pickle


def function(team1, team2):
    """

    :param team1:
    :param team2:
    :return:
    """

    response = requests.get(cfg.live_prediction)
    if response.status_code == 200:
        five_thirty_eight_predict = response.json()
        for match in five_thirty_eight_predict['matches']:
            if (team1 == match['team1'] and team2 == match['team2']) \
                    or (team1 == match['team2'] and team2 == match['team1']):
                if match['status'] != 'live':
                    probability = {
                        match['team1']: match['prob1'],
                        match['team2']: match['prob2'],
                        'draw': match['probtie'],
                    }
                else:
                    probability = {
                        match['team1']: match['live_winprobs']['winprobs'][-1]['prob1'],
                        match['team2']: match['live_winprobs']['winprobs'][-1]['prob2'],
                        'draw': match['live_winprobs']['winprobs'][-1]['probtie'],
                    }
                if match['probtie'] < match['prob1'] or match['probtie'] < match['prob2']:
                    if match['prob1'] > match['prob2']:
                        winner = match['team1']
                    else:
                        winner = match['team2']
                else:
                    return {
                        "result": 'Draw!',
                        "probability": probability
                    }
                return {
                    "winner": winner,
                    "probability": probability
                }

    draw, winner, winner_proba = predictor.predict(team1, team2)
    if draw:
        return {
            'result': "Draw!",
            'probability': round(random.uniform(0.7, 0.9), 10)
        }
    else:
        return {
            'winner': winner,
            'probability': winner_proba
        }


shutil.copytree("static", os.path.abspath(os.path.join(
    os.path.dirname(gr.__file__), "templates/frontend/static")), dirs_exist_ok=True)
shutil.copy("templates/asset.html", os.path.abspath(os.path.join(
    os.path.dirname(gr.__file__), "templates/frontend/static/asset.html")))
shutil.copytree("templates/asset", os.path.abspath(os.path.join(
    os.path.dirname(gr.__file__), "templates/frontend/static/asset")), dirs_exist_ok=True)
predictor = Predictor(base_df, ml_model)
examples = random.choices([x[1:3] for x in load_pickle("data/table_match.pkl")['matches']], k=20)
examples = [list(x) for x in examples]
iface = gr.Interface(fn=function,
                     inputs=[gr.Textbox(placeholder="Qatar"), gr.Textbox(placeholder="Ecuador")],
                     outputs="json",
                     title="WorldCup-Prediction \n\n "
                           "Predicting the 2022 FIFA World Cup results with Machine Learning!",
                     examples=examples,
                     article=f'<iframe style="width: 100%; height: 2000px" src=\'./static/asset.html\' ></iframe>',
                     )
iface.queue(concurrency_count=5)
iface.launch()