Spaces:
Sleeping
Sleeping
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) | |