Update app.py
Browse files
app.py
CHANGED
@@ -87,11 +87,22 @@ def extract_user_content(messages):
|
|
87 |
def refresh_models():
|
88 |
global models
|
89 |
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
for model_type in ["text"]:
|
93 |
logging.info(f"所有{model_type}模型列表:{models[model_type]}")
|
94 |
-
logging.info(f"免费{model_type}模型列表:{models[f'free_{model_type}']}")
|
95 |
|
96 |
def load_keys():
|
97 |
global key_status
|
@@ -106,7 +117,27 @@ def load_keys():
|
|
106 |
logging.warning("环境变量 KEYS 未设置。")
|
107 |
return
|
108 |
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
def process_key(key, test_model):
|
112 |
credit_summary = get_credit_summary(key)
|
@@ -500,6 +531,56 @@ def handsome_chat_completions():
|
|
500 |
except requests.exceptions.RequestException as e:
|
501 |
logging.error(f"请求转发异常: {e}")
|
502 |
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
503 |
|
504 |
if __name__ == '__main__':
|
505 |
logging.info(f"环境变量:{os.environ}")
|
@@ -514,4 +595,4 @@ if __name__ == '__main__':
|
|
514 |
refresh_models()
|
515 |
logging.info("首次刷新模型列表已手动触发执行")
|
516 |
|
517 |
-
app.run(debug=False,host='0.0.0.0',port=int(os.environ.get('PORT', 7860)))
|
|
|
87 |
def refresh_models():
|
88 |
global models
|
89 |
|
90 |
+
# Find the first valid key
|
91 |
+
first_valid_key = None
|
92 |
+
for key_list in key_status.values():
|
93 |
+
if key_list:
|
94 |
+
first_valid_key = key_list[0]
|
95 |
+
break
|
96 |
+
|
97 |
+
if first_valid_key:
|
98 |
+
models["text"] = get_all_models(first_valid_key)
|
99 |
+
else:
|
100 |
+
logging.warning("No valid keys found to fetch models.")
|
101 |
+
models["text"] = []
|
102 |
+
|
103 |
|
104 |
for model_type in ["text"]:
|
105 |
logging.info(f"所有{model_type}模型列表:{models[model_type]}")
|
|
|
106 |
|
107 |
def load_keys():
|
108 |
global key_status
|
|
|
117 |
logging.warning("环境变量 KEYS 未设置。")
|
118 |
return
|
119 |
|
120 |
+
keys = keys_str.split(",")
|
121 |
+
keys = [key.strip() for key in keys]
|
122 |
+
|
123 |
+
global valid_keys_global, free_keys_global, unverified_keys_global
|
124 |
+
valid_keys_global = []
|
125 |
+
free_keys_global = []
|
126 |
+
unverified_keys_global = []
|
127 |
+
|
128 |
+
|
129 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
130 |
+
futures = [executor.submit(process_key, key, "gpt-3.5-turbo") for key in keys]
|
131 |
+
for key, future in zip(keys, futures):
|
132 |
+
status = future.result()
|
133 |
+
key_status[status].append(key)
|
134 |
+
if status == "valid":
|
135 |
+
valid_keys_global.append(key)
|
136 |
+
elif status == "free":
|
137 |
+
free_keys_global.append(key)
|
138 |
+
elif status == "unverified":
|
139 |
+
unverified_keys_global.append(key)
|
140 |
+
logging.info(f"Key {key} status: {status}")
|
141 |
|
142 |
def process_key(key, test_model):
|
143 |
credit_summary = get_credit_summary(key)
|
|
|
531 |
except requests.exceptions.RequestException as e:
|
532 |
logging.error(f"请求转发异常: {e}")
|
533 |
return jsonify({"error": str(e)}), 500
|
534 |
+
|
535 |
+
def get_credit_summary(api_key):
|
536 |
+
headers = {
|
537 |
+
"Authorization": f"Bearer {api_key}",
|
538 |
+
"Content-Type": "application/json"
|
539 |
+
}
|
540 |
+
try:
|
541 |
+
response = session.get(
|
542 |
+
API_ENDPOINT,
|
543 |
+
headers=headers
|
544 |
+
)
|
545 |
+
response.raise_for_status()
|
546 |
+
return response.json().get("credit_summary")
|
547 |
+
except requests.exceptions.RequestException as e:
|
548 |
+
logging.error(
|
549 |
+
f"获取账户余额失败,"
|
550 |
+
f"API Key:{api_key},错误信息:{e}"
|
551 |
+
)
|
552 |
+
return None
|
553 |
+
except (KeyError, TypeError) as e:
|
554 |
+
logging.error(
|
555 |
+
f"解析账户余额失败,"
|
556 |
+
f"API Key:{api_key},错误信息:{e}"
|
557 |
+
)
|
558 |
+
return None
|
559 |
+
|
560 |
+
def test_model_availability(api_key, test_model):
|
561 |
+
headers = {
|
562 |
+
"Authorization": f"Bearer {api_key}",
|
563 |
+
"Content-Type": "application/json"
|
564 |
+
}
|
565 |
+
data = {
|
566 |
+
"model": test_model,
|
567 |
+
"messages": [{"role": "user", "content": "hello"}]
|
568 |
+
}
|
569 |
+
try:
|
570 |
+
response = session.post(
|
571 |
+
TEST_MODEL_ENDPOINT,
|
572 |
+
headers=headers,
|
573 |
+
json=data
|
574 |
+
)
|
575 |
+
response.raise_for_status()
|
576 |
+
return True
|
577 |
+
except requests.exceptions.RequestException as e:
|
578 |
+
logging.error(
|
579 |
+
f"模型测试失败,"
|
580 |
+
f"API Key:{api_key},错误信息:{e}"
|
581 |
+
)
|
582 |
+
return False
|
583 |
+
|
584 |
|
585 |
if __name__ == '__main__':
|
586 |
logging.info(f"环境变量:{os.environ}")
|
|
|
595 |
refresh_models()
|
596 |
logging.info("首次刷新模型列表已手动触发执行")
|
597 |
|
598 |
+
app.run(debug=False,host='0.0.0.0',port=int(os.environ.get('PORT', 7860)))
|