import os import gradio as gr import xgboost as xgb import numpy as np import pandas as pd from huggingface_hub import hf_hub_download # **📌 先运行 `preprocess.py` 进行数据处理** if not os.path.exists("processed_data.csv"): print("📌 运行 `preprocess.py` 进行数据处理...") os.system("python preprocess.py") # **📌 加载处理后的数据** processed_data_path = "processed_data.csv" if not os.path.exists(processed_data_path): raise FileNotFoundError("❌ `processed_data.csv` 未找到,请先运行 `preprocess.py` 处理数据!") df = pd.read_csv(processed_data_path) # **📌 加载 XGBoost 预测模型** model_path = hf_hub_download(repo_id="YDluffy/lottery_prediction", filename="lottery_xgboost_model.json") model = xgb.XGBRegressor() model.load_model(model_path) # **📌 预测函数** def predict_lottery(year, period, num1, num2, num3, num4, num5, num6, special): # 从历史数据查找对应的月份和日期 history = df[(df['期号_年份'] == year) & (df['期数'] == period)] if not history.empty: month = history.iloc[0]['月份'] day = history.iloc[0]['日期'] else: month, day = 1, 1 # 默认值 # 计算中奖号码均值 avg_number = np.mean([num1, num2, num3, num4, num5, num6]) # 形成特征数组 features = np.array([[year, period, month, day, num1, num2, num3, num4, num5, num6, special, avg_number]]) # 进行预测 prediction = model.predict(features) return prediction # **📌 Gradio Web 界面** iface = gr.Interface( fn=predict_lottery, inputs=[ gr.Number(label="年份"), gr.Number(label="期数"), gr.Number(label="号码1"), gr.Number(label="号码2"), gr.Number(label="号码3"), gr.Number(label="号码4"), gr.Number(label="号码5"), gr.Number(label="号码6"), gr.Number(label="特别号码") ], outputs="text", title="六合彩预测模型", description="输入期号和历史开奖号码,预测指定期开奖的号码" ) # **📌 启动 Gradio 应用** iface.launch(share=True)