dan92 commited on
Commit
9ebea03
·
verified ·
1 Parent(s): 800a154

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -36
app.py CHANGED
@@ -6,7 +6,6 @@ import time
6
  import uuid
7
  import re
8
  import socket
9
- import subprocess # 新增 subprocess 模块用于执行外部脚本
10
  from concurrent.futures import ThreadPoolExecutor
11
  from functools import lru_cache, wraps
12
  from typing import Dict, Any, Callable, List, Tuple
@@ -20,6 +19,9 @@ import urllib3
20
  from cachetools import TTLCache
21
  import threading
22
 
 
 
 
23
  # Constants
24
  CHAT_COMPLETION_CHUNK = 'chat.completion.chunk'
25
  CHAT_COMPLETION = 'chat.completion'
@@ -30,8 +32,8 @@ _USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.3
30
 
31
  # 从环境变量获取API密钥和特定URL
32
  API_KEY = os.getenv('API_KEY')
33
- _PASTE_API_URL = os.getenv('PASTE_API_URL')
34
- _PASTE_API_PASSWORD = os.getenv('PASTE_API_PASSWORD')
35
 
36
  if not API_KEY:
37
  raise ValueError("API_KEY environment variable must be set")
@@ -494,6 +496,26 @@ def get_auth_credentials():
494
  def before_request():
495
  global multi_auth_manager
496
  credentials = get_auth_credentials()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
497
  if credentials:
498
  multi_auth_manager = MultiAuthManager(credentials)
499
  else:
@@ -651,39 +673,7 @@ def make_request(payload, auth_manager, model_id):
651
  auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
652
  if not auth_manager:
653
  logger.error(f"No available accounts for model {model_id}")
654
-
655
- # 尝试自动注册新账号
656
- try:
657
- register_bot_path = os.path.join(os.path.dirname(__file__), 'register_bot.py')
658
-
659
- # 执行 register_bot.py 脚本
660
- result = subprocess.run(
661
- [sys.executable, register_bot_path], # 使用当前 Python 解释器执行脚本
662
- capture_output=True, # 捕获标准输出和标准错误
663
- text=True, # 返回文本输出而非字节
664
- timeout=600 # 设置超时时间为10分钟
665
- )
666
-
667
- # 记录注册脚本的输出
668
- logger.info(f"Register bot stdout: {result.stdout}")
669
- logger.error(f"Register bot stderr: {result.stderr}")
670
-
671
- # 重新初始化 multi_auth_manager
672
- credentials = get_auth_credentials()
673
- if credentials:
674
- multi_auth_manager = MultiAuthManager(credentials)
675
-
676
- # 重新尝试获取账号
677
- auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
678
- if not auth_manager:
679
- raise Exception("Failed to register new accounts")
680
-
681
- except subprocess.CalledProcessError as e:
682
- logger.error(f"Error executing register_bot.py: {e}")
683
- raise Exception(f"No available accounts for model {model_id} and failed to register new accounts")
684
- except Exception as e:
685
- logger.error(f"Unexpected error during account registration: {e}")
686
- raise Exception(f"No available accounts for model {model_id} and failed to register new accounts")
687
 
688
  for attempt in range(max_retries):
689
  try:
 
6
  import uuid
7
  import re
8
  import socket
 
9
  from concurrent.futures import ThreadPoolExecutor
10
  from functools import lru_cache, wraps
11
  from typing import Dict, Any, Callable, List, Tuple
 
19
  from cachetools import TTLCache
20
  import threading
21
 
22
+ # 新增导入
23
+ import register_bot
24
+
25
  # Constants
26
  CHAT_COMPLETION_CHUNK = 'chat.completion.chunk'
27
  CHAT_COMPLETION = 'chat.completion'
 
32
 
33
  # 从环境变量获取API密钥和特定URL
34
  API_KEY = os.getenv('API_KEY')
35
+ _PASTE_API_URL = os.getenv('PASTE_API_URL', "https://page.zhoudan.icu/api/paste/b40v96oX")
36
+ _PASTE_API_PASSWORD = os.getenv('PASTE_API_PASSWORD', '321')
37
 
38
  if not API_KEY:
39
  raise ValueError("API_KEY environment variable must be set")
 
496
  def before_request():
497
  global multi_auth_manager
498
  credentials = get_auth_credentials()
499
+
500
+ # 如果没有凭据,尝试自动注册
501
+ if not credentials:
502
+ try:
503
+ # 使用 register_bot 注册新账号
504
+ successful_accounts = register_bot.register_and_verify(5) # 注册5个账号
505
+
506
+ if successful_accounts:
507
+ # 更新凭据
508
+ credentials = [(account['email'], account['password']) for account in successful_accounts]
509
+ logger.info(f"成功注册 {len(successful_accounts)} 个新账号")
510
+ else:
511
+ logger.error("无法自动注册新账号")
512
+ multi_auth_manager = None
513
+ return
514
+ except Exception as e:
515
+ logger.error(f"自动注册过程发生错误: {e}")
516
+ multi_auth_manager = None
517
+ return
518
+
519
  if credentials:
520
  multi_auth_manager = MultiAuthManager(credentials)
521
  else:
 
673
  auth_manager = multi_auth_manager.get_next_auth_manager(model_id)
674
  if not auth_manager:
675
  logger.error(f"No available accounts for model {model_id}")
676
+ raise Exception(f"No available accounts for model {model_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677
 
678
  for attempt in range(max_retries):
679
  try: