Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ logging.basicConfig(level=logging.INFO,
|
|
17 |
|
18 |
API_ENDPOINT = "https://api-st.siliconflow.cn/v1/user/info"
|
19 |
TEST_MODEL_ENDPOINT = "https://api-st.siliconflow.cn/v1/chat/completions"
|
20 |
-
MODELS_ENDPOINT = "https://api
|
21 |
EMBEDDINGS_ENDPOINT = "https://api-st.siliconflow.cn/v1/embeddings"
|
22 |
IMAGE_ENDPOINT = "https://api-st.siliconflow.cn/v1/images/generations"
|
23 |
|
@@ -239,60 +239,12 @@ def get_siliconflow_data(model_name, data):
|
|
239 |
def refresh_models():
|
240 |
global models
|
241 |
|
242 |
-
models["text"] = get_all_models(FREE_MODEL_TEST_KEY
|
243 |
-
models["embedding"] = get_all_models(FREE_MODEL_TEST_KEY, "embedding")
|
244 |
-
models["image"] = get_all_models(FREE_MODEL_TEST_KEY, "text-to-image")
|
245 |
-
|
246 |
-
models["free_text"] = []
|
247 |
-
models["free_embedding"] = []
|
248 |
-
models["free_image"] = []
|
249 |
-
|
250 |
-
ban_models = []
|
251 |
-
ban_models_str = os.environ.get("BAN_MODELS")
|
252 |
-
if ban_models_str:
|
253 |
-
try:
|
254 |
-
ban_models = json.loads(ban_models_str)
|
255 |
-
if not isinstance(ban_models, list):
|
256 |
-
logging.warning("环境变量 BAN_MODELS 格式不正确,应为 JSON 数组。")
|
257 |
-
ban_models = []
|
258 |
-
except json.JSONDecodeError:
|
259 |
-
logging.warning("环境变量 BAN_MODELS JSON 解析失败,请检查格式。")
|
260 |
-
|
261 |
-
models["text"] = [model for model in models["text"] if model not in ban_models]
|
262 |
-
models["embedding"] = [model for model in models["embedding"] if model not in ban_models]
|
263 |
-
models["image"] = [model for model in models["image"] if model not in ban_models]
|
264 |
-
|
265 |
-
model_types = [
|
266 |
-
("text", "chat"),
|
267 |
-
("embedding", "embedding"),
|
268 |
-
("image", "image")
|
269 |
-
]
|
270 |
-
|
271 |
-
for model_type, test_type in model_types:
|
272 |
-
with concurrent.futures.ThreadPoolExecutor(max_workers=10000) as executor:
|
273 |
-
future_to_model = {
|
274 |
-
executor.submit(
|
275 |
-
test_model_availability,
|
276 |
-
FREE_MODEL_TEST_KEY,
|
277 |
-
model,
|
278 |
-
test_type
|
279 |
-
): model for model in models[model_type]
|
280 |
-
}
|
281 |
-
|
282 |
-
for future in concurrent.futures.as_completed(future_to_model):
|
283 |
-
model = future_to_model[future]
|
284 |
-
try:
|
285 |
-
is_free = future.result()
|
286 |
-
if is_free:
|
287 |
-
models[f"free_{model_type}"].append(model)
|
288 |
-
except Exception as exc:
|
289 |
-
logging.error(f"{model_type}模型 {model} 测试生成异常: {exc}")
|
290 |
|
291 |
-
for model_type in ["text"
|
292 |
logging.info(f"所有{model_type}模型列表:{models[model_type]}")
|
293 |
logging.info(f"免费{model_type}模型列表:{models[f'free_{model_type}']}")
|
294 |
|
295 |
-
|
296 |
def load_keys():
|
297 |
global key_status
|
298 |
for status in key_status:
|
@@ -319,7 +271,7 @@ def process_key(key, test_model):
|
|
319 |
else:
|
320 |
return "unverified"
|
321 |
|
322 |
-
def get_all_models(api_key
|
323 |
headers = {
|
324 |
"Authorization": f"Bearer {api_key}",
|
325 |
"Content-Type": "application/json"
|
@@ -327,8 +279,7 @@ def get_all_models(api_key, sub_type):
|
|
327 |
try:
|
328 |
response = session.get(
|
329 |
MODELS_ENDPOINT,
|
330 |
-
headers=headers
|
331 |
-
params={"sub_type": sub_type}
|
332 |
)
|
333 |
response.raise_for_status()
|
334 |
data = response.json()
|
|
|
17 |
|
18 |
API_ENDPOINT = "https://api-st.siliconflow.cn/v1/user/info"
|
19 |
TEST_MODEL_ENDPOINT = "https://api-st.siliconflow.cn/v1/chat/completions"
|
20 |
+
MODELS_ENDPOINT = "https://api.openai.com/v1/models"
|
21 |
EMBEDDINGS_ENDPOINT = "https://api-st.siliconflow.cn/v1/embeddings"
|
22 |
IMAGE_ENDPOINT = "https://api-st.siliconflow.cn/v1/images/generations"
|
23 |
|
|
|
239 |
def refresh_models():
|
240 |
global models
|
241 |
|
242 |
+
models["text"] = get_all_models(FREE_MODEL_TEST_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
+
for model_type in ["text"]:
|
245 |
logging.info(f"所有{model_type}模型列表:{models[model_type]}")
|
246 |
logging.info(f"免费{model_type}模型列表:{models[f'free_{model_type}']}")
|
247 |
|
|
|
248 |
def load_keys():
|
249 |
global key_status
|
250 |
for status in key_status:
|
|
|
271 |
else:
|
272 |
return "unverified"
|
273 |
|
274 |
+
def get_all_models(api_key):
|
275 |
headers = {
|
276 |
"Authorization": f"Bearer {api_key}",
|
277 |
"Content-Type": "application/json"
|
|
|
279 |
try:
|
280 |
response = session.get(
|
281 |
MODELS_ENDPOINT,
|
282 |
+
headers=headers
|
|
|
283 |
)
|
284 |
response.raise_for_status()
|
285 |
data = response.json()
|