Update app.py
Browse files
app.py
CHANGED
|
@@ -39,7 +39,7 @@ free_keys_global = []
|
|
| 39 |
unverified_keys_global = []
|
| 40 |
valid_keys_global = []
|
| 41 |
|
| 42 |
-
executor = concurrent.futures.ThreadPoolExecutor(max_workers=
|
| 43 |
model_key_indices = {}
|
| 44 |
|
| 45 |
request_timestamps = []
|
|
@@ -47,9 +47,6 @@ token_counts = []
|
|
| 47 |
data_lock = threading.Lock()
|
| 48 |
|
| 49 |
def get_credit_summary(api_key):
|
| 50 |
-
"""
|
| 51 |
-
使用 API 密钥获取额度信息。
|
| 52 |
-
"""
|
| 53 |
headers = {
|
| 54 |
"Authorization": f"Bearer {api_key}",
|
| 55 |
"Content-Type": "application/json"
|
|
@@ -77,9 +74,6 @@ FREE_IMAGE_LIST = [
|
|
| 77 |
]
|
| 78 |
|
| 79 |
def test_model_availability(api_key, model_name):
|
| 80 |
-
"""
|
| 81 |
-
测试指定的模型是否可用。
|
| 82 |
-
"""
|
| 83 |
headers = {
|
| 84 |
"Authorization": f"Bearer {api_key}",
|
| 85 |
"Content-Type": "application/json"
|
|
@@ -108,9 +102,6 @@ def test_model_availability(api_key, model_name):
|
|
| 108 |
return False
|
| 109 |
|
| 110 |
def refresh_models():
|
| 111 |
-
"""
|
| 112 |
-
刷新模型列表和免费模型列表。
|
| 113 |
-
"""
|
| 114 |
global text_models, free_text_models
|
| 115 |
global embedding_models, free_embedding_models
|
| 116 |
global image_models, free_image_models
|
|
@@ -143,7 +134,7 @@ def refresh_models():
|
|
| 143 |
image_models = [model for model in image_models if model not in ban_models]
|
| 144 |
|
| 145 |
with concurrent.futures.ThreadPoolExecutor(
|
| 146 |
-
max_workers=
|
| 147 |
) as executor:
|
| 148 |
future_to_model = {
|
| 149 |
executor.submit(
|
|
@@ -162,7 +153,7 @@ def refresh_models():
|
|
| 162 |
logging.error(f"模型 {model} 测试生成异常: {exc}")
|
| 163 |
|
| 164 |
with concurrent.futures.ThreadPoolExecutor(
|
| 165 |
-
max_workers=
|
| 166 |
) as executor:
|
| 167 |
future_to_model = {
|
| 168 |
executor.submit(
|
|
@@ -180,7 +171,7 @@ def refresh_models():
|
|
| 180 |
logging.error(f"模型 {model} 测试生成异常: {exc}")
|
| 181 |
|
| 182 |
with concurrent.futures.ThreadPoolExecutor(
|
| 183 |
-
max_workers=
|
| 184 |
) as executor:
|
| 185 |
future_to_model = {
|
| 186 |
executor.submit(
|
|
@@ -205,9 +196,6 @@ def refresh_models():
|
|
| 205 |
logging.info(f"免费生图模型列表:{free_image_models}")
|
| 206 |
|
| 207 |
def test_embedding_model_availability(api_key, model_name):
|
| 208 |
-
"""
|
| 209 |
-
测试指定的向量模型是否可用。
|
| 210 |
-
"""
|
| 211 |
headers = {
|
| 212 |
"Authorization": f"Bearer {api_key}",
|
| 213 |
"Content-Type": "application/json"
|
|
@@ -234,19 +222,9 @@ def test_embedding_model_availability(api_key, model_name):
|
|
| 234 |
return False
|
| 235 |
|
| 236 |
def test_image_model_availability(api_key, model_name):
|
| 237 |
-
"""
|
| 238 |
-
测试指定的图像模型是否在 FREE_IMAGE_LIST 中。
|
| 239 |
-
如果在列表中,返回 True,否则返回 False。
|
| 240 |
-
"""
|
| 241 |
return model_name in FREE_IMAGE_LIST
|
| 242 |
|
| 243 |
def load_keys():
|
| 244 |
-
"""
|
| 245 |
-
从环境变量中加载 keys,进行去重,
|
| 246 |
-
并根据额度和模型可用性进行分类,
|
| 247 |
-
然后记录到日志中。
|
| 248 |
-
使用线程池并发处理每个 key。
|
| 249 |
-
"""
|
| 250 |
keys_str = os.environ.get("KEYS")
|
| 251 |
test_model = os.environ.get(
|
| 252 |
"TEST_MODEL",
|
|
@@ -262,7 +240,7 @@ def load_keys():
|
|
| 262 |
logging.info(f"加载的 keys:{unique_keys}")
|
| 263 |
|
| 264 |
with concurrent.futures.ThreadPoolExecutor(
|
| 265 |
-
max_workers=
|
| 266 |
) as executor:
|
| 267 |
future_to_key = {
|
| 268 |
executor.submit(
|
|
@@ -308,9 +286,6 @@ def load_keys():
|
|
| 308 |
logging.warning("环境变量 KEYS 未设置。")
|
| 309 |
|
| 310 |
def process_key(key, test_model):
|
| 311 |
-
"""
|
| 312 |
-
处理单个 key,判断其类型。
|
| 313 |
-
"""
|
| 314 |
credit_summary = get_credit_summary(key)
|
| 315 |
if credit_summary is None:
|
| 316 |
return "invalid"
|
|
@@ -325,9 +300,6 @@ def process_key(key, test_model):
|
|
| 325 |
return "unverified"
|
| 326 |
|
| 327 |
def get_all_models(api_key, sub_type):
|
| 328 |
-
"""
|
| 329 |
-
获取所有模型列表。
|
| 330 |
-
"""
|
| 331 |
headers = {
|
| 332 |
"Authorization": f"Bearer {api_key}",
|
| 333 |
"Content-Type": "application/json"
|
|
@@ -366,9 +338,6 @@ def get_all_models(api_key, sub_type):
|
|
| 366 |
return []
|
| 367 |
|
| 368 |
def determine_request_type(model_name, model_list, free_model_list):
|
| 369 |
-
"""
|
| 370 |
-
根据用户请求的模型判断请求类型。
|
| 371 |
-
"""
|
| 372 |
if model_name in free_model_list:
|
| 373 |
return "free"
|
| 374 |
elif model_name in model_list:
|
|
@@ -377,10 +346,6 @@ def determine_request_type(model_name, model_list, free_model_list):
|
|
| 377 |
return "unknown"
|
| 378 |
|
| 379 |
def select_key(request_type, model_name):
|
| 380 |
-
"""
|
| 381 |
-
根据请求类型和模型名称选择合适的 KEY,
|
| 382 |
-
并实现轮询和重试机制。
|
| 383 |
-
"""
|
| 384 |
if request_type == "free":
|
| 385 |
available_keys = (
|
| 386 |
free_keys_global +
|
|
@@ -417,10 +382,6 @@ def select_key(request_type, model_name):
|
|
| 417 |
return None
|
| 418 |
|
| 419 |
def key_is_valid(key, request_type):
|
| 420 |
-
"""
|
| 421 |
-
检查 KEY 是否有效,
|
| 422 |
-
根据不同的请求类型进行不同的检查。
|
| 423 |
-
"""
|
| 424 |
if request_type == "invalid":
|
| 425 |
return False
|
| 426 |
|
|
@@ -438,10 +399,6 @@ def key_is_valid(key, request_type):
|
|
| 438 |
return False
|
| 439 |
|
| 440 |
def check_authorization(request):
|
| 441 |
-
"""
|
| 442 |
-
检查请求头中的 Authorization 字段
|
| 443 |
-
是否匹配环境变量 AUTHORIZATION_KEY。
|
| 444 |
-
"""
|
| 445 |
authorization_key = os.environ.get("AUTHORIZATION_KEY")
|
| 446 |
if not authorization_key:
|
| 447 |
logging.warning("环境变量 AUTHORIZATION_KEY 未设置,请设置后重试。")
|
|
@@ -487,7 +444,7 @@ def check_tokens():
|
|
| 487 |
)
|
| 488 |
|
| 489 |
with concurrent.futures.ThreadPoolExecutor(
|
| 490 |
-
max_workers=
|
| 491 |
) as executor:
|
| 492 |
future_to_token = {
|
| 493 |
executor.submit(
|
|
|
|
| 39 |
unverified_keys_global = []
|
| 40 |
valid_keys_global = []
|
| 41 |
|
| 42 |
+
executor = concurrent.futures.ThreadPoolExecutor(max_workers=10000)
|
| 43 |
model_key_indices = {}
|
| 44 |
|
| 45 |
request_timestamps = []
|
|
|
|
| 47 |
data_lock = threading.Lock()
|
| 48 |
|
| 49 |
def get_credit_summary(api_key):
|
|
|
|
|
|
|
|
|
|
| 50 |
headers = {
|
| 51 |
"Authorization": f"Bearer {api_key}",
|
| 52 |
"Content-Type": "application/json"
|
|
|
|
| 74 |
]
|
| 75 |
|
| 76 |
def test_model_availability(api_key, model_name):
|
|
|
|
|
|
|
|
|
|
| 77 |
headers = {
|
| 78 |
"Authorization": f"Bearer {api_key}",
|
| 79 |
"Content-Type": "application/json"
|
|
|
|
| 102 |
return False
|
| 103 |
|
| 104 |
def refresh_models():
|
|
|
|
|
|
|
|
|
|
| 105 |
global text_models, free_text_models
|
| 106 |
global embedding_models, free_embedding_models
|
| 107 |
global image_models, free_image_models
|
|
|
|
| 134 |
image_models = [model for model in image_models if model not in ban_models]
|
| 135 |
|
| 136 |
with concurrent.futures.ThreadPoolExecutor(
|
| 137 |
+
max_workers=10000
|
| 138 |
) as executor:
|
| 139 |
future_to_model = {
|
| 140 |
executor.submit(
|
|
|
|
| 153 |
logging.error(f"模型 {model} 测试生成异常: {exc}")
|
| 154 |
|
| 155 |
with concurrent.futures.ThreadPoolExecutor(
|
| 156 |
+
max_workers=10000
|
| 157 |
) as executor:
|
| 158 |
future_to_model = {
|
| 159 |
executor.submit(
|
|
|
|
| 171 |
logging.error(f"模型 {model} 测试生成异常: {exc}")
|
| 172 |
|
| 173 |
with concurrent.futures.ThreadPoolExecutor(
|
| 174 |
+
max_workers=10000
|
| 175 |
) as executor:
|
| 176 |
future_to_model = {
|
| 177 |
executor.submit(
|
|
|
|
| 196 |
logging.info(f"免费生图模型列表:{free_image_models}")
|
| 197 |
|
| 198 |
def test_embedding_model_availability(api_key, model_name):
|
|
|
|
|
|
|
|
|
|
| 199 |
headers = {
|
| 200 |
"Authorization": f"Bearer {api_key}",
|
| 201 |
"Content-Type": "application/json"
|
|
|
|
| 222 |
return False
|
| 223 |
|
| 224 |
def test_image_model_availability(api_key, model_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
return model_name in FREE_IMAGE_LIST
|
| 226 |
|
| 227 |
def load_keys():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
keys_str = os.environ.get("KEYS")
|
| 229 |
test_model = os.environ.get(
|
| 230 |
"TEST_MODEL",
|
|
|
|
| 240 |
logging.info(f"加载的 keys:{unique_keys}")
|
| 241 |
|
| 242 |
with concurrent.futures.ThreadPoolExecutor(
|
| 243 |
+
max_workers=10000
|
| 244 |
) as executor:
|
| 245 |
future_to_key = {
|
| 246 |
executor.submit(
|
|
|
|
| 286 |
logging.warning("环境变量 KEYS 未设置。")
|
| 287 |
|
| 288 |
def process_key(key, test_model):
|
|
|
|
|
|
|
|
|
|
| 289 |
credit_summary = get_credit_summary(key)
|
| 290 |
if credit_summary is None:
|
| 291 |
return "invalid"
|
|
|
|
| 300 |
return "unverified"
|
| 301 |
|
| 302 |
def get_all_models(api_key, sub_type):
|
|
|
|
|
|
|
|
|
|
| 303 |
headers = {
|
| 304 |
"Authorization": f"Bearer {api_key}",
|
| 305 |
"Content-Type": "application/json"
|
|
|
|
| 338 |
return []
|
| 339 |
|
| 340 |
def determine_request_type(model_name, model_list, free_model_list):
|
|
|
|
|
|
|
|
|
|
| 341 |
if model_name in free_model_list:
|
| 342 |
return "free"
|
| 343 |
elif model_name in model_list:
|
|
|
|
| 346 |
return "unknown"
|
| 347 |
|
| 348 |
def select_key(request_type, model_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
if request_type == "free":
|
| 350 |
available_keys = (
|
| 351 |
free_keys_global +
|
|
|
|
| 382 |
return None
|
| 383 |
|
| 384 |
def key_is_valid(key, request_type):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
if request_type == "invalid":
|
| 386 |
return False
|
| 387 |
|
|
|
|
| 399 |
return False
|
| 400 |
|
| 401 |
def check_authorization(request):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
authorization_key = os.environ.get("AUTHORIZATION_KEY")
|
| 403 |
if not authorization_key:
|
| 404 |
logging.warning("环境变量 AUTHORIZATION_KEY 未设置,请设置后重试。")
|
|
|
|
| 444 |
)
|
| 445 |
|
| 446 |
with concurrent.futures.ThreadPoolExecutor(
|
| 447 |
+
max_workers=10000
|
| 448 |
) as executor:
|
| 449 |
future_to_token = {
|
| 450 |
executor.submit(
|