| import gradio as gr |
| from joblib import load |
| import pandas as pd |
|
|
| path='./huggingface_final_df_deployment.csv' |
| df=pd.read_csv(path) |
| df['DATE']=pd.to_datetime(df['DATE']) |
|
|
| y_values = ['L', 'W'] |
| y=pd.Series(y_values) |
| y=y.astype('category') |
|
|
| model=load('./model_LogisticRegression.joblib') |
| |
| label_encoding=load('./label_classes.joblib') |
| idx_labels=label_encoding.transform(y.cat.categories) |
| class_names=y.cat.categories[idx_labels] |
| def run_my_model(Home_Team, Away_Team): |
| |
| |
| def home_team_recent_record(Home_Team): |
| filtered_df=df[df['TEAM_ABBREVIATION']==Home_Team] |
| most_recent_record=filtered_df[filtered_df['DATE']==filtered_df['DATE'].max()] |
| |
| return most_recent_record |
|
|
| |
| def away_team_recent_record(Away_Team): |
| filtered_df=df[df['TEAM_ABBREVIATION']==Away_Team] |
| most_recent_record=filtered_df[filtered_df['DATE']==filtered_df['DATE'].max()] |
| |
| return most_recent_record |
| |
| |
| df_home_team_latest_record=home_team_recent_record(Home_Team) |
| df_home_team_latest_record=df_home_team_latest_record.reset_index() |
| |
| |
| df_away_team_latest_record=away_team_recent_record(Away_Team) |
| df_away_team_latest_record=df_away_team_latest_record.reset_index() |
| |
| |
| HOME_AWAY="HOME" |
| WINS_DIFFERENTIAL=df_home_team_latest_record.loc[0,"W"]-df_away_team_latest_record.loc[0,"W"] |
| WINNING_PERCENTAGE_DIFFERENTIAL=(df_home_team_latest_record.loc[0,'W_PCT']-df_away_team_latest_record.loc[0,'W_PCT'])*100 |
| AWAY_WINS_DIFFERENTIAL=df_home_team_latest_record.loc[0,'AWAY_WINS']-df_away_team_latest_record.loc[0,'AWAY_WINS'] |
| WINS_IN_LAST_10_GAMES=df_home_team_latest_record.loc[0,'LAST_10_GAME_WINS'] |
| WINS_IN_LAST_5_GAMES=df_home_team_latest_record.loc[0,'LAST_5_GAME_WINS'] |
| BACK_TO_BACK="NO_B2B" |
| |
| |
| data = { |
| 'HOME/AWAY': [HOME_AWAY], |
| 'WINS_DIFFERENTIAL': [WINS_DIFFERENTIAL], |
| 'WINNING PERCENTAGE DIFFERENTIAL': [WINNING_PERCENTAGE_DIFFERENTIAL], |
| 'AWAY WINS DIFFERENTIAL': [AWAY_WINS_DIFFERENTIAL], |
| 'WINS IN LAST 10 GAMES': [WINS_IN_LAST_10_GAMES], |
| 'WINS IN LAST 5 GAMES': [WINS_IN_LAST_5_GAMES], |
| 'BACK-TO-BACK': [BACK_TO_BACK]} |
|
|
| input_df = pd.DataFrame(data) |
| |
| |
| if class_names[model.predict(input_df)][0]=="W": |
| Winning_Team=Home_Team |
| Winning_Proba=model.predict_proba(input_df)[0][1] |
| else: |
| Winning_Team=Away_Team |
| Winning_Proba=model.predict_proba(input_df)[0][0] |
| |
| return f"The team with higher probability of winning is: {Winning_Team} with a probability of {Winning_Proba * 100:.2f}%" |
|
|
| gui= gr.Interface( |
| fn=run_my_model, |
| inputs=[gr.Dropdown(['ATL', 'BKN', 'BOS', 'CHA', 'CHI', 'CLE', 'DAL', 'DEN', 'DET', 'GSW', 'HOU', 'IND', 'LAC', 'LAL', 'MEM', 'MIA', 'MIL', 'MIN', 'NOP', 'NYK', 'OKC', 'ORL', 'PHI', 'PHX', 'POR', 'SAC', 'SAS', 'TOR', 'UTA', 'WAS'],label="HOME TEAM"),gr.Dropdown(['ATL', 'BKN', 'BOS', 'CHA', 'CHI', 'CLE', 'DAL', 'DEN', 'DET', 'GSW', 'HOU', 'IND', 'LAC', 'LAL', 'MEM', 'MIA', 'MIL', 'MIN', 'NOP', 'NYK', 'OKC', 'ORL', 'PHI', 'PHX', 'POR', 'SAC', 'SAS', 'TOR', 'UTA', 'WAS'],label="AWAY TEAM")], |
| outputs="text", |
| live=False, |
| title='NBA regular season games outcome prediction', |
| description='IMPORTANT: The predictions generated by this AI tool are for informational purposes only and should not be considered as any type of advice. Past performance is not indicative of future results. This predictions are only for NBA regular season games.' |
| ) |
|
|
| gui.launch() |