File size: 7,601 Bytes
4547396
 
 
 
 
7488952
4547396
9947cd9
 
d7b1ed3
 
 
 
 
9947cd9
d7b1ed3
4547396
d7b1ed3
 
 
 
4547396
309800b
4547396
309800b
16ff37d
4547396
d7b1ed3
 
 
4547396
 
d7b1ed3
a045f2c
 
16ff37d
d7b1ed3
4547396
7488952
 
 
f6ceff9
 
 
 
 
7488952
 
 
 
d7b1ed3
 
 
 
 
7488952
d7b1ed3
 
 
7488952
 
309800b
7488952
d7b1ed3
 
7488952
d7b1ed3
 
7488952
 
 
d7b1ed3
a045f2c
 
16ff37d
d7b1ed3
309800b
16ff37d
 
 
d7b1ed3
7488952
 
 
 
f6ceff9
 
 
 
 
4547396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7488952
 
 
 
 
 
4547396
 
7488952
 
4547396
 
229eff2
7488952
 
 
 
 
 
229eff2
 
7488952
 
 
4547396
 
 
 
 
9947cd9
4547396
9947cd9
7488952
9947cd9
 
4547396
229eff2
 
7488952
 
9947cd9
7488952
9947cd9
 
229eff2
7488952
 
9947cd9
7488952
229eff2
4547396
 
 
 
7488952
 
 
 
 
 
 
 
 
 
 
 
 
4547396
7488952
 
 
 
 
4547396
4c6501c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import gradio as gr
import numpy as np
import pandas as pd
import pickle
import xgboost as xgb
from catboost import CatBoostRegressor

def predict(team, inning, venue, hits, errors, lob, runs, opp_team, opp_runs, opp_hits):
    data = [team, opp_team, inning, venue, hits, opp_hits, errors, runs, opp_runs, lob]

    df_main = pd.read_csv("Score_prediction_dataset_11th_July.csv")
    df_main = df_main.drop(columns=['Final_Score', 'Opp_LOB'])
    df_main = pd.get_dummies(df_main, columns=['Team_Name', 'Opposition_Team'])

    df = pd.DataFrame([data], columns=["Team_Name", "Opposition_Team", "Inning", "Home/Away", "Hits", "Opp_Hits", "Errors", "Runs", "Opp_Runs", "LOB"])
    df = pd.get_dummies(df, columns=['Team_Name', 'Opposition_Team'])

    df = df.reindex(columns=df_main.columns, fill_value=0)

    # print(df.columns)
    # print(len(df.columns))
    xgb_model = xgb.XGBRegressor()
    xgb_model.load_model('xgbr1_exp10_model.json')
    
    with open('pca_model7.pkl', 'rb') as f:
        pca = pickle.load(f)

    # with open('label_encoder_teams_xgbr1_exp3.pkl', 'rb') as f:
    #     label_encoder = pickle.load(f)
    
    home_away_status = {'Home': 0, 'Away': 1}
    df['Home/Away'] = df['Home/Away'].map(home_away_status)

    df = df.astype(int)

    df = pca.transform(df)

    score = xgb_model.predict(df)
    if score[0] < 0:
        score = np.clip(score[0], a_min=0, a_max=None)
        return np.round(score,1)

    if score[0] < runs:
        score = runs
        return score
    
    return np.round(score[0],1)

def predict_2(team, inning, venue, hits, errors, lob, runs, opp_team, opp_runs, opp_hits):
    data = [team, opp_team, inning, venue, hits, opp_hits, errors, runs, opp_runs, lob]

    df_main = pd.read_csv("Score_prediction_dataset_11th_July.csv")
    df_main = df_main.drop(columns=['Final_Score', 'Opp_LOB'])
    df_main = pd.get_dummies(df_main, columns=['Team_Name', 'Opposition_Team'])

    df = pd.DataFrame([data], columns=["Team_Name", "Opposition_Team", "Inning", "Home/Away", "Hits", "Opp_Hits", "Errors", "Runs", "Opp_Runs", "LOB"])
    df = pd.get_dummies(df, columns=['Team_Name', 'Opposition_Team'])

    df = df.reindex(columns=df_main.columns, fill_value=0)

    cat_model = CatBoostRegressor()
    cat_model.load_model('catbr1_exp11_model.json')
    
    # with open('label_encoder_teams_catbr1_exp1.pkl', 'rb') as f:
    #     label_encoder = pickle.load(f)

    # df['Team_Name'] = label_encoder.transform(df['Team_Name'])
    # df['Opposition_Team'] = label_encoder.transform(df['Opposition_Team'])

    home_away_status = {'Home': 0, 'Away': 1}
    df['Home/Away'] = df['Home/Away'].map(home_away_status)

    df = df.astype(int)

    # print(df)

    with open('pca_model7.pkl', 'rb') as f:
        pca = pickle.load(f)    

    df = pca.transform(df)
    
    score = cat_model.predict(df)
    if score[0] < 0:
        score = np.clip(score[0], a_min=0, a_max=None)
        return np.round(score,1)

    if score[0] < runs:
        score = runs
        return score
        
    return np.round(score[0],1)

