Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -107,7 +107,7 @@ def create_custom_session():
|
|
| 107 |
return session
|
| 108 |
|
| 109 |
# 添加速率限制相关的常量
|
| 110 |
-
AUTH_RETRY_DELAY = 60 #
|
| 111 |
AUTH_BACKOFF_FACTOR = 2 # 退避因子
|
| 112 |
AUTH_MAX_RETRIES = 3 # 最大重试次数
|
| 113 |
AUTH_CHECK_INTERVAL = 300 # 健康检查间隔(秒)
|
|
@@ -450,7 +450,7 @@ def create_openai_chunk(content, model, finish_reason=None, usage=None):
|
|
| 450 |
"delta": {"content": content} if content else {},
|
| 451 |
"logprobs": None,
|
| 452 |
"finish_reason": finish_reason,
|
| 453 |
-
#
|
| 454 |
"context_preserved": True
|
| 455 |
}
|
| 456 |
]
|
|
@@ -847,12 +847,24 @@ def make_request(payload, auth_manager, model_id):
|
|
| 847 |
# 记录已尝试的账号
|
| 848 |
tried_accounts = set()
|
| 849 |
|
| 850 |
-
while
|
| 851 |
auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
|
| 852 |
if not auth_manager:
|
| 853 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
|
| 855 |
-
#
|
| 856 |
if auth_manager._email in tried_accounts:
|
| 857 |
continue
|
| 858 |
|
|
@@ -863,49 +875,52 @@ def make_request(payload, auth_manager, model_id):
|
|
| 863 |
try:
|
| 864 |
url = get_notdiamond_url()
|
| 865 |
headers = get_notdiamond_headers(auth_manager)
|
| 866 |
-
response = executor.submit(
|
| 867 |
-
requests.post,
|
| 868 |
-
url,
|
| 869 |
-
headers=headers,
|
| 870 |
-
json=payload,
|
| 871 |
-
stream=True
|
| 872 |
-
).result()
|
| 873 |
|
| 874 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 875 |
logger.info(f"请求成功,使用账号 {auth_manager._email}")
|
| 876 |
return response
|
| 877 |
|
| 878 |
-
|
| 879 |
-
|
| 880 |
if response.status_code == 401: # Unauthorized
|
| 881 |
-
logger.
|
| 882 |
if auth_manager.ensure_valid_token():
|
| 883 |
continue
|
|
|
|
| 884 |
|
| 885 |
-
if response.status_code == 403: # Forbidden
|
| 886 |
logger.warning(f"Model {model_id} usage limit reached for account {auth_manager._email}")
|
| 887 |
auth_manager.set_model_unavailable(model_id)
|
| 888 |
-
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 889 |
|
| 890 |
-
logger.error(f"Request failed with status {response.status_code}
|
| 891 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 892 |
except Exception as e:
|
| 893 |
-
logger.error(f"
|
| 894 |
if attempt < max_retries - 1:
|
| 895 |
-
time.sleep(retry_delay)
|
| 896 |
continue
|
| 897 |
-
|
| 898 |
-
# 所有账号都尝试过且失败后,才进行注册
|
| 899 |
-
if len(tried_accounts) == len(multi_auth_manager.auth_managers):
|
| 900 |
-
logger.info("所有现有账号都已尝试,开始注册新账号")
|
| 901 |
-
successful_accounts = register_bot.register_and_verify(5)
|
| 902 |
-
if successful_accounts:
|
| 903 |
-
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
| 904 |
-
multi_auth_manager = MultiAuthManager(credentials)
|
| 905 |
-
# 使用新注册的账号重试请求
|
| 906 |
-
return make_request(payload, None, model_id)
|
| 907 |
|
| 908 |
-
raise Exception("
|
| 909 |
|
| 910 |
# 删除 health_check 函数和相关的线程启动代码
|
| 911 |
if __name__ == "__main__":
|
|
|
|
| 107 |
return session
|
| 108 |
|
| 109 |
# 添加速率限制相关的常量
|
| 110 |
+
AUTH_RETRY_DELAY = 60 # 认证���试延迟(秒)
|
| 111 |
AUTH_BACKOFF_FACTOR = 2 # 退避因子
|
| 112 |
AUTH_MAX_RETRIES = 3 # 最大重试次数
|
| 113 |
AUTH_CHECK_INTERVAL = 300 # 健康检查间隔(秒)
|
|
|
|
| 450 |
"delta": {"content": content} if content else {},
|
| 451 |
"logprobs": None,
|
| 452 |
"finish_reason": finish_reason,
|
| 453 |
+
# 添加上下文关信息
|
| 454 |
"context_preserved": True
|
| 455 |
}
|
| 456 |
]
|
|
|
|
| 847 |
# 记录已尝试的账号
|
| 848 |
tried_accounts = set()
|
| 849 |
|
| 850 |
+
while True: # 持续尝试,直到成功或确定所有账号都不可用
|
| 851 |
auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
|
| 852 |
if not auth_manager:
|
| 853 |
+
# 检查是否所有账号都已尝试
|
| 854 |
+
if len(tried_accounts) >= len(multi_auth_manager.auth_managers):
|
| 855 |
+
logger.error("所有账号都已尝试且不可用")
|
| 856 |
+
# 尝试注册新账号
|
| 857 |
+
successful_accounts = register_bot.register_and_verify(5)
|
| 858 |
+
if successful_accounts:
|
| 859 |
+
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
| 860 |
+
multi_auth_manager = MultiAuthManager(credentials)
|
| 861 |
+
tried_accounts.clear() # 清空已尝试账号列表
|
| 862 |
+
continue # 继续尝试新注册的账号
|
| 863 |
+
else:
|
| 864 |
+
raise Exception("所有账号均不可用,且注册新账号失败")
|
| 865 |
+
continue # 如果还有未尝试的账号,继续循环
|
| 866 |
|
| 867 |
+
# 如果这个账号已经尝试过,跳过
|
| 868 |
if auth_manager._email in tried_accounts:
|
| 869 |
continue
|
| 870 |
|
|
|
|
| 875 |
try:
|
| 876 |
url = get_notdiamond_url()
|
| 877 |
headers = get_notdiamond_headers(auth_manager)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 878 |
|
| 879 |
+
# 使用 session 发送请求
|
| 880 |
+
session = create_custom_session()
|
| 881 |
+
response = session.post(
|
| 882 |
+
url,
|
| 883 |
+
headers=headers,
|
| 884 |
+
json=payload,
|
| 885 |
+
stream=True,
|
| 886 |
+
timeout=30 # 添加超时设置
|
| 887 |
+
)
|
| 888 |
+
|
| 889 |
+
if response.status_code == 200:
|
| 890 |
logger.info(f"请求成功,使用账号 {auth_manager._email}")
|
| 891 |
return response
|
| 892 |
|
| 893 |
+
# 处理不同的错误状态码
|
|
|
|
| 894 |
if response.status_code == 401: # Unauthorized
|
| 895 |
+
logger.warning(f"Token expired for account {auth_manager._email}")
|
| 896 |
if auth_manager.ensure_valid_token():
|
| 897 |
continue
|
| 898 |
+
break
|
| 899 |
|
| 900 |
+
if response.status_code == 403: # Forbidden
|
| 901 |
logger.warning(f"Model {model_id} usage limit reached for account {auth_manager._email}")
|
| 902 |
auth_manager.set_model_unavailable(model_id)
|
| 903 |
+
break
|
| 904 |
+
|
| 905 |
+
if response.status_code >= 500: # Server error
|
| 906 |
+
if attempt < max_retries - 1:
|
| 907 |
+
time.sleep(retry_delay * (attempt + 1))
|
| 908 |
+
continue
|
| 909 |
|
| 910 |
+
logger.error(f"Request failed with status {response.status_code}")
|
| 911 |
|
| 912 |
+
except requests.RequestException as e:
|
| 913 |
+
logger.error(f"Request error: {str(e)}")
|
| 914 |
+
if attempt < max_retries - 1:
|
| 915 |
+
time.sleep(retry_delay * (attempt + 1))
|
| 916 |
+
continue
|
| 917 |
except Exception as e:
|
| 918 |
+
logger.error(f"Unexpected error: {str(e)}")
|
| 919 |
if attempt < max_retries - 1:
|
| 920 |
+
time.sleep(retry_delay * (attempt + 1))
|
| 921 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 922 |
|
| 923 |
+
raise Exception("无法完成请求,所有可用账号都已尝试")
|
| 924 |
|
| 925 |
# 删除 health_check 函数和相关的线程启动代码
|
| 926 |
if __name__ == "__main__":
|