Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -309,42 +309,43 @@ class MultiAuthManager:
|
|
| 309 |
self.current_index = 0
|
| 310 |
self._last_rotation = time.time()
|
| 311 |
self._rotation_interval = 300 # 5分钟轮转间隔
|
| 312 |
-
self.last_successful_index = 0 #
|
| 313 |
-
self.
|
| 314 |
|
| 315 |
def get_next_auth_manager(self, model):
|
| 316 |
-
"""
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
start_index = (self.last_successful_index + 1) % len(self.auth_managers)
|
| 328 |
-
|
| 329 |
|
| 330 |
-
while
|
| 331 |
-
auth_manager = self.auth_managers[
|
| 332 |
if auth_manager.is_model_available(model) and auth_manager._should_attempt_auth():
|
| 333 |
-
self.last_successful_index =
|
| 334 |
return auth_manager
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
if current_index == start_index:
|
| 338 |
-
break
|
| 339 |
-
|
| 340 |
return None
|
| 341 |
|
| 342 |
-
def
|
| 343 |
-
"""
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
self.last_successful_index = i
|
| 347 |
-
break
|
| 348 |
|
| 349 |
def ensure_valid_token(self, model):
|
| 350 |
for _ in range(len(self.auth_managers)):
|
|
@@ -747,7 +748,7 @@ def handle_request():
|
|
| 747 |
}), 500
|
| 748 |
|
| 749 |
def build_payload(request_data, model_id):
|
| 750 |
-
"""
|
| 751 |
messages = request_data.get('messages', [])
|
| 752 |
|
| 753 |
# 检查是否已经存在系统消息
|
|
@@ -821,9 +822,6 @@ def make_request(payload, auth_manager, model_id):
|
|
| 821 |
while len(tried_accounts) < len(multi_auth_manager.auth_managers):
|
| 822 |
auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
|
| 823 |
if not auth_manager:
|
| 824 |
-
if not multi_auth_manager.daily_rotation_triggered:
|
| 825 |
-
logger.info("当前账号不可用,等待至次日重试")
|
| 826 |
-
raise Exception("当前账号不可用,请等待至次日重试")
|
| 827 |
break
|
| 828 |
|
| 829 |
# 如果这个账号已经尝试过,继续下一个
|
|
@@ -847,7 +845,9 @@ def make_request(payload, auth_manager, model_id):
|
|
| 847 |
|
| 848 |
if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
|
| 849 |
logger.info(f"请求成功,使用账号 {auth_manager._email}")
|
| 850 |
-
|
|
|
|
|
|
|
| 851 |
return response
|
| 852 |
|
| 853 |
headers_cache.clear()
|
|
@@ -883,43 +883,49 @@ def make_request(payload, auth_manager, model_id):
|
|
| 883 |
raise Exception("所有账号均不可用,且注册新账号失败")
|
| 884 |
|
| 885 |
def health_check():
|
| 886 |
-
"""
|
| 887 |
check_index = 0
|
| 888 |
-
|
| 889 |
-
last_date = datetime.now().date()
|
| 890 |
|
| 891 |
while True:
|
| 892 |
try:
|
| 893 |
if multi_auth_manager:
|
| 894 |
-
current_time = time.time()
|
| 895 |
current_date = datetime.now().date()
|
| 896 |
|
| 897 |
-
#
|
| 898 |
-
if current_date >
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
logger.info("New day started
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
#
|
| 905 |
-
if
|
| 906 |
auth_manager = multi_auth_manager.auth_managers[check_index]
|
|
|
|
| 907 |
|
| 908 |
if auth_manager._should_attempt_auth():
|
| 909 |
if not auth_manager.ensure_valid_token():
|
| 910 |
-
logger.warning(f"Auth token validation failed during health check for {
|
| 911 |
auth_manager.clear_auth()
|
| 912 |
else:
|
| 913 |
-
logger.info(f"Health check passed for {
|
|
|
|
|
|
|
| 914 |
|
| 915 |
-
#
|
| 916 |
check_index = (check_index + 1) % len(multi_auth_manager.auth_managers)
|
| 917 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 918 |
|
| 919 |
except Exception as e:
|
| 920 |
logger.error(f"Health check error: {e}")
|
| 921 |
|
| 922 |
-
sleep(
|
| 923 |
|
| 924 |
# 为了兼容 Flask CLI 和 Gunicorn,修改启动逻辑
|
| 925 |
if __name__ != "__main__":
|
|
|
|
| 309 |
self.current_index = 0
|
| 310 |
self._last_rotation = time.time()
|
| 311 |
self._rotation_interval = 300 # 5分钟轮转间隔
|
| 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)):
|
|
|
|
| 748 |
}), 500
|
| 749 |
|
| 750 |
def build_payload(request_data, model_id):
|
| 751 |
+
"""构建请求有���负载,确保保持完���的上下文。"""
|
| 752 |
messages = request_data.get('messages', [])
|
| 753 |
|
| 754 |
# 检查是否已经存在系统消息
|
|
|
|
| 822 |
while len(tried_accounts) < len(multi_auth_manager.auth_managers):
|
| 823 |
auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
|
| 824 |
if not auth_manager:
|
|
|
|
|
|
|
|
|
|
| 825 |
break
|
| 826 |
|
| 827 |
# 如果这个账号已经尝试过,继续下一个
|
|
|
|
| 845 |
|
| 846 |
if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
|
| 847 |
logger.info(f"请求成功,使用账号 {auth_manager._email}")
|
| 848 |
+
# 更新最后成功使用的账号索引
|
| 849 |
+
current_index = multi_auth_manager.auth_managers.index(auth_manager)
|
| 850 |
+
multi_auth_manager.update_last_successful(current_index)
|
| 851 |
return response
|
| 852 |
|
| 853 |
headers_cache.clear()
|
|
|
|
| 883 |
raise Exception("所有账号均不可用,且注册新账号失败")
|
| 884 |
|
| 885 |
def health_check():
|
| 886 |
+
"""改进的健康检查函数,每60秒只检查一个账号"""
|
| 887 |
check_index = 0
|
| 888 |
+
last_check_date = datetime.now().date()
|
|
|
|
| 889 |
|
| 890 |
while True:
|
| 891 |
try:
|
| 892 |
if multi_auth_manager:
|
|
|
|
| 893 |
current_date = datetime.now().date()
|
| 894 |
|
| 895 |
+
# 如果是新的一天,重置检查索引
|
| 896 |
+
if current_date > last_check_date:
|
| 897 |
+
check_index = 0
|
| 898 |
+
last_check_date = current_date
|
| 899 |
+
logger.info("New day started, resetting health check index")
|
| 900 |
+
continue
|
| 901 |
+
|
| 902 |
+
# 只检查一个账号
|
| 903 |
+
if check_index < len(multi_auth_manager.auth_managers):
|
| 904 |
auth_manager = multi_auth_manager.auth_managers[check_index]
|
| 905 |
+
email = auth_manager._email
|
| 906 |
|
| 907 |
if auth_manager._should_attempt_auth():
|
| 908 |
if not auth_manager.ensure_valid_token():
|
| 909 |
+
logger.warning(f"Auth token validation failed during health check for {email}")
|
| 910 |
auth_manager.clear_auth()
|
| 911 |
else:
|
| 912 |
+
logger.info(f"Health check passed for {email}")
|
| 913 |
+
else:
|
| 914 |
+
logger.info(f"Skipping health check for {email} due to rate limiting")
|
| 915 |
|
| 916 |
+
# 更新检查索引
|
| 917 |
check_index = (check_index + 1) % len(multi_auth_manager.auth_managers)
|
| 918 |
+
|
| 919 |
+
# 在每天午夜重置所有账号的模型使用状态
|
| 920 |
+
current_time_local = time.localtime()
|
| 921 |
+
if current_time_local.tm_hour == 0 and current_time_local.tm_min == 0:
|
| 922 |
+
multi_auth_manager.reset_all_model_status()
|
| 923 |
+
logger.info("Reset model status for all accounts")
|
| 924 |
|
| 925 |
except Exception as e:
|
| 926 |
logger.error(f"Health check error: {e}")
|
| 927 |
|
| 928 |
+
sleep(60) # 每60秒检查一个账号
|
| 929 |
|
| 930 |
# 为了兼容 Flask CLI 和 Gunicorn,修改启动逻辑
|
| 931 |
if __name__ != "__main__":
|