Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -305,25 +305,43 @@ class MultiAuthManager:
|
|
| 305 |
def __init__(self, credentials):
|
| 306 |
self.auth_managers = [AuthManager(email, password) for email, password in credentials]
|
| 307 |
self.current_index = 0
|
| 308 |
-
self.last_success_index = None
|
| 309 |
self._last_rotation = time.time()
|
| 310 |
-
self._rotation_interval = 300
|
| 311 |
-
self._model_usage = {}
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
|
| 317 |
def get_next_auth_manager(self, model):
|
| 318 |
"""改进的账号选择逻辑,优先使用模型对应的上次成功账号"""
|
|
|
|
|
|
|
|
|
|
| 319 |
# 首先尝试使用该模型上次成功的账号
|
| 320 |
if model in self._model_usage:
|
| 321 |
last_success_index = self._model_usage[model]
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
| 325 |
|
| 326 |
# 如果没有该模型的成功记录,或上次成功的账号不可用,则从当前位置开始轮询
|
|
|
|
|
|
|
|
|
|
| 327 |
start_index = self.current_index
|
| 328 |
for _ in range(len(self.auth_managers)):
|
| 329 |
auth_manager = self.auth_managers[self.current_index]
|
|
@@ -334,24 +352,18 @@ class MultiAuthManager:
|
|
| 334 |
break
|
| 335 |
return None
|
| 336 |
|
| 337 |
-
def ensure_valid_token(self, model):
|
| 338 |
-
"""确保获取有效的token"""
|
| 339 |
-
auth_manager = self.get_next_auth_manager(model)
|
| 340 |
-
if auth_manager and auth_manager.ensure_valid_token():
|
| 341 |
-
return auth_manager
|
| 342 |
-
return None
|
| 343 |
-
|
| 344 |
def mark_success(self, auth_manager, model):
|
| 345 |
"""记录成功使用的账号索引,并与模型关联"""
|
| 346 |
for i, manager in enumerate(self.auth_managers):
|
| 347 |
if manager == auth_manager:
|
| 348 |
-
self._model_usage[model] = i
|
| 349 |
self.last_success_index = i
|
| 350 |
break
|
| 351 |
|
| 352 |
def reset_model_status(self):
|
| 353 |
"""重置所有账号的模型使用状态"""
|
| 354 |
-
self._model_usage.clear()
|
|
|
|
| 355 |
for auth_manager in self.auth_managers:
|
| 356 |
auth_manager.reset_model_status()
|
| 357 |
|
|
@@ -805,7 +817,7 @@ def make_request(payload, auth_manager, model_id):
|
|
| 805 |
logger.error("MultiAuthManager 不存在,尝试重新初始化")
|
| 806 |
credentials = get_auth_credentials()
|
| 807 |
if not credentials:
|
| 808 |
-
logger.error("无法获取凭据,尝试注新账号")
|
| 809 |
successful_accounts = register_bot.register_and_verify(5)
|
| 810 |
if successful_accounts:
|
| 811 |
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
|
@@ -842,7 +854,6 @@ def make_request(payload, auth_manager, model_id):
|
|
| 842 |
|
| 843 |
if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
|
| 844 |
logger.info(f"请求成功,使用账号 {auth_manager._email}")
|
| 845 |
-
# 记录成功使用的账号,并关联模型
|
| 846 |
multi_auth_manager.mark_success(auth_manager, model_id)
|
| 847 |
return response
|
| 848 |
|
|
@@ -850,30 +861,39 @@ def make_request(payload, auth_manager, model_id):
|
|
| 850 |
|
| 851 |
if response.status_code == 401: # Unauthorized
|
| 852 |
logger.info(f"Token expired for account {auth_manager._email}, attempting refresh")
|
| 853 |
-
|
| 854 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
|
| 856 |
if response.status_code == 403: # Forbidden, 模型使用限制
|
| 857 |
logger.warning(f"Model {model_id} usage limit reached for account {auth_manager._email}")
|
| 858 |
auth_manager.set_model_unavailable(model_id)
|
| 859 |
-
break
|
| 860 |
|
| 861 |
logger.error(f"Request failed with status {response.status_code} for account {auth_manager._email}")
|
| 862 |
|
| 863 |
except Exception as e:
|
| 864 |
logger.error(f"Request attempt {attempt + 1} failed for account {auth_manager._email}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
if attempt < max_retries - 1:
|
| 866 |
time.sleep(retry_delay)
|
| 867 |
continue
|
| 868 |
|
| 869 |
-
#
|
| 870 |
-
if len(tried_accounts) == len(multi_auth_manager.auth_managers):
|
| 871 |
-
logger.info("所有现有账号都已尝试,开始注册新账号")
|
| 872 |
successful_accounts = register_bot.register_and_verify(5)
|
| 873 |
if successful_accounts:
|
| 874 |
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
| 875 |
multi_auth_manager = MultiAuthManager(credentials)
|
| 876 |
-
# 使用新注册的账号重试请求
|
| 877 |
return make_request(payload, None, model_id)
|
| 878 |
|
| 879 |
raise Exception("所有账号均不可用,且注册新账号失败")
|
|
|
|
| 305 |
def __init__(self, credentials):
|
| 306 |
self.auth_managers = [AuthManager(email, password) for email, password in credentials]
|
| 307 |
self.current_index = 0
|
| 308 |
+
self.last_success_index = None
|
| 309 |
self._last_rotation = time.time()
|
| 310 |
+
self._rotation_interval = 300
|
| 311 |
+
self._model_usage = {}
|
| 312 |
+
self._invalid_accounts = set() # 记录失效的账号
|
| 313 |
+
|
| 314 |
+
def remove_invalid_account(self, auth_manager):
|
| 315 |
+
"""移除失效的账号"""
|
| 316 |
+
if auth_manager._email not in self._invalid_accounts:
|
| 317 |
+
self._invalid_accounts.add(auth_manager._email)
|
| 318 |
+
self.auth_managers = [am for am in self.auth_managers if am._email != auth_manager._email]
|
| 319 |
+
logger.info(f"已移除失效账号: {auth_manager._email}")
|
| 320 |
+
|
| 321 |
+
# 如果移除的是当前使用的账号,重置索引
|
| 322 |
+
if self.last_success_index is not None:
|
| 323 |
+
if self.last_success_index >= len(self.auth_managers):
|
| 324 |
+
self.last_success_index = None
|
| 325 |
+
if self.current_index >= len(self.auth_managers):
|
| 326 |
+
self.current_index = 0
|
| 327 |
|
| 328 |
def get_next_auth_manager(self, model):
|
| 329 |
"""改进的账号选择逻辑,优先使用模型对应的上次成功账号"""
|
| 330 |
+
if not self.auth_managers: # 如果没有可用账号,返回 None
|
| 331 |
+
return None
|
| 332 |
+
|
| 333 |
# 首先尝试使用该模型上次成功的账号
|
| 334 |
if model in self._model_usage:
|
| 335 |
last_success_index = self._model_usage[model]
|
| 336 |
+
if last_success_index < len(self.auth_managers):
|
| 337 |
+
auth_manager = self.auth_managers[last_success_index]
|
| 338 |
+
if auth_manager.is_model_available(model) and auth_manager._should_attempt_auth():
|
| 339 |
+
return auth_manager
|
| 340 |
|
| 341 |
# 如果没有该模型的成功记录,或上次成功的账号不可用,则从当前位置开始轮询
|
| 342 |
+
if len(self.auth_managers) == 0:
|
| 343 |
+
return None
|
| 344 |
+
|
| 345 |
start_index = self.current_index
|
| 346 |
for _ in range(len(self.auth_managers)):
|
| 347 |
auth_manager = self.auth_managers[self.current_index]
|
|
|
|
| 352 |
break
|
| 353 |
return None
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
def mark_success(self, auth_manager, model):
|
| 356 |
"""记录成功使用的账号索引,并与模型关联"""
|
| 357 |
for i, manager in enumerate(self.auth_managers):
|
| 358 |
if manager == auth_manager:
|
| 359 |
+
self._model_usage[model] = i
|
| 360 |
self.last_success_index = i
|
| 361 |
break
|
| 362 |
|
| 363 |
def reset_model_status(self):
|
| 364 |
"""重置所有账号的模型使用状态"""
|
| 365 |
+
self._model_usage.clear()
|
| 366 |
+
self._invalid_accounts.clear() # 清除失效账号记录
|
| 367 |
for auth_manager in self.auth_managers:
|
| 368 |
auth_manager.reset_model_status()
|
| 369 |
|
|
|
|
| 817 |
logger.error("MultiAuthManager 不存在,尝试重新初始化")
|
| 818 |
credentials = get_auth_credentials()
|
| 819 |
if not credentials:
|
| 820 |
+
logger.error("无法获取凭据,尝试注册新账号")
|
| 821 |
successful_accounts = register_bot.register_and_verify(5)
|
| 822 |
if successful_accounts:
|
| 823 |
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
|
|
|
| 854 |
|
| 855 |
if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
|
| 856 |
logger.info(f"请求成功,使用账号 {auth_manager._email}")
|
|
|
|
| 857 |
multi_auth_manager.mark_success(auth_manager, model_id)
|
| 858 |
return response
|
| 859 |
|
|
|
|
| 861 |
|
| 862 |
if response.status_code == 401: # Unauthorized
|
| 863 |
logger.info(f"Token expired for account {auth_manager._email}, attempting refresh")
|
| 864 |
+
try:
|
| 865 |
+
if auth_manager.ensure_valid_token():
|
| 866 |
+
continue
|
| 867 |
+
except requests.exceptions.RequestException as e:
|
| 868 |
+
if "400 Client Error: Bad Request" in str(e):
|
| 869 |
+
logger.error(f"账号 {auth_manager._email} 已失效")
|
| 870 |
+
multi_auth_manager.remove_invalid_account(auth_manager)
|
| 871 |
+
break
|
| 872 |
|
| 873 |
if response.status_code == 403: # Forbidden, 模型使用限制
|
| 874 |
logger.warning(f"Model {model_id} usage limit reached for account {auth_manager._email}")
|
| 875 |
auth_manager.set_model_unavailable(model_id)
|
| 876 |
+
break
|
| 877 |
|
| 878 |
logger.error(f"Request failed with status {response.status_code} for account {auth_manager._email}")
|
| 879 |
|
| 880 |
except Exception as e:
|
| 881 |
logger.error(f"Request attempt {attempt + 1} failed for account {auth_manager._email}: {e}")
|
| 882 |
+
if "400 Client Error: Bad Request" in str(e):
|
| 883 |
+
logger.error(f"账号 {auth_manager._email} 已失效")
|
| 884 |
+
multi_auth_manager.remove_invalid_account(auth_manager)
|
| 885 |
+
break
|
| 886 |
if attempt < max_retries - 1:
|
| 887 |
time.sleep(retry_delay)
|
| 888 |
continue
|
| 889 |
|
| 890 |
+
# 检查是否需要注册新账号
|
| 891 |
+
if not multi_auth_manager.auth_managers or len(tried_accounts) == len(multi_auth_manager.auth_managers):
|
| 892 |
+
logger.info("所有现有账号都已尝试或无可用账号,开始注册新账号")
|
| 893 |
successful_accounts = register_bot.register_and_verify(5)
|
| 894 |
if successful_accounts:
|
| 895 |
credentials = [(account['email'], account['password']) for account in successful_accounts]
|
| 896 |
multi_auth_manager = MultiAuthManager(credentials)
|
|
|
|
| 897 |
return make_request(payload, None, model_id)
|
| 898 |
|
| 899 |
raise Exception("所有账号均不可用,且注册新账号失败")
|