ffzeroHua commited on
Commit
7aaa3cd
·
verified ·
1 Parent(s): 40c0e3b

Update start.sh

Browse files
Files changed (1) hide show
  1. start.sh +50 -12
start.sh CHANGED
@@ -47,8 +47,11 @@ app = Flask('kitakaze')
47
  SELF = '3823042923'
48
  API_BASE = 'http://127.0.0.1:7667'
49
 
50
- HF_SPACE_URL = "ffzeroHua/Jiantao"
51
- HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
52
 
53
  try:
54
  print("正在初始化 Hugging Face 云端连接...")
@@ -75,24 +78,59 @@ def Send(msg, uid, gid=None):
75
 
76
  def ContainsRoundInfo(s): return '局' in s or '本场' in s
77
 
 
 
 
 
 
 
 
78
  def start_analyze(mid, uid, gid, msg, hanchan=False):
79
  def analyze_task():
80
  try:
81
- response_str = hf_client.predict(msg, hanchan)
82
- result_data = json.loads(response_str)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  if "error" in result_data:
84
- Send(Reply(mid) + f"云端检讨失败,可能牌谱链接有误\n({result_data['error'][:50]}...)", uid, gid)
 
85
  return
86
- os.makedirs('reviewreports', exist_ok=True)
87
- ID = hex(int(time.time()))
88
- filename = f"reviewreports/{ID}.json"
89
- with open(filename, 'w', encoding='utf-8') as f:
90
- json.dump(result_data.get("logs", []), f, ensure_ascii=False)
91
  overall_rating = result_data.get("overall_rating", 0.0)
92
- final_msg = f'看完啦!\nhttps://online.4z.autos/?id={ID}\n总体评分 {overall_rating}'
 
93
  Send(Reply(mid) + final_msg, uid, gid)
 
 
 
 
94
  except Exception as e:
95
- Send(Reply(mid) + '有点不懂,改日再看\n(本地调度或网络通信出错)', uid, gid)
 
 
 
96
  thread = threading.Thread(target=analyze_task)
97
  thread.daemon = True
98
  thread.start()
 
47
  SELF = '3823042923'
48
  API_BASE = 'http://127.0.0.1:7667'
49
 
50
+ HF_SPACE_URL = "https://hf.4z.autos/"
51
+ HF_TOKEN = os.getenv('HF_TOKEN')
52
+
53
+ # 如果你的 Hugging Face 仓库是 Private 的,必须在请求头里带上 Token 才能访问 API
54
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
55
 
56
  try:
57
  print("正在初始化 Hugging Face 云端连接...")
 
78
 
79
  def ContainsRoundInfo(s): return '局' in s or '本场' in s
80
 
81
+ def extract_paipu_id(paipu_url_or_msg):
82
+ """在本地计算牌谱 ID,确保生成的链接与云端保存的文件名完全一致"""
83
+ match = re.search(r'log=([\w-]+)', paipu_url_or_msg)
84
+ if match:
85
+ return match.group(1)
86
+ return hashlib.md5(paipu_url_or_msg.encode('utf-8')).hexdigest()
87
+
88
  def start_analyze(mid, uid, gid, msg, hanchan=False):
89
  def analyze_task():
90
  try:
91
+ # 1. 提取或生成本次请求对应的 ID
92
+ paipu_id = extract_paipu_id(msg)
93
+
94
+ # 2. 准备 HTTP 请求,目标是我们刚才用 FastAPI 写好的兼容接口
95
+ # 注意:如果 URL 结尾有斜杠则去掉,防止拼出双斜杠
96
+ api_endpoint = HF_SPACE_URL.rstrip('/') + "/api/predict"
97
+ payload = {"data": [msg, hanchan]}
98
+
99
+ # 3. 发送纯正的 POST 请求(设置超时时间,以防云端检讨太久假死)
100
+ response = requests.post(
101
+ api_endpoint,
102
+ json=payload,
103
+ headers=HEADERS,
104
+ timeout=180
105
+ )
106
+ response.raise_for_status() # 如果是 401/404/500 等 HTTP 错误会直接跳入 except
107
+
108
+ # 4. 解析后端返回的 JSON 数据
109
+ # 后端返回格式是 {"data": ["{真正的结果JSON字符串}"]}
110
+ response_json = response.json()
111
+ result_str = response_json.get("data", ["{}"])[0]
112
+ result_data = json.loads(result_str)
113
+
114
+ # 5. 业务错误处理
115
  if "error" in result_data:
116
+ error_msg = str(result_data['error'])[:50]
117
+ Send(Reply(mid) + f"云端检讨失败,可能牌谱链接有误\n({error_msg}...)", uid, gid)
118
  return
119
+
120
+ # 6. 构造最终要发送给用户的消息(不再需要把 result_data 写入本地磁盘)
 
 
 
121
  overall_rating = result_data.get("overall_rating", 0.0)
122
+ final_msg = f'看完啦!\nhttps://online.4z.autos/?id={paipu_id}\n总体评分 {overall_rating}'
123
+
124
  Send(Reply(mid) + final_msg, uid, gid)
125
+
126
+ except requests.exceptions.RequestException as e:
127
+ print(f"HTTP 通信出错: {e}")
128
+ Send(Reply(mid) + '有点不懂,改日再看\n(云端连接断开或超时)', uid, gid)
129
  except Exception as e:
130
+ print(f"解析或调度出错: {e}")
131
+ Send(Reply(mid) + '有点不懂,改日再看\n(客户端处理异常)', uid, gid)
132
+
133
+ # 启动后台线程
134
  thread = threading.Thread(target=analyze_task)
135
  thread.daemon = True
136
  thread.start()