Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
@@ -668,11 +668,10 @@ def make_request(payload, auth_manager, model_id):
|
|
668 |
global multi_auth_manager
|
669 |
max_retries = 3
|
670 |
retry_delay = 1
|
671 |
-
accounts_exhausted = False
|
672 |
|
673 |
logger.info(f"尝试发送请求,模型:{model_id}")
|
674 |
|
675 |
-
#
|
676 |
if not multi_auth_manager:
|
677 |
logger.error("MultiAuthManager 不存在,尝试重新初始化")
|
678 |
credentials = get_auth_credentials()
|
@@ -685,12 +684,32 @@ def make_request(payload, auth_manager, model_id):
|
|
685 |
else:
|
686 |
raise Exception("无法注册新账号")
|
687 |
|
688 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
689 |
auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
|
690 |
if not auth_manager:
|
691 |
logger.error(f"No available accounts for model {model_id}")
|
692 |
-
|
693 |
-
|
|
|
|
|
694 |
|
695 |
for attempt in range(max_retries):
|
696 |
try:
|
@@ -710,14 +729,18 @@ def make_request(payload, auth_manager, model_id):
|
|
710 |
headers_cache.clear()
|
711 |
|
712 |
if response.status_code == 401: # Unauthorized
|
713 |
-
logger.info(f"Token expired for account {auth_manager._email}, attempting refresh
|
714 |
if auth_manager.ensure_valid_token():
|
715 |
continue
|
716 |
|
717 |
-
if response.status_code == 403: # Forbidden,
|
718 |
logger.warning(f"Model {model_id} usage limit reached for account {auth_manager._email}")
|
719 |
-
|
720 |
-
|
|
|
|
|
|
|
|
|
721 |
|
722 |
logger.error(f"Request failed with status {response.status_code} for account {auth_manager._email}")
|
723 |
|
@@ -727,32 +750,10 @@ def make_request(payload, auth_manager, model_id):
|
|
727 |
time.sleep(retry_delay)
|
728 |
continue
|
729 |
|
730 |
-
#
|
731 |
continue
|
732 |
|
733 |
-
|
734 |
-
if accounts_exhausted:
|
735 |
-
logger.info("所有账号已耗尽,强制尝试注册新账号")
|
736 |
-
try:
|
737 |
-
# 直接调用注册方法
|
738 |
-
successful_accounts = register_bot.register_and_verify(5) # 注册5个账号
|
739 |
-
|
740 |
-
if successful_accounts:
|
741 |
-
logger.info(f"成功注册 {len(successful_accounts)} 个新账号")
|
742 |
-
# 重新初始化 multi_auth_manager
|
743 |
-
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
744 |
-
multi_auth_manager = MultiAuthManager(credentials)
|
745 |
-
|
746 |
-
# 重新尝试请求
|
747 |
-
return make_request(payload, None, model_id)
|
748 |
-
else:
|
749 |
-
logger.error("无法自动注册新账号")
|
750 |
-
raise Exception("Failed to register new accounts")
|
751 |
-
except Exception as e:
|
752 |
-
logger.error(f"自动注册过程发生错误: {e}")
|
753 |
-
raise Exception("Failed to make request after trying all accounts and registration")
|
754 |
-
|
755 |
-
raise Exception("Failed to make request after trying all accounts")
|
756 |
|
757 |
def health_check():
|
758 |
"""定期检查认证状态和重置模型使用状态"""
|
|
|
668 |
global multi_auth_manager
|
669 |
max_retries = 3
|
670 |
retry_delay = 1
|
|
|
671 |
|
672 |
logger.info(f"尝试发送请求,模型:{model_id}")
|
673 |
|
674 |
+
# 确保 multi_auth_manager 存在
|
675 |
if not multi_auth_manager:
|
676 |
logger.error("MultiAuthManager 不存在,尝试重新初始化")
|
677 |
credentials = get_auth_credentials()
|
|
|
684 |
else:
|
685 |
raise Exception("无法注册新账号")
|
686 |
|
687 |
+
def trigger_registration():
|
688 |
+
"""内部函数,用于触发账号注册"""
|
689 |
+
logger.info("触发新账号注册流程")
|
690 |
+
try:
|
691 |
+
successful_accounts = register_bot.register_and_verify(5)
|
692 |
+
if successful_accounts:
|
693 |
+
logger.info(f"成功注册 {len(successful_accounts)} 个新账号")
|
694 |
+
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
695 |
+
global multi_auth_manager
|
696 |
+
multi_auth_manager = MultiAuthManager(credentials)
|
697 |
+
return True
|
698 |
+
else:
|
699 |
+
logger.error("无法自动注册新账号")
|
700 |
+
return False
|
701 |
+
except Exception as e:
|
702 |
+
logger.error(f"注册过程发生错误: {e}")
|
703 |
+
return False
|
704 |
+
|
705 |
+
for _ in range(len(multi_auth_manager.auth_managers)):
|
706 |
auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
|
707 |
if not auth_manager:
|
708 |
logger.error(f"No available accounts for model {model_id}")
|
709 |
+
# 立即触发注册
|
710 |
+
if not trigger_registration():
|
711 |
+
raise Exception("无法注册新账号")
|
712 |
+
continue
|
713 |
|
714 |
for attempt in range(max_retries):
|
715 |
try:
|
|
|
729 |
headers_cache.clear()
|
730 |
|
731 |
if response.status_code == 401: # Unauthorized
|
732 |
+
logger.info(f"Token expired for account {auth_manager._email}, attempting refresh")
|
733 |
if auth_manager.ensure_valid_token():
|
734 |
continue
|
735 |
|
736 |
+
if response.status_code == 403: # Forbidden, 模型使用限制
|
737 |
logger.warning(f"Model {model_id} usage limit reached for account {auth_manager._email}")
|
738 |
+
# 立即触发注册
|
739 |
+
if trigger_registration():
|
740 |
+
# 重试请求
|
741 |
+
return make_request(payload, None, model_id)
|
742 |
+
else:
|
743 |
+
raise Exception("注册新账号失败")
|
744 |
|
745 |
logger.error(f"Request failed with status {response.status_code} for account {auth_manager._email}")
|
746 |
|
|
|
750 |
time.sleep(retry_delay)
|
751 |
continue
|
752 |
|
753 |
+
# 如果当前账号重试失败,尝试下一个账号
|
754 |
continue
|
755 |
|
756 |
+
raise Exception("所有账号和注册尝试均失败")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
757 |
|
758 |
def health_check():
|
759 |
"""定期检查认证状态和重置模型使用状态"""
|