team_names = ["Arizona Diamondbacks",
"Atlanta Braves",
"Baltimore Orioles",
"Boston Red Sox",
"Chicago Cubs",
"Chicago White Sox",
"Cincinnati Reds",
"Cleveland Guardians",
"Colorado Rockies",
"Detroit Tigers",
"Houston Astros",
"Kansas City Royals",
"Los Angeles Angels",
"Los Angeles Dodgers",
"Miami Marlins",
"Milwaukee Brewers",
"Minnesota Twins",
"New York Mets",
"New York Yankees",
"Oakland Athletics",
"Philadelphia Phillies",
"Pittsburgh Pirates",
"San Diego Padres",
"San Francisco Giants",
"Seattle Mariners",
"St. Louis Cardinals",
"Tampa Bay Rays",
"Texas Rangers",
"Toronto Blue Jays",
"Washington Nationals"]

with gr.Blocks() as demo:
    # gr.Image("../Documentation/Context Diagram.png", scale=2)
    # gr(title="Your Interface Title")
    gr.Markdown("""
                <center> 
                <span style='font-size: 50px; font-weight: Bold; font-family: "Graduate", serif'>
                MLB Score Predictor 
                </span>
                </center>
                """)
    # gr.Markdown("""
    #             <center> 
    #             <span style='font-size: 30px; line-height: 0.1; font-weight: Bold; font-family: "Graduate", serif'>
    #             Admin Dashboard 
    #             </span>
    #             </center>
    #             """)
    with gr.Row():
        inning = gr.Number(None, label="Inning", minimum = 1, maximum = 8, scale=1)
    
    with gr.Row():
        with gr.Column():
            venue = gr.Dropdown(choices = ["Home", "Away"], value="Away", max_choices = 1, label="Home/Away Status", scale=1)  

        
        with gr.Column():
            opp_venue = gr.Dropdown(choices = ["Home", "Away"], value="Home", max_choices = 1, label="Opposition Home/Away Status", scale=1)  

    
    with gr.Row():
        with gr.Column():
            team = gr.Dropdown(choices = team_names, max_choices = 1, label="Team", scale=1)  
        
        with gr.Column():
            opp_team = gr.Dropdown(choices = team_names, max_choices = 1, label="Opposition Team", scale=1) 
    
    with gr.Row():
        with gr.Column():
            hits = gr.Number(None, minimum=0, label="Hits - (H)", scale=1)
            
        with gr.Column():
            opp_hits = gr.Number(None, minimum=0, label="Opposition Hits - (H)", scale=1)
    
    # summarize_btn = gr.Button(value="Summarize Text", size = 'sm')

    with gr.Row():
        with gr.Column():
            errors = gr.Number(None, minimum=0, label="Errors - (E)", scale=2)
        
        with gr.Column():
            opp_errors = gr.Number(None, minimum=0, label="Opposition Errors - (E)", scale=2)

        # runs = gr.Number(None, minimum=0, label="Runs - (R)", scale=1)

    with gr.Row():
        with gr.Column():
            lob = gr.Number(None, minimum=0, label="Left on Base - (LOB)", scale=1)

        with gr.Column():
            opp_lob = gr.Number(None, minimum=0, label="Opposition Left on Base - (LOB)", scale=1)

    with gr.Row():
        with gr.Column():
            runs = gr.Number(None, minimum=0, label="Runs - (R)", scale=1)

        with gr.Column():
            opp_runs = gr.Number(None, minimum=0, label="Opposition Runs - (R)", scale=1)

    with gr.Row():
        predict_btn = gr.Button(value="Predict", size = 'sm')

    with gr.Row():
        with gr.Column():
            final_score_away1 = gr.Textbox(label="Predicted Score Model XGB", scale=1)

        with gr.Column():
            final_score_home1 = gr.Textbox(label="Opposition Predicted Score Model XGB", scale=1)

    with gr.Row():
        with gr.Column():
            final_score_away2 = gr.Textbox(label="Predicted Score Model CATB", scale=1)

        with gr.Column():
            final_score_home2 = gr.Textbox(label="Opposition Predicted Score Model CATB", scale=1)

    # patent_doc.upload(document_to_text, inputs = [patent_doc, slider, select_model], outputs=summary_doc)
    predict_btn.click(predict, inputs=[team, inning, venue, hits, errors, lob, runs, opp_team, opp_runs, opp_hits], outputs=final_score_away1)
    predict_btn.click(predict, inputs=[opp_team, inning, opp_venue, opp_hits, opp_errors, opp_lob, opp_runs, team, runs, hits], outputs=final_score_home1)

    predict_btn.click(predict_2, inputs=[team, inning, venue, hits, errors, lob, runs, opp_team, opp_runs, opp_hits], outputs=final_score_away2)
    predict_btn.click(predict_2, inputs=[opp_team, inning, opp_venue, opp_hits, opp_errors, opp_lob, opp_runs, team, runs, hits], outputs=final_score_home2)

demo.launch(inline=False)