import gradio as gr import xgboost as xgb import numpy as np import pandas as pd from huggingface_hub import hf_hub_download # **📥 从 Hugging Face 下载 XGBoost 模型** repo_id = "YDluffy/lottery_prediction" model_filename = "lottery_xgboost_model.ubj" model_path = hf_hub_download(repo_id=repo_id, filename=model_filename) # **✅ 加载 XGBoost 预测模型** model = xgb.Booster() model.load_model(model_path) # **📥 读取历史开奖记录** history_data_path = "cleaned_mark_six.csv" history_data = pd.read_csv(history_data_path) # **📌 预测函数** def predict_lottery(year, period): # **📌 1. 查找往年相同期数的开奖记录** historical_matches = history_data[history_data["期数"] == period] # **📌 2. 计算历史趋势(如高频号码、奇偶比例)** most_frequent_numbers = historical_matches.iloc[:, -7:].mode().iloc[0].tolist() # **📌 3. 生成 XGBoost 预测输入** test_features = np.array([[year, period] + most_frequent_numbers]) dtest = xgb.DMatrix(test_features) # **📌 4. 进行预测** prediction = model.predict(dtest) final_prediction = np.round(prediction).astype(int).tolist() return final_prediction # **📌 创建 API 接口** iface = gr.Interface( fn=predict_lottery, inputs=["number", "number"], outputs="text", title="六合彩智能预测 API", description="输入年份和期数,自动分析历史开奖记录,预测开奖号码" ) iface.launch(share=True)