File size: 2,525 Bytes
9f075a6 df0843b 4b0bb56 2db7de7 8898ec4 48b6405 9f075a6 48b6405 9f075a6 df0843b 9f075a6 4b0bb56 df0843b 48b6405 9f075a6 2db7de7 9f075a6 2db7de7 df0843b 9f075a6 2db7de7 48b6405 70f514e 48b6405 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import os
import gradio as gr
import xgboost as xgb
import numpy as np
import pandas as pd
import requests
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)
# **📌 调用 LLM API 解析用户输入**
LLM_API_URL = "https://your-llm-space.gradio.app" # 替换为 LLM 服务器地址
def get_prediction_from_llm(user_input):
# 调用 LLM 提取预测所需的特征
response = requests.post(LLM_API_URL + "/api/predict", json={"input": user_input})
llm_output = response.json().get("prediction", "")
# 解析 LLM 输出(示例解析)
year, period = 2025, 16
nums = [5, 12, 23, 34, 45, 56]
special = 7
# **📌 预测**
prediction = predict_lottery(year, period, *nums, special)
return f"预测的号码是: {prediction}\n\n模型解析的特征:{llm_output}"
# **📌 预测函数**
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=get_prediction_from_llm,
inputs=gr.Textbox(label="请输入问题或期号信息"),
outputs="text",
title="六合彩预测模型",
description="通过与 LLM 交互,解析输入信息并进行预测"
)
# **📌 启动 Gradio 应用**
iface.launch(share=True)
|