Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ os.environ['TZ'] = 'Asia/Shanghai'
|
|
| 18 |
time.tzset()
|
| 19 |
|
| 20 |
logging.basicConfig(level=logging.INFO,
|
| 21 |
-
format='%(asctime)s - %(levelname)s - %(
|
| 22 |
|
| 23 |
API_ENDPOINT = "https://api.siliconflow.cn/v1/user/info"
|
| 24 |
TEST_MODEL_ENDPOINT = "https://api.siliconflow.cn/v1/chat/completions"
|
|
@@ -46,15 +46,6 @@ request_timestamps = []
|
|
| 46 |
token_counts = []
|
| 47 |
data_lock = threading.Lock()
|
| 48 |
|
| 49 |
-
def get_client_ip():
|
| 50 |
-
"""
|
| 51 |
-
获取客户端IP地址。
|
| 52 |
-
"""
|
| 53 |
-
if request.headers.getlist("X-Forwarded-For"):
|
| 54 |
-
return request.headers.getlist("X-Forwarded-For")[0]
|
| 55 |
-
else:
|
| 56 |
-
return request.remote_addr
|
| 57 |
-
|
| 58 |
def get_credit_summary(api_key):
|
| 59 |
"""
|
| 60 |
使用 API 密钥获取额度信息。
|
|
@@ -552,8 +543,7 @@ def check_tokens():
|
|
| 552 |
)
|
| 553 |
except Exception as exc:
|
| 554 |
logging.error(
|
| 555 |
-
f"处理 Token {token} 生成异常: {exc}"
|
| 556 |
-
extra={'client_ip': get_client_ip()}
|
| 557 |
)
|
| 558 |
|
| 559 |
return jsonify(results)
|
|
@@ -783,8 +773,7 @@ def handsome_embeddings():
|
|
| 783 |
except (KeyError, ValueError, IndexError) as e:
|
| 784 |
logging.error(
|
| 785 |
f"解析响应 JSON 失败: {e}, "
|
| 786 |
-
f"完整内容: {response_json}"
|
| 787 |
-
extra={'client_ip': get_client_ip()}
|
| 788 |
)
|
| 789 |
prompt_tokens = 0
|
| 790 |
embedding_data = []
|
|
@@ -793,8 +782,7 @@ def handsome_embeddings():
|
|
| 793 |
f"使用的key: {api_key}, "
|
| 794 |
f"提示token: {prompt_tokens}, "
|
| 795 |
f"总共用时: {total_time:.4f}秒, "
|
| 796 |
-
f"使用的模型: {model_name}"
|
| 797 |
-
extra={'client_ip': get_client_ip()}
|
| 798 |
)
|
| 799 |
|
| 800 |
with data_lock:
|
|
@@ -948,12 +936,12 @@ def handsome_images_generations():
|
|
| 948 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 949 |
openai_images.append({"b64_json": img_str})
|
| 950 |
except Exception as e:
|
| 951 |
-
logging.error(f"图片转base64失败: {e}"
|
| 952 |
openai_images.append({"url": image_url})
|
| 953 |
else:
|
| 954 |
openai_images.append({"url": image_url})
|
| 955 |
else:
|
| 956 |
-
logging.error(f"无效的图片数据: {item}"
|
| 957 |
openai_images.append({"url": item})
|
| 958 |
|
| 959 |
|
|
@@ -964,8 +952,7 @@ def handsome_images_generations():
|
|
| 964 |
except (KeyError, ValueError, IndexError) as e:
|
| 965 |
logging.error(
|
| 966 |
f"解析响应 JSON 失败: {e}, "
|
| 967 |
-
f"完整内容: {response_json}"
|
| 968 |
-
extra={'client_ip': get_client_ip()}
|
| 969 |
)
|
| 970 |
response_data = {
|
| 971 |
"created": int(time.time()),
|
|
@@ -975,8 +962,7 @@ def handsome_images_generations():
|
|
| 975 |
logging.info(
|
| 976 |
f"使用的key: {api_key}, "
|
| 977 |
f"总共用时: {total_time:.4f}秒, "
|
| 978 |
-
f"使用的模型: {model_name}"
|
| 979 |
-
extra={'client_ip': get_client_ip()}
|
| 980 |
)
|
| 981 |
|
| 982 |
with data_lock:
|
|
@@ -986,7 +972,7 @@ def handsome_images_generations():
|
|
| 986 |
return jsonify(response_data)
|
| 987 |
|
| 988 |
except requests.exceptions.RequestException as e:
|
| 989 |
-
logging.error(f"请求转发异常: {e}"
|
| 990 |
return jsonify({"error": str(e)}), 500
|
| 991 |
else:
|
| 992 |
return jsonify({"error": "Unsupported model"}), 400
|
|
@@ -1144,10 +1130,10 @@ def handsome_chat_completions():
|
|
| 1144 |
image_url = ""
|
| 1145 |
if images and isinstance(images[0], dict) and "url" in images[0]:
|
| 1146 |
image_url = images[0]["url"]
|
| 1147 |
-
logging.info(f"Extracted image URL: {image_url}"
|
| 1148 |
elif images and isinstance(images[0], str):
|
| 1149 |
image_url = images[0]
|
| 1150 |
-
logging.info(f"Extracted image URL: {image_url}"
|
| 1151 |
|
| 1152 |
markdown_image_link = f""
|
| 1153 |
if image_url:
|
|
@@ -1207,7 +1193,7 @@ def handsome_chat_completions():
|
|
| 1207 |
request_timestamps.append(time.time())
|
| 1208 |
token_counts.append(0)
|
| 1209 |
except requests.exceptions.RequestException as e:
|
| 1210 |
-
logging.error(f"请求转发异常: {e}"
|
| 1211 |
error_chunk_data = {
|
| 1212 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 1213 |
"object": "chat.completion.chunk",
|
|
@@ -1241,8 +1227,7 @@ def handsome_chat_completions():
|
|
| 1241 |
yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
|
| 1242 |
logging.info(
|
| 1243 |
f"使用的key: {api_key}, "
|
| 1244 |
-
f"使用的模型: {model_name}"
|
| 1245 |
-
extra={'client_ip': get_client_ip()}
|
| 1246 |
)
|
| 1247 |
yield "data: [DONE]\n\n".encode('utf-8')
|
| 1248 |
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
|
@@ -1259,10 +1244,10 @@ def handsome_chat_completions():
|
|
| 1259 |
image_url = ""
|
| 1260 |
if images and isinstance(images[0], dict) and "url" in images[0]:
|
| 1261 |
image_url = images[0]["url"]
|
| 1262 |
-
logging.info(f"Extracted image URL: {image_url}"
|
| 1263 |
elif images and isinstance(images[0], str):
|
| 1264 |
image_url = images[0]
|
| 1265 |
-
logging.info(f"Extracted image URL: {image_url}"
|
| 1266 |
|
| 1267 |
markdown_image_link = f""
|
| 1268 |
response_data = {
|
|
@@ -1284,8 +1269,7 @@ def handsome_chat_completions():
|
|
| 1284 |
except (KeyError, ValueError, IndexError) as e:
|
| 1285 |
logging.error(
|
| 1286 |
f"解析响应 JSON 失败: {e}, "
|
| 1287 |
-
f"完整内容: {response_json}"
|
| 1288 |
-
extra={'client_ip': get_client_ip()}
|
| 1289 |
)
|
| 1290 |
response_data = {
|
| 1291 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
@@ -1307,8 +1291,7 @@ def handsome_chat_completions():
|
|
| 1307 |
logging.info(
|
| 1308 |
f"使用的key: {api_key}, "
|
| 1309 |
f"总共用时: {total_time:.4f}秒, "
|
| 1310 |
-
f"使用的模型: {model_name}"
|
| 1311 |
-
extra={'client_ip': get_client_ip()}
|
| 1312 |
)
|
| 1313 |
with data_lock:
|
| 1314 |
request_timestamps.append(time.time())
|
|
@@ -1316,7 +1299,7 @@ def handsome_chat_completions():
|
|
| 1316 |
return jsonify(response_data)
|
| 1317 |
|
| 1318 |
except requests.exceptions.RequestException as e:
|
| 1319 |
-
logging.error(f"请求转发异常: {e}"
|
| 1320 |
return jsonify({"error": str(e)}), 500
|
| 1321 |
else:
|
| 1322 |
try:
|
|
@@ -1341,7 +1324,7 @@ def handsome_chat_completions():
|
|
| 1341 |
chunk = chunk.decode('utf-8')
|
| 1342 |
yield f"{chunk}\n\n".encode('utf-8')
|
| 1343 |
except requests.exceptions.RequestException as e:
|
| 1344 |
-
logging.error(f"请求转发异常: {e}"
|
| 1345 |
error_chunk_data = {
|
| 1346 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 1347 |
"object": "chat.completion.chunk",
|
|
@@ -1408,8 +1391,7 @@ def handsome_chat_completions():
|
|
| 1408 |
except (KeyError, ValueError, IndexError) as e:
|
| 1409 |
logging.error(
|
| 1410 |
f"解析响应 JSON 失败: {e}, "
|
| 1411 |
-
f"完整内容: {response_json}"
|
| 1412 |
-
extra={'client_ip': get_client_ip()}
|
| 1413 |
)
|
| 1414 |
response_data = {
|
| 1415 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
@@ -1431,20 +1413,19 @@ def handsome_chat_completions():
|
|
| 1431 |
logging.info(
|
| 1432 |
f"使用的key: {api_key}, "
|
| 1433 |
f"总共用时: {total_time:.4f}秒, "
|
| 1434 |
-
f"使用的模型: {model_name}"
|
| 1435 |
-
extra={'client_ip': get_client_ip()}
|
| 1436 |
)
|
| 1437 |
with data_lock:
|
| 1438 |
request_timestamps.append(time.time())
|
| 1439 |
token_counts.append(0)
|
| 1440 |
return jsonify(response_data)
|
| 1441 |
except requests.exceptions.RequestException as e:
|
| 1442 |
-
logging.error(f"请求转发异常: {e}"
|
| 1443 |
return jsonify({"error": str(e)}), 500
|
| 1444 |
|
| 1445 |
if __name__ == '__main__':
|
| 1446 |
import json
|
| 1447 |
-
logging.info(f"环境变量:{os.environ}"
|
| 1448 |
|
| 1449 |
invalid_keys_global = []
|
| 1450 |
free_keys_global = []
|
|
@@ -1452,14 +1433,14 @@ if __name__ == '__main__':
|
|
| 1452 |
valid_keys_global = []
|
| 1453 |
|
| 1454 |
load_keys()
|
| 1455 |
-
logging.info("程序启动时首次加载 keys 已执行"
|
| 1456 |
|
| 1457 |
scheduler.start()
|
| 1458 |
|
| 1459 |
-
logging.info("首次加载 keys 已手动触发执行"
|
| 1460 |
|
| 1461 |
refresh_models()
|
| 1462 |
-
logging.info("首次刷新模型列表已手动触发执行"
|
| 1463 |
|
| 1464 |
app.run(
|
| 1465 |
debug=False,
|
|
|
|
| 18 |
time.tzset()
|
| 19 |
|
| 20 |
logging.basicConfig(level=logging.INFO,
|
| 21 |
+
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 22 |
|
| 23 |
API_ENDPOINT = "https://api.siliconflow.cn/v1/user/info"
|
| 24 |
TEST_MODEL_ENDPOINT = "https://api.siliconflow.cn/v1/chat/completions"
|
|
|
|
| 46 |
token_counts = []
|
| 47 |
data_lock = threading.Lock()
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def get_credit_summary(api_key):
|
| 50 |
"""
|
| 51 |
使用 API 密钥获取额度信息。
|
|
|
|
| 543 |
)
|
| 544 |
except Exception as exc:
|
| 545 |
logging.error(
|
| 546 |
+
f"处理 Token {token} 生成异常: {exc}"
|
|
|
|
| 547 |
)
|
| 548 |
|
| 549 |
return jsonify(results)
|
|
|
|
| 773 |
except (KeyError, ValueError, IndexError) as e:
|
| 774 |
logging.error(
|
| 775 |
f"解析响应 JSON 失败: {e}, "
|
| 776 |
+
f"完整内容: {response_json}"
|
|
|
|
| 777 |
)
|
| 778 |
prompt_tokens = 0
|
| 779 |
embedding_data = []
|
|
|
|
| 782 |
f"使用的key: {api_key}, "
|
| 783 |
f"提示token: {prompt_tokens}, "
|
| 784 |
f"总共用时: {total_time:.4f}秒, "
|
| 785 |
+
f"使用的模型: {model_name}"
|
|
|
|
| 786 |
)
|
| 787 |
|
| 788 |
with data_lock:
|
|
|
|
| 936 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 937 |
openai_images.append({"b64_json": img_str})
|
| 938 |
except Exception as e:
|
| 939 |
+
logging.error(f"图片转base64失败: {e}")
|
| 940 |
openai_images.append({"url": image_url})
|
| 941 |
else:
|
| 942 |
openai_images.append({"url": image_url})
|
| 943 |
else:
|
| 944 |
+
logging.error(f"无效的图片数据: {item}")
|
| 945 |
openai_images.append({"url": item})
|
| 946 |
|
| 947 |
|
|
|
|
| 952 |
except (KeyError, ValueError, IndexError) as e:
|
| 953 |
logging.error(
|
| 954 |
f"解析响应 JSON 失败: {e}, "
|
| 955 |
+
f"完整内容: {response_json}"
|
|
|
|
| 956 |
)
|
| 957 |
response_data = {
|
| 958 |
"created": int(time.time()),
|
|
|
|
| 962 |
logging.info(
|
| 963 |
f"使用的key: {api_key}, "
|
| 964 |
f"总共用时: {total_time:.4f}秒, "
|
| 965 |
+
f"使用的模型: {model_name}"
|
|
|
|
| 966 |
)
|
| 967 |
|
| 968 |
with data_lock:
|
|
|
|
| 972 |
return jsonify(response_data)
|
| 973 |
|
| 974 |
except requests.exceptions.RequestException as e:
|
| 975 |
+
logging.error(f"请求转发异常: {e}")
|
| 976 |
return jsonify({"error": str(e)}), 500
|
| 977 |
else:
|
| 978 |
return jsonify({"error": "Unsupported model"}), 400
|
|
|
|
| 1130 |
image_url = ""
|
| 1131 |
if images and isinstance(images[0], dict) and "url" in images[0]:
|
| 1132 |
image_url = images[0]["url"]
|
| 1133 |
+
logging.info(f"Extracted image URL: {image_url}")
|
| 1134 |
elif images and isinstance(images[0], str):
|
| 1135 |
image_url = images[0]
|
| 1136 |
+
logging.info(f"Extracted image URL: {image_url}")
|
| 1137 |
|
| 1138 |
markdown_image_link = f""
|
| 1139 |
if image_url:
|
|
|
|
| 1193 |
request_timestamps.append(time.time())
|
| 1194 |
token_counts.append(0)
|
| 1195 |
except requests.exceptions.RequestException as e:
|
| 1196 |
+
logging.error(f"请求转发异常: {e}")
|
| 1197 |
error_chunk_data = {
|
| 1198 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 1199 |
"object": "chat.completion.chunk",
|
|
|
|
| 1227 |
yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
|
| 1228 |
logging.info(
|
| 1229 |
f"使用的key: {api_key}, "
|
| 1230 |
+
f"使用的模型: {model_name}"
|
|
|
|
| 1231 |
)
|
| 1232 |
yield "data: [DONE]\n\n".encode('utf-8')
|
| 1233 |
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
|
|
|
| 1244 |
image_url = ""
|
| 1245 |
if images and isinstance(images[0], dict) and "url" in images[0]:
|
| 1246 |
image_url = images[0]["url"]
|
| 1247 |
+
logging.info(f"Extracted image URL: {image_url}")
|
| 1248 |
elif images and isinstance(images[0], str):
|
| 1249 |
image_url = images[0]
|
| 1250 |
+
logging.info(f"Extracted image URL: {image_url}")
|
| 1251 |
|
| 1252 |
markdown_image_link = f""
|
| 1253 |
response_data = {
|
|
|
|
| 1269 |
except (KeyError, ValueError, IndexError) as e:
|
| 1270 |
logging.error(
|
| 1271 |
f"解析响应 JSON 失败: {e}, "
|
| 1272 |
+
f"完整内容: {response_json}"
|
|
|
|
| 1273 |
)
|
| 1274 |
response_data = {
|
| 1275 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
|
|
| 1291 |
logging.info(
|
| 1292 |
f"使用的key: {api_key}, "
|
| 1293 |
f"总共用时: {total_time:.4f}秒, "
|
| 1294 |
+
f"使用的模型: {model_name}"
|
|
|
|
| 1295 |
)
|
| 1296 |
with data_lock:
|
| 1297 |
request_timestamps.append(time.time())
|
|
|
|
| 1299 |
return jsonify(response_data)
|
| 1300 |
|
| 1301 |
except requests.exceptions.RequestException as e:
|
| 1302 |
+
logging.error(f"请求转发异常: {e}")
|
| 1303 |
return jsonify({"error": str(e)}), 500
|
| 1304 |
else:
|
| 1305 |
try:
|
|
|
|
| 1324 |
chunk = chunk.decode('utf-8')
|
| 1325 |
yield f"{chunk}\n\n".encode('utf-8')
|
| 1326 |
except requests.exceptions.RequestException as e:
|
| 1327 |
+
logging.error(f"请求转发异常: {e}")
|
| 1328 |
error_chunk_data = {
|
| 1329 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 1330 |
"object": "chat.completion.chunk",
|
|
|
|
| 1391 |
except (KeyError, ValueError, IndexError) as e:
|
| 1392 |
logging.error(
|
| 1393 |
f"解析响应 JSON 失败: {e}, "
|
| 1394 |
+
f"完整内容: {response_json}"
|
|
|
|
| 1395 |
)
|
| 1396 |
response_data = {
|
| 1397 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
|
|
| 1413 |
logging.info(
|
| 1414 |
f"使用的key: {api_key}, "
|
| 1415 |
f"总共用时: {total_time:.4f}秒, "
|
| 1416 |
+
f"使用的模型: {model_name}"
|
|
|
|
| 1417 |
)
|
| 1418 |
with data_lock:
|
| 1419 |
request_timestamps.append(time.time())
|
| 1420 |
token_counts.append(0)
|
| 1421 |
return jsonify(response_data)
|
| 1422 |
except requests.exceptions.RequestException as e:
|
| 1423 |
+
logging.error(f"请求转发异常: {e}")
|
| 1424 |
return jsonify({"error": str(e)}), 500
|
| 1425 |
|
| 1426 |
if __name__ == '__main__':
|
| 1427 |
import json
|
| 1428 |
+
logging.info(f"环境变量:{os.environ}")
|
| 1429 |
|
| 1430 |
invalid_keys_global = []
|
| 1431 |
free_keys_global = []
|
|
|
|
| 1433 |
valid_keys_global = []
|
| 1434 |
|
| 1435 |
load_keys()
|
| 1436 |
+
logging.info("程序启动时首次加载 keys 已执行")
|
| 1437 |
|
| 1438 |
scheduler.start()
|
| 1439 |
|
| 1440 |
+
logging.info("首次加载 keys 已手动触发执行")
|
| 1441 |
|
| 1442 |
refresh_models()
|
| 1443 |
+
logging.info("首次刷新模型列表已手动触发执行")
|
| 1444 |
|
| 1445 |
app.run(
|
| 1446 |
debug=False,
|