Spaces:
Build error
Build error
| import math | |
| 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 | |
| def get_result(team1, prob1, score1, team2, prob2, score2, probtie): | |
| if prob1 > prob2 and prob1 > probtie: | |
| winner = {"name": team1, "probability": prob1, "goals": score1} | |
| loser = {"name": team2, "probability": prob2, "goals": score2} | |
| elif prob1 < prob2 and prob2 > probtie: | |
| loser = {"name": team1, "probability": prob1, "goals": score1} | |
| winner = {"name": team2, "probability": prob2, "goals": score2} | |
| else: | |
| loser = {"name": None, "probability": 0.0, "goals": score1} | |
| winner = {"name": None, "probability": 0.0, "goals": score2} | |
| result = { | |
| "winner": winner, | |
| "loser": loser, | |
| "draw": {"probability": probtie}, | |
| } | |
| return result | |
| 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 not ( | |
| (team1 == match["team1"] and team2 == match["team2"]) | |
| or (team1 == match["team2"] and team2 == match["team1"]) | |
| ): | |
| continue | |
| if match["status"] != "live": | |
| result = get_result( | |
| match["team1"], | |
| match["prob1"], | |
| math.ceil(match["adj_score1"]) | |
| if "adj_score1" in match | |
| else math.ceil(match["o1"] - match["d2"]), | |
| match["team2"], | |
| match["prob2"], | |
| math.ceil(match["adj_score2"]) | |
| if "adj_score2" in match | |
| else math.ceil(match["o2"] - match["d1"]), | |
| match["probtie"], | |
| ) | |
| else: | |
| result = get_result( | |
| match["team1"], | |
| match["live_winprobs"]["winprobs"][-1]["prob1"], | |
| math.ceil(match["adj_score1"]) | |
| if "adj_score1" in match | |
| else math.ceil(match["o1"] - match["d2"]), | |
| match["team2"], | |
| match["live_winprobs"]["winprobs"][-1]["prob2"], | |
| math.ceil(match["adj_score2"]) | |
| if "adj_score2" in match | |
| else math.ceil(match["o2"] - match["d1"]), | |
| match["probtie"], | |
| ) | |
| return result | |
| draw, winner, winner_proba = predictor.predict(team1, team2) | |
| if draw: | |
| draw_prob = round(random.uniform(0.7, 0.9), 10) | |
| winner_proba = round(random.uniform(0, 1 - draw_prob), 10) | |
| loser_proba = 1 - draw_prob - winner_proba | |
| return { | |
| "winner": {"name": team1, "probability": winner_proba, "goals": None}, | |
| "loser": {"name": team2, "probability": loser_proba, "goals": None}, | |
| "draw": {"probability": draw_prob}, | |
| } | |
| else: | |
| loser_proba = round(random.uniform(0, 1 - winner_proba), 10) | |
| return { | |
| "winner": {"name": winner, "probability": winner_proba, "goals": None}, | |
| "loser": { | |
| "name": team1 if winner == team2 else team2, | |
| "probability": loser_proba, | |
| "goals": None, | |
| }, | |
| "draw": {"probability": 1 - winner_proba - loser_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 = ( | |
| ("Croatia", "Argentina"), | |
| ("Morocco", "France"), | |
| ("Argentina", "France"), | |
| ("Morocco", "Croatia"), | |
| ) | |
| 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() | |