Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -320,47 +320,61 @@ class MultiAuthManager:
|
|
| 320 |
last_index = self.model_usage[model]
|
| 321 |
auth_manager = self.auth_managers[last_index]
|
| 322 |
if auth_manager.is_model_available(model) and auth_manager.ensure_valid_token():
|
|
|
|
| 323 |
return auth_manager
|
| 324 |
|
| 325 |
-
#
|
| 326 |
-
|
| 327 |
-
tried_count = 0
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
-
|
| 337 |
-
tried_count += 1
|
| 338 |
|
| 339 |
# 如果是新的一天,重置所有状态
|
| 340 |
if datetime.now().date() > self.last_success_date:
|
|
|
|
| 341 |
self.reset_all_model_status()
|
| 342 |
self.last_success_date = datetime.now().date()
|
| 343 |
-
self.model_usage.clear()
|
| 344 |
|
| 345 |
# 重新尝试第一个账号
|
| 346 |
auth_manager = self.auth_managers[0]
|
| 347 |
if auth_manager.ensure_valid_token():
|
| 348 |
self.current_index = 0
|
| 349 |
self.model_usage[model] = 0
|
|
|
|
| 350 |
return auth_manager
|
| 351 |
-
|
|
|
|
| 352 |
return None
|
| 353 |
|
| 354 |
def update_model_success(self, model, index):
|
| 355 |
"""更新模型成功使用的账号索引"""
|
|
|
|
| 356 |
self.model_usage[model] = index
|
| 357 |
self.last_success_date = datetime.now().date()
|
|
|
|
|
|
|
| 358 |
|
| 359 |
def reset_all_model_status(self):
|
| 360 |
"""重置所有账号的状态和模型使用记录"""
|
| 361 |
for auth_manager in self.auth_managers:
|
| 362 |
auth_manager.reset_model_status()
|
| 363 |
self.model_usage.clear()
|
|
|
|
| 364 |
|
| 365 |
def require_auth(func: Callable) -> Callable:
|
| 366 |
"""装饰器,确保在调用API之前有有效的token。"""
|
|
@@ -810,7 +824,7 @@ def make_request(payload, auth_manager, model_id):
|
|
| 810 |
else:
|
| 811 |
raise Exception("无法注册新账号")
|
| 812 |
|
| 813 |
-
#
|
| 814 |
tried_accounts = set()
|
| 815 |
|
| 816 |
while len(tried_accounts) < len(multi_auth_manager.auth_managers):
|
|
|
|
| 320 |
last_index = self.model_usage[model]
|
| 321 |
auth_manager = self.auth_managers[last_index]
|
| 322 |
if auth_manager.is_model_available(model) and auth_manager.ensure_valid_token():
|
| 323 |
+
logger.info(f"Using last successful account for model {model}: {auth_manager._email}")
|
| 324 |
return auth_manager
|
| 325 |
|
| 326 |
+
# 如果该模型没有成功记录或上次的账号不可用,从头开始尝试所有账号
|
| 327 |
+
tried_accounts = set()
|
|
|
|
| 328 |
|
| 329 |
+
# 从当前位置开始尝试
|
| 330 |
+
current = self.current_index
|
| 331 |
+
while len(tried_accounts) < len(self.auth_managers):
|
| 332 |
+
auth_manager = self.auth_managers[current]
|
| 333 |
+
|
| 334 |
+
if auth_manager._email not in tried_accounts:
|
| 335 |
+
tried_accounts.add(auth_manager._email)
|
| 336 |
+
|
| 337 |
+
if auth_manager.is_model_available(model) and auth_manager.ensure_valid_token():
|
| 338 |
+
# 更新该模型的成功账号记录
|
| 339 |
+
self.model_usage[model] = current
|
| 340 |
+
self.current_index = current
|
| 341 |
+
logger.info(f"Found available account for model {model}: {auth_manager._email}")
|
| 342 |
+
return auth_manager
|
| 343 |
|
| 344 |
+
current = (current + 1) % len(self.auth_managers)
|
|
|
|
| 345 |
|
| 346 |
# 如果是新的一天,重置所有状态
|
| 347 |
if datetime.now().date() > self.last_success_date:
|
| 348 |
+
logger.info("New day started, resetting all model status")
|
| 349 |
self.reset_all_model_status()
|
| 350 |
self.last_success_date = datetime.now().date()
|
| 351 |
+
self.model_usage.clear()
|
| 352 |
|
| 353 |
# 重新尝试第一个账号
|
| 354 |
auth_manager = self.auth_managers[0]
|
| 355 |
if auth_manager.ensure_valid_token():
|
| 356 |
self.current_index = 0
|
| 357 |
self.model_usage[model] = 0
|
| 358 |
+
logger.info(f"Using first account after reset: {auth_manager._email}")
|
| 359 |
return auth_manager
|
| 360 |
+
|
| 361 |
+
logger.warning(f"No available accounts found for model {model}")
|
| 362 |
return None
|
| 363 |
|
| 364 |
def update_model_success(self, model, index):
|
| 365 |
"""更新模型成功使用的账号索引"""
|
| 366 |
+
old_index = self.model_usage.get(model, None)
|
| 367 |
self.model_usage[model] = index
|
| 368 |
self.last_success_date = datetime.now().date()
|
| 369 |
+
if old_index != index:
|
| 370 |
+
logger.info(f"Updated successful account for model {model}: {self.auth_managers[index]._email}")
|
| 371 |
|
| 372 |
def reset_all_model_status(self):
|
| 373 |
"""重置所有账号的状态和模型使用记录"""
|
| 374 |
for auth_manager in self.auth_managers:
|
| 375 |
auth_manager.reset_model_status()
|
| 376 |
self.model_usage.clear()
|
| 377 |
+
logger.info("Reset all model status and usage records")
|
| 378 |
|
| 379 |
def require_auth(func: Callable) -> Callable:
|
| 380 |
"""装饰器,确保在调用API之前有有效的token。"""
|
|
|
|
| 824 |
else:
|
| 825 |
raise Exception("无法注册新账号")
|
| 826 |
|
| 827 |
+
# 记录���尝试的账号
|
| 828 |
tried_accounts = set()
|
| 829 |
|
| 830 |
while len(tried_accounts) < len(multi_auth_manager.auth_managers):
|