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') #aquí indicamos cuál es el valor de las clases label_encoding=load('./label_classes.joblib') # aquí traemos el orden de clasificación de las categoría idx_labels=label_encoding.transform(y.cat.categories) # reordeno mis categorías con esos índices class_names=y.cat.categories[idx_labels] # aquí damos los valores de las categorías en el orden que teníamos guardados def run_my_model(Home_Team, Away_Team): #aquí ponemos que es solo una entrada pero es UNA TABLA la entrada #Paso1: Función de HomeTeam 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 #Paso2: Función de AwayTeam 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 #Paso3: Dataframe con últimos records de HomeTeam df_home_team_latest_record=home_team_recent_record(Home_Team) df_home_team_latest_record=df_home_team_latest_record.reset_index() #Paso4: Dataframe con últimos records de AwayTeam df_away_team_latest_record=away_team_recent_record(Away_Team) df_away_team_latest_record=df_away_team_latest_record.reset_index() #Paso5: Cálculos 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" #Paso6: Convierto a un input dataframe 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) #Paso7: Creación del If 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()