GaboDataScientist commited on
Commit
bb66d8f
·
verified ·
1 Parent(s): 93f7c7b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from joblib import load
3
+ import pandas as pd
4
+
5
+ path='./huggingface_final_df_deployment.csv'
6
+ df=pd.read_csv(path)
7
+ df['DATE']=pd.to_datetime(df['DATE'])
8
+
9
+ y_values = ['L', 'W']
10
+ y=pd.Series(y_values)
11
+ y=y.astype('category')
12
+
13
+ model=load('./model_SVM.joblib')
14
+ #aquí indicamos cuál es el valor de las clases
15
+ label_encoding=load('./label_classes.joblib') # aquí traemos el orden de clasificación de las categoría
16
+ idx_labels=label_encoding.transform(y.cat.categories) # reordeno mis categorías con esos índices
17
+ class_names=y.cat.categories[idx_labels] # aquí damos los valores de las categorías en el orden que teníamos guardados
18
+ def run_my_model(Home_Team, Away_Team): #aquí ponemos que es solo una entrada pero es UNA TABLA la entrada
19
+
20
+ #Paso1: Función de HomeTeam
21
+ def home_team_recent_record(Home_Team):
22
+ filtered_df=df[df['TEAM_ABBREVIATION']==Home_Team]
23
+ most_recent_record=filtered_df[filtered_df['DATE']==filtered_df['DATE'].max()]
24
+
25
+ return most_recent_record
26
+
27
+ #Paso2: Función de AwayTeam
28
+ def away_team_recent_record(Away_Team):
29
+ filtered_df=df[df['TEAM_ABBREVIATION']==Away_Team]
30
+ most_recent_record=filtered_df[filtered_df['DATE']==filtered_df['DATE'].max()]
31
+
32
+ return most_recent_record
33
+
34
+ #Paso3: Dataframe con últimos records de HomeTeam
35
+ df_home_team_latest_record=home_team_recent_record(Home_Team)
36
+ df_home_team_latest_record=df_home_team_latest_record.reset_index()
37
+
38
+ #Paso4: Dataframe con últimos records de AwayTeam
39
+ df_away_team_latest_record=away_team_recent_record(Away_Team)
40
+ df_away_team_latest_record=df_away_team_latest_record.reset_index()
41
+
42
+ #Paso5: Cálculos
43
+ HOME_AWAY="HOME"
44
+ WINS_DIFFERENTIAL=df_home_team_latest_record.loc[0,"W"]-df_away_team_latest_record.loc[0,"W"]
45
+ WINNING_PERCENTAGE_DIFFERENTIAL=(df_home_team_latest_record.loc[0,'W_PCT']-df_away_team_latest_record.loc[0,'W_PCT'])*100
46
+ AWAY_WINS_DIFFERENTIAL=df_home_team_latest_record.loc[0,'AWAY_WINS']-df_away_team_latest_record.loc[0,'AWAY_WINS']
47
+ WINS_IN_LAST_10_GAMES=df_home_team_latest_record.loc[0,'LAST_10_GAME_WINS']
48
+ WINS_IN_LAST_5_GAMES=df_home_team_latest_record.loc[0,'LAST_5_GAME_WINS']
49
+ BACK_TO_BACK="NO_B2B"
50
+
51
+ #Paso6: Convierto a un input dataframe
52
+ data = {
53
+ 'HOME/AWAY': [HOME_AWAY],
54
+ 'WINS_DIFFERENTIAL': [WINS_DIFFERENTIAL],
55
+ 'WINNING PERCENTAGE DIFFERENTIAL': [WINNING_PERCENTAGE_DIFFERENTIAL],
56
+ 'AWAY WINS DIFFERENTIAL': [AWAY_WINS_DIFFERENTIAL],
57
+ 'WINS IN LAST 10 GAMES': [WINS_IN_LAST_10_GAMES],
58
+ 'WINS IN LAST 5 GAMES': [WINS_IN_LAST_5_GAMES],
59
+ 'BACK-TO-BACK': [BACK_TO_BACK]}
60
+
61
+ input_df = pd.DataFrame(data)
62
+
63
+ #Paso7: Creación del If
64
+ if class_names[model.predict(input_df)][0]=="W":
65
+ Winning_Team=Home_Team
66
+ else:
67
+ Winning_Team=Away_Team
68
+
69
+ return "The winning team is: " + Winning_Team
70
+
71
+ gui= gr.Interface(
72
+ fn=run_my_model,
73
+ inputs=["text","text"],
74
+ outputs="text",
75
+ live=False,
76
+ title='NBA Regular games outcome prediction AI Oracle'
77
+ )
78
+
79
+ gui.launch()