YDluffy commited on
Commit
6200f2f
·
verified ·
1 Parent(s): 484cdf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -6,7 +6,7 @@ import pandas as pd
6
  from transformers import pipeline
7
  from huggingface_hub import hf_hub_download
8
 
9
- # **📌 先运行 `preprocess.py`,确保数据可用**
10
  processed_data_path = "processed_data.csv"
11
 
12
  if not os.path.exists(processed_data_path):
@@ -28,11 +28,15 @@ model.load_model(model_path)
28
 
29
  # **📌 LLM 解析用户输入**
30
  def chat_with_llm(user_input):
31
- prompt = f"请从以下问题中提取出预测所需的参数(年份、期号、中奖号码等):'{user_input}'"
32
 
33
  try:
34
- response = generator(prompt, max_length=100, num_return_sequences=1)
35
  extracted_text = response[0]['generated_text']
 
 
 
 
36
  return extracted_text
37
  except Exception as e:
38
  return f"❌ GPT-Neo 处理错误: {str(e)}"
@@ -55,16 +59,26 @@ def predict_lottery(year, period, num1, num2, num3, num4, num5, num6, special):
55
 
56
  # **进行预测**
57
  prediction = model.predict(features)
58
- return prediction
 
 
 
 
59
 
60
  # **📌 结合 LLM 和 XGBoost**
61
  def predict_and_interact(user_input):
62
  llm_output = chat_with_llm(user_input)
63
 
64
- # **默认解析出的预测参数**
65
- year, period = 2025, 16
66
- nums = [5, 12, 23, 34, 45, 56]
67
- special = 7
 
 
 
 
 
 
68
 
69
  # **进行预测**
70
  prediction = predict_lottery(year, period, *nums, special)
 
6
  from transformers import pipeline
7
  from huggingface_hub import hf_hub_download
8
 
9
+ # **📌 先运行 `preprocess.py` 处理数据**
10
  processed_data_path = "processed_data.csv"
11
 
12
  if not os.path.exists(processed_data_path):
 
28
 
29
  # **📌 LLM 解析用户输入**
30
  def chat_with_llm(user_input):
31
+ prompt = f"请仅返回预测所需的参数(年份、期号、中奖号码),格式为:'年份:2025, 期号:16, 号码:[5,12,23,34,45,56], 特别号码:7'。输入问题: {user_input}"
32
 
33
  try:
34
+ response = generator(prompt, max_length=50, num_return_sequences=1)
35
  extracted_text = response[0]['generated_text']
36
+
37
+ # **🚀 确保 UTF-8 编码,移除特殊字符**
38
+ extracted_text = extracted_text.encode("utf-8", "ignore").decode("utf-8").strip()
39
+
40
  return extracted_text
41
  except Exception as e:
42
  return f"❌ GPT-Neo 处理错误: {str(e)}"
 
59
 
60
  # **进行预测**
61
  prediction = model.predict(features)
62
+
63
+ # **🚀 修正浮点数问题:四舍五入为整数**
64
+ prediction = np.round(prediction).astype(int)
65
+
66
+ return prediction.tolist()
67
 
68
  # **📌 结合 LLM 和 XGBoost**
69
  def predict_and_interact(user_input):
70
  llm_output = chat_with_llm(user_input)
71
 
72
+ try:
73
+ # **解析 LLM 输出格式:"年份:2025, 期号:16, 号码:[5,12,23,34,45,56], 特别号码:7"**
74
+ parts = llm_output.split(",")
75
+ year = int(parts[0].split(":")[1])
76
+ period = int(parts[1].split(":")[1])
77
+ nums = [int(x) for x in parts[2].split(":")[1].strip("[]").split()]
78
+ special = int(parts[3].split(":")[1])
79
+
80
+ except Exception as e:
81
+ return f"❌ LLM 解析数据失败: {str(e)}\n\n📢 原始 LLM 解析结果: {llm_output}"
82
 
83
  # **进行预测**
84
  prediction = predict_lottery(year, period, *nums, special)