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'', ) iface.queue(concurrency_count=5) iface.launch()