File size: 2,102 Bytes
9f075a6
df0843b
4b0bb56
2db7de7
8898ec4
9f075a6
 
 
 
 
 
 
 
 
 
 
 
 
df0843b
9f075a6
4b0bb56
 
 
df0843b
9f075a6
2db7de7
9f075a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db7de7
 
df0843b
9f075a6
2db7de7
9f075a6
70f514e
 
 
 
 
 
 
 
 
 
 
 
 
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
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)