Download app.py from ququ227/ftapp: direct link, hf CLI and curl.
- Browser
- Download file 4.63 kB
-
https://huggingface.co/spaces/ququ227/ftapp/resolve/main/app.py
- Command line
-
hf download hf://spaces/ququ227/ftapp/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ququ227/ftapp/resolve/main/app.py
4.63 kB
| import glob | |
| import os | |
| import gradio as gr | |
| import numpy as np | |
| import pandas as pd | |
| import joblib | |
| all_model_path = r'各个赛区保存的模型' | |
| league_dict = {}# 赛季 :路径 | |
| for file in os.listdir(all_model_path): | |
| dir_path = os.path.join(all_model_path, file) | |
| if os.path.isdir(dir_path): | |
| pkl_files = glob.glob(os.path.join(dir_path, "*.pkl")) | |
| if pkl_files: | |
| latest_pkl = sorted(pkl_files, key=os.path.getmtime)[-1] | |
| league_dict[file] = latest_pkl | |
| league_choices = ["请先选择赛区"] + list(league_dict.keys())#赛区名字 | |
| class FootballPredictor: | |
| def __init__(self,model_path): | |
| self.model_data = joblib.load(model_path) | |
| self.xgb_model = None | |
| self.poisson_model = None | |
| self.scaler = None | |
| self.feature_list = pd.DataFrame() | |
| self.team_data = pd.DataFrame() | |
| self.load_model() | |
| def load_model(self) -> None: | |
| self.xgb_model = self.model_data['xgb_model'] | |
| self.poisson_model = self.model_data['poisson_model'] | |
| self.scaler = self.model_data['scaler'] | |
| self.feature_list = self.model_data['feature_list'] | |
| self.team_data = self.model_data['team_data'] | |
| return | |
| def predict_goals(self,home_team,away_team): | |
| df_prev = pd.DataFrame() | |
| home_td = self.team_data.loc[self.team_data['HomeTeam'] == home_team].tail(1).reset_index(drop=True) | |
| home_td = home_td.drop(columns=['HomeTeam','AwayTeam'], axis=1) | |
| away_td = self.team_data.loc[self.team_data['AwayTeam'] == away_team].tail(1).reset_index(drop=True) | |
| away_td = away_td.drop(columns=['HomeTeam','AwayTeam'], axis=1) | |
| concat_df = pd.concat([home_td, away_td], axis=0 ,ignore_index=True) | |
| df_prev = concat_df.mean(axis=0).to_frame().T | |
| X_scaler_prev = self.scaler.transform(df_prev) | |
| X_scaler_prev = pd.DataFrame(X_scaler_prev) | |
| y_xgb_pred = self.xgb_model.predict(X_scaler_prev) | |
| y_pred_xgb = np.round(y_xgb_pred).clip(min=0) | |
| y_pred_poisson = self.poisson_model.predict(X_scaler_prev) | |
| y_pred_poisson = np.round(y_pred_poisson).clip(min=0) | |
| return y_pred_xgb, y_pred_poisson | |
| f = FootballPredictor | |
| def load_all_data(league): | |
| #第一次动态加载赛季队伍 | |
| data_path = league_dict[league] | |
| model_load = joblib.load(data_path) | |
| league_team_name_dict = model_load['league_team_name_dict'] # 赛季:队列列表 | |
| league_team_name_list = league_team_name_dict[league]#队列列表 | |
| if league_team_name_list: | |
| team_drop_update = gr.update(choices=league_team_name_list, value=league_team_name_list[0]) | |
| else: | |
| team_drop_update = gr.update(choices=["无有效队伍数据"], value="无有效队伍数据") | |
| #队列列表*2,data——path,字典 | |
| return team_drop_update, team_drop_update,data_path,league_team_name_dict | |
| def predict_wrapper(home_team,away_team,model_path): | |
| f = FootballPredictor(model_path) | |
| y_pred_xgb, y_pred_poisson = f.predict_goals(home_team,away_team) | |
| return y_pred_xgb, y_pred_poisson | |
| with gr.Blocks(title="七哥看球模拟器") as demo: | |
| with gr.Row(): | |
| league_drop = gr.Dropdown( | |
| label="选择赛区", | |
| choices=league_choices, | |
| value=league_choices[0], | |
| scale=2 | |
| ) | |
| # 主/客队下拉框(初始提示语统一,保持界面一致性) | |
| home_team_drop = gr.Dropdown(label="选择主队", choices=["请先选择赛区"], value="请先选择赛区") | |
| away_team_drop = gr.Dropdown(label="选择客队", choices=["请先选择赛区"], value="请先选择赛区") | |
| league_pkl_path_state = gr.State("") | |
| team_dict_data_state = gr.State({}) | |
| league_drop.change( | |
| fn=load_all_data, | |
| inputs=league_drop, | |
| outputs=[home_team_drop, away_team_drop,league_pkl_path_state, team_dict_data_state] | |
| ) | |
| output1 = gr.Textbox(label="泊松回归模型结果", placeholder="模型预测结果将显示在这里...") | |
| output2 = gr.Textbox(label="XGBoost模型结果", placeholder="模型预测结果将显示在这里...") | |
| predict_btn = gr.Button("点击执行双模型预测", variant="primary", scale=1) | |
| predict_btn.click( | |
| fn=predict_wrapper, # 调用包装函数,封装类实例化和预测 | |
| inputs=[ | |
| home_team_drop, # 入参1:主队名 | |
| away_team_drop, # 入参2:客队名 | |
| league_pkl_path_state # 入参3:State暂存的pkl路径(传给类__init__) | |
| ], | |
| outputs=[output1,output2] | |
| ) | |
| if __name__ == '__main__': | |
| demo.launch(share=True) |