dan92 commited on
Commit
e2562c8
·
verified ·
1 Parent(s): 2aaf054

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -29
app.py CHANGED
@@ -312,46 +312,35 @@ class MultiAuthManager:
312
  self.last_successful_index = 0 # 记录上次成功的账号索引
313
  self.last_success_date = datetime.now().date() # 记录上次成功的日期
314
 
315
- def get_next_auth_manager(self, model):
316
- """改进的账号选择逻辑,优先使用上次成功的账号"""
317
- current_date = datetime.now().date()
318
-
319
- # 如果是新的一天,重置状态并从第一个账号开始
320
- if current_date > self.last_success_date:
321
- self.current_index = 0
322
- self.last_successful_index = 0
323
- self.last_success_date = current_date
324
- self.reset_all_model_status()
325
- return self.auth_managers[0] if self.auth_managers else None
326
-
327
- # 优先使用上次成功的账号
328
  auth_manager = self.auth_managers[self.last_successful_index]
329
- if auth_manager.is_model_available(model) and auth_manager._should_attempt_auth():
330
  return auth_manager
331
 
332
- # 如果上次成功的账号不可用,才开始轮询其他账号
333
  start_index = (self.last_successful_index + 1) % len(self.auth_managers)
334
  current = start_index
335
 
336
  while current != self.last_successful_index:
337
  auth_manager = self.auth_managers[current]
338
- if auth_manager.is_model_available(model) and auth_manager._should_attempt_auth():
339
- self.last_successful_index = current
 
340
  return auth_manager
341
  current = (current + 1) % len(self.auth_managers)
342
 
343
- return None
344
-
345
- def update_last_successful(self, index):
346
- """更新最后一次成功使用的账号索引"""
347
- self.last_successful_index = index
348
- self.last_success_date = datetime.now().date()
349
-
350
- def ensure_valid_token(self, model):
351
- for _ in range(len(self.auth_managers)):
352
- auth_manager = self.get_next_auth_manager(model)
353
- if auth_manager and auth_manager.ensure_valid_token():
354
  return auth_manager
 
355
  return None
356
 
357
  def reset_all_model_status(self):
@@ -748,7 +737,7 @@ def handle_request():
748
  }), 500
749
 
750
  def build_payload(request_data, model_id):
751
- """构建请求有���负载,确保保持完���的上下文。"""
752
  messages = request_data.get('messages', [])
753
 
754
  # 检查是否已经存在系统消息
 
312
  self.last_successful_index = 0 # 记录上次成功的账号索引
313
  self.last_success_date = datetime.now().date() # 记录上次成功的日期
314
 
315
+ def ensure_valid_token(self, model):
316
+ """改进的token验证方法,优先使用上次成功的账号"""
317
+ # 首先尝试使用上次成功的账号
 
 
 
 
 
 
 
 
 
 
318
  auth_manager = self.auth_managers[self.last_successful_index]
319
+ if auth_manager.is_model_available(model) and auth_manager.ensure_valid_token():
320
  return auth_manager
321
 
322
+ # 如果上次成功的账号不可用,才尝试其他账号
323
  start_index = (self.last_successful_index + 1) % len(self.auth_managers)
324
  current = start_index
325
 
326
  while current != self.last_successful_index:
327
  auth_manager = self.auth_managers[current]
328
+ if auth_manager.is_model_available(model) and auth_manager.ensure_valid_token():
329
+ self.last_successful_index = current # 更新最后成功的账号索引
330
+ self.last_success_date = datetime.now().date()
331
  return auth_manager
332
  current = (current + 1) % len(self.auth_managers)
333
 
334
+ # 如果所有账号都不可用,检查是否是新的一天
335
+ if datetime.now().date() > self.last_success_date:
336
+ self.reset_all_model_status()
337
+ self.last_success_date = datetime.now().date()
338
+ # 重新尝试第一个账号
339
+ auth_manager = self.auth_managers[0]
340
+ if auth_manager.ensure_valid_token():
341
+ self.last_successful_index = 0
 
 
 
342
  return auth_manager
343
+
344
  return None
345
 
346
  def reset_all_model_status(self):
 
737
  }), 500
738
 
739
  def build_payload(request_data, model_id):
740
+ """构建请求有负载,确保保持完的上下文。"""
741
  messages = request_data.get('messages', [])
742
 
743
  # 检查是否已经存在系统消息