yangtb24 commited on
Commit
470a33a
·
verified ·
1 Parent(s): 485c6ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +204 -341
app.py CHANGED
@@ -944,13 +944,13 @@ def handsome_chat_completions():
944
  return jsonify({"error": "Invalid request data"}), 400
945
 
946
  model_name = data['model']
947
-
948
  request_type = determine_request_type(
949
  model_name,
950
  text_models + image_models,
951
  free_text_models + free_image_models
952
  )
953
-
954
  api_key = select_key(request_type, model_name)
955
 
956
  if not api_key:
@@ -968,7 +968,7 @@ def handsome_chat_completions():
968
  "Authorization": f"Bearer {api_key}",
969
  "Content-Type": "application/json"
970
  }
971
-
972
  if model_name in image_models:
973
  user_content = ""
974
  messages = data.get("messages", [])
@@ -991,6 +991,7 @@ def handsome_chat_completions():
991
  siliconflow_data = {
992
  "model": model_name,
993
  "prompt": user_content,
 
994
  }
995
  if model_name == "black-forest-labs/FLUX.1-pro":
996
  siliconflow_data["width"] = data.get("width", 1024)
@@ -1004,26 +1005,27 @@ def handsome_chat_completions():
1004
  siliconflow_data["output_format"] = data.get("output_format", "png")
1005
  seed = data.get("seed")
1006
  if isinstance(seed, int) and 0 < seed < 9999999999:
1007
- siliconflow_data["seed"] = seed
1008
  if siliconflow_data["width"] < 256 or siliconflow_data["width"] > 1440 or siliconflow_data["width"] % 32 != 0:
1009
- siliconflow_data["width"] = 1024
1010
  if siliconflow_data["height"] < 256 or siliconflow_data["height"] > 1440 or siliconflow_data["height"] % 32 != 0:
1011
  siliconflow_data["height"] = 768
 
1012
  if siliconflow_data["steps"] < 1 or siliconflow_data["steps"] > 50:
1013
- siliconflow_data["steps"] = 20
1014
  if siliconflow_data["guidance"] < 1.5 or siliconflow_data["guidance"] > 5:
1015
- siliconflow_data["guidance"] = 3
1016
  if siliconflow_data["safety_tolerance"] < 0 or siliconflow_data["safety_tolerance"] > 6:
1017
  siliconflow_data["safety_tolerance"] = 2
1018
  if siliconflow_data["interval"] < 1 or siliconflow_data["interval"] > 4 :
1019
- siliconflow_data["interval"] = 2
1020
  else:
1021
  siliconflow_data["image_size"] = "1024x1024"
1022
  siliconflow_data["batch_size"] = 1
1023
  siliconflow_data["num_inference_steps"] = 20
1024
  siliconflow_data["guidance_scale"] = 7.5
1025
  siliconflow_data["prompt_enhancement"] = False
1026
-
1027
  if data.get("size"):
1028
  siliconflow_data["image_size"] = data.get("size")
1029
  if data.get("n"):
@@ -1031,66 +1033,106 @@ def handsome_chat_completions():
1031
  if data.get("steps"):
1032
  siliconflow_data["num_inference_steps"] = data.get("steps")
1033
  if data.get("guidance_scale"):
1034
- siliconflow_data["guidance_scale"] = data.get("guidance_scale")
1035
  if data.get("negative_prompt"):
1036
- siliconflow_data["negative_prompt"] = data.get("negative_prompt")
1037
  if data.get("seed"):
1038
- siliconflow_data["seed"] = data.get("seed")
1039
  if data.get("prompt_enhancement"):
1040
- siliconflow_data["prompt_enhancement"] = data.get("prompt_enhancement")
 
1041
  if siliconflow_data["batch_size"] < 1:
1042
- siliconflow_data["batch_size"] = 1
1043
  if siliconflow_data["batch_size"] > 4:
1044
- siliconflow_data["batch_size"] = 4
1045
 
1046
  if siliconflow_data["num_inference_steps"] < 1:
1047
  siliconflow_data["num_inference_steps"] = 1
1048
  if siliconflow_data["num_inference_steps"] > 50:
1049
- siliconflow_data["num_inference_steps"] = 50
1050
-
1051
  if siliconflow_data["guidance_scale"] < 0:
1052
- siliconflow_data["guidance_scale"] = 0
1053
  if siliconflow_data["guidance_scale"] > 100:
1054
- siliconflow_data["guidance_scale"] = 100
1055
-
1056
  if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024", "960x1280", "720x1440", "720x1280"]:
1057
  siliconflow_data["image_size"] = "1024x1024"
1058
 
1059
  try:
1060
- start_time = time.time()
1061
- response = requests.post(
1062
- "https://api-st.siliconflow.cn/v1/images/generations",
1063
  headers=headers,
1064
  json=siliconflow_data,
1065
  timeout=120,
1066
  stream=data.get("stream", False)
1067
- )
1068
- if response.status_code == 429:
1069
- return jsonify(response.json()), 429
 
1070
 
1071
- if data.get("stream", False):
1072
  def generate():
1073
- first_chunk_time = None
1074
- full_response_content = ""
1075
- try:
1076
- response.raise_for_status()
1077
- end_time = time.time()
1078
- response_json = response.json()
1079
- total_time = end_time - start_time
1080
-
1081
- images = response_json.get("images", [])
1082
-
1083
- image_url = ""
1084
- if images and isinstance(images[0], dict) and "url" in images[0]:
1085
- image_url = images[0]["url"]
1086
- logging.info(f"Extracted image URL: {image_url}")
1087
- elif images and isinstance(images[0], str):
1088
- image_url = images[0]
1089
- logging.info(f"Extracted image URL: {image_url}")
1090
-
1091
- markdown_image_link = f"![image]({image_url})"
1092
- if image_url:
1093
- chunk_data = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1094
  "id": f"chatcmpl-{uuid.uuid4()}",
1095
  "object": "chat.completion.chunk",
1096
  "created": int(time.time()),
@@ -1098,18 +1140,18 @@ def handsome_chat_completions():
1098
  "choices": [
1099
  {
1100
  "index": 0,
1101
- "delta": {
1102
- "role": "assistant",
1103
- "content": markdown_image_link
1104
- },
1105
- "finish_reason": None
1106
  }
1107
  ]
1108
  }
1109
- yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
1110
- full_response_content = markdown_image_link
1111
- else:
1112
- chunk_data = {
 
 
 
1113
  "id": f"chatcmpl-{uuid.uuid4()}",
1114
  "object": "chat.completion.chunk",
1115
  "created": int(time.time()),
@@ -1119,168 +1161,110 @@ def handsome_chat_completions():
1119
  "index": 0,
1120
  "delta": {
1121
  "role": "assistant",
1122
- "content": "Failed to generate image"
1123
  },
1124
  "finish_reason": None
1125
  }
1126
  ]
1127
  }
1128
- yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
1129
- full_response_content = "Failed to generate image"
1130
-
1131
- end_chunk_data = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1132
  "id": f"chatcmpl-{uuid.uuid4()}",
1133
- "object": "chat.completion.chunk",
1134
  "created": int(time.time()),
1135
  "model": model_name,
1136
  "choices": [
1137
  {
1138
- "index": 0,
1139
- "delta": {},
1140
- "finish_reason": "stop"
 
 
 
1141
  }
1142
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1143
  }
1144
- yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
1145
- with data_lock:
1146
- request_timestamps.append(time.time())
1147
- token_counts.append(0)
1148
- except requests.exceptions.RequestException as e:
1149
- logging.error(f"请求转发异常: {e}")
1150
- error_chunk_data = {
1151
- "id": f"chatcmpl-{uuid.uuid4()}",
1152
- "object": "chat.completion.chunk",
1153
- "created": int(time.time()),
1154
- "model": model_name,
1155
- "choices": [
1156
- {
1157
- "index": 0,
1158
- "delta": {
1159
- "role": "assistant",
1160
- "content": f"Error: {str(e)}"
1161
- },
1162
- "finish_reason": None
1163
- }
1164
- ]
1165
- }
1166
- yield f"data: {json.dumps(error_chunk_data)}\n\n".encode('utf-8')
1167
- end_chunk_data = {
1168
- "id": f"chatcmpl-{uuid.uuid4()}",
1169
- "object": "chat.completion.chunk",
1170
- "created": int(time.time()),
1171
- "model": model_name,
1172
- "choices": [
1173
- {
1174
- "index": 0,
1175
- "delta": {},
1176
- "finish_reason": "stop"
1177
- }
1178
- ]
1179
- }
1180
- yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
1181
- logging.info(
1182
- f"使用的key: {api_key}, "
1183
- f"使用的模型: {model_name}"
1184
- )
1185
- yield "data: [DONE]\n\n".encode('utf-8')
1186
- return Response(stream_with_context(generate()), content_type='text/event-stream')
1187
 
1188
- else:
1189
- response.raise_for_status()
1190
- end_time = time.time()
1191
- response_json = response.json()
1192
- total_time = end_time - start_time
1193
-
1194
- try:
1195
- images = response_json.get("images", [])
1196
-
1197
- image_url = ""
1198
- if images and isinstance(images[0], dict) and "url" in images[0]:
1199
- image_url = images[0]["url"]
1200
- logging.info(f"Extracted image URL: {image_url}")
1201
- elif images and isinstance(images[0], str):
1202
- image_url = images[0]
1203
- logging.info(f"Extracted image URL: {image_url}")
1204
-
1205
- markdown_image_link = f"![image]({image_url})"
1206
- response_data = {
1207
- "id": f"chatcmpl-{uuid.uuid4()}",
1208
- "object": "chat.completion",
1209
- "created": int(time.time()),
1210
- "model": model_name,
1211
- "choices": [
1212
- {
1213
- "index": 0,
1214
- "message": {
1215
- "role": "assistant",
1216
- "content": markdown_image_link if image_url else "Failed to generate image",
1217
- },
1218
- "finish_reason": "stop",
1219
- }
1220
- ],
1221
- }
1222
- except (KeyError, ValueError, IndexError) as e:
1223
- logging.error(
1224
- f"解析响应 JSON 失败: {e}, "
1225
- f"完整内容: {response_json}"
1226
- )
1227
- response_data = {
1228
- "id": f"chatcmpl-{uuid.uuid4()}",
1229
- "object": "chat.completion",
1230
- "created": int(time.time()),
1231
- "model": model_name,
1232
- "choices": [
1233
- {
1234
- "index": 0,
1235
- "message": {
1236
- "role": "assistant",
1237
- "content": "Failed to process image data",
1238
- },
1239
- "finish_reason": "stop",
1240
- }
1241
- ],
1242
- }
1243
-
1244
- logging.info(
1245
  f"使用的key: {api_key}, "
1246
  f"总共用时: {total_time:.4f}秒, "
1247
  f"使用的模型: {model_name}"
1248
- )
1249
- with data_lock:
1250
  request_timestamps.append(time.time())
1251
  token_counts.append(0)
1252
- return jsonify(response_data)
1253
 
1254
  except requests.exceptions.RequestException as e:
1255
- logging.error(f"请求转发异常: {e}")
1256
- return jsonify({"error": str(e)}), 500
1257
  else:
1258
- tools = data.get("tools")
1259
- tool_choice = data.get("tool_choice")
1260
-
1261
- siliconflow_data = {
1262
- "model": model_name,
1263
- "messages": data.get("messages", []),
1264
- "stream": data.get("stream", False),
1265
- "max_tokens": data.get("max_tokens"),
1266
- "temperature": data.get("temperature"),
1267
- "top_p": data.get("top_p"),
1268
- "frequency_penalty": data.get("frequency_penalty"),
1269
- "presence_penalty": data.get("presence_penalty"),
1270
- "stop": data.get("stop"),
1271
- }
1272
-
1273
- if tools:
1274
- siliconflow_data["tools"] = tools
1275
- if tool_choice:
1276
- siliconflow_data["tool_choice"] = tool_choice
1277
-
1278
  try:
1279
  start_time = time.time()
1280
  response = requests.post(
1281
  TEST_MODEL_ENDPOINT,
1282
  headers=headers,
1283
- json=siliconflow_data,
1284
  stream=data.get("stream", False),
1285
  timeout=60
1286
  )
@@ -1309,16 +1293,14 @@ def handsome_chat_completions():
1309
  prompt_tokens = 0
1310
  completion_tokens = 0
1311
  response_content = ""
1312
- function_call = None
1313
- tool_calls = []
1314
-
1315
  for line in full_response_content.splitlines():
1316
  if line.startswith("data:"):
1317
- line = line[5:].strip()
1318
- if line == "[DONE]":
1319
  continue
1320
- try:
1321
  response_json = json.loads(line)
 
1322
  if (
1323
  "usage" in response_json and
1324
  "completion_tokens" in response_json["usage"]
@@ -1329,21 +1311,15 @@ def handsome_chat_completions():
1329
 
1330
  if (
1331
  "choices" in response_json and
1332
- len(response_json["choices"]) > 0
 
 
 
 
1333
  ):
1334
- choice = response_json["choices"][0]
1335
- if "delta" in choice:
1336
- delta = choice["delta"]
1337
- if "content" in delta:
1338
- response_content += delta["content"]
1339
- if "tool_calls" in delta:
1340
- tool_calls.extend(delta["tool_calls"])
1341
- elif "message" in choice:
1342
- message = choice["message"]
1343
- if "content" in message and message["content"]:
1344
- response_content += message["content"]
1345
- if "tool_calls" in message:
1346
- tool_calls.extend(message["tool_calls"])
1347
 
1348
  if (
1349
  "usage" in response_json and
@@ -1353,24 +1329,23 @@ def handsome_chat_completions():
1353
  "usage"
1354
  ]["prompt_tokens"]
1355
 
1356
-
1357
- except (
1358
  KeyError,
1359
  ValueError,
1360
  IndexError
1361
- ) as e:
1362
- logging.error(
1363
  f"解析流式响应单行 JSON 失败: {e}, "
1364
  f"行内容: {line}"
1365
- )
1366
 
1367
  user_content = ""
1368
  messages = data.get("messages", [])
1369
  for message in messages:
1370
- if message["role"] == "user":
1371
- if isinstance(message["content"], str):
1372
  user_content += message["content"] + " "
1373
- elif isinstance(message["content"], list):
1374
  for item in message["content"]:
1375
  if (
1376
  isinstance(item, dict) and
@@ -1382,14 +1357,15 @@ def handsome_chat_completions():
1382
  )
1383
 
1384
  user_content = user_content.strip()
 
1385
  user_content_replaced = user_content.replace(
1386
  '\n', '\\n'
1387
  ).replace('\r', '\\n')
1388
  response_content_replaced = response_content.replace(
1389
  '\n', '\\n'
1390
  ).replace('\r', '\\n')
1391
-
1392
- log_message = (
1393
  f"使用的key: {api_key}, "
1394
  f"提示token: {prompt_tokens}, "
1395
  f"输出token: {completion_tokens}, "
@@ -1399,73 +1375,11 @@ def handsome_chat_completions():
1399
  f"用户的内容: {user_content_replaced}, "
1400
  f"输出的内容: {response_content_replaced}"
1401
  )
1402
-
1403
- if tool_calls:
1404
- log_message += f", tool_calls: {tool_calls}"
1405
-
1406
- logging.info(log_message)
1407
 
1408
  with data_lock:
1409
  request_timestamps.append(time.time())
1410
  token_counts.append(prompt_tokens+completion_tokens)
1411
-
1412
- # 构造 OpenAI 格式的响应数据
1413
- response_data = {
1414
- "id": f"chatcmpl-{uuid.uuid4()}",
1415
- "object": "chat.completion.chunk",
1416
- "created": int(time.time()),
1417
- "model": model_name,
1418
- "choices": [
1419
- {
1420
- "index": 0,
1421
- "delta": {
1422
- "role": "assistant",
1423
- },
1424
- "finish_reason": None
1425
- }
1426
- ]
1427
- }
1428
-
1429
- if tool_calls:
1430
- if isinstance(tool_calls, list) and len(tool_calls) > 0:
1431
-
1432
- first_tool_call = tool_calls[0]
1433
- if isinstance(first_tool_call, dict) and "function" in first_tool_call:
1434
- function_call_data = first_tool_call.get("function")
1435
- if isinstance(function_call_data, dict) and "name" in function_call_data and "arguments" in function_call_data:
1436
- function_call = {
1437
- "name": function_call_data["name"],
1438
- "arguments": json.dumps(function_call_data["arguments"]) if isinstance(function_call_data.get("arguments"), dict) else function_call_data["arguments"]
1439
- }
1440
- response_data["choices"][0]["delta"]["function_call"] = function_call
1441
- response_data["choices"][0]["delta"]["content"] = None
1442
- response_data["choices"][0]["finish_reason"] = "function_call"
1443
- else:
1444
- response_data["choices"][0]["delta"]["tool_calls"] = tool_calls
1445
- response_data["choices"][0]["delta"]["content"] = None
1446
- else:
1447
- response_data["choices"][0]["delta"]["tool_calls"] = tool_calls
1448
- response_data["choices"][0]["delta"]["content"] = None
1449
- elif response_content:
1450
- response_data["choices"][0]["delta"]["content"] = response_content
1451
-
1452
-
1453
- yield f"data: {json.dumps(response_data)}\n\n".encode('utf-8')
1454
-
1455
- end_chunk_data = {
1456
- "id": f"chatcmpl-{uuid.uuid4()}",
1457
- "object": "chat.completion.chunk",
1458
- "created": int(time.time()),
1459
- "model": model_name,
1460
- "choices": [
1461
- {
1462
- "index": 0,
1463
- "delta": {},
1464
- "finish_reason": "stop"
1465
- }
1466
- ]
1467
- }
1468
- yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
1469
  return Response(
1470
  stream_with_context(generate()),
1471
  content_type=response.headers['Content-Type']
@@ -1484,10 +1398,6 @@ def handsome_chat_completions():
1484
  response_content = response_json[
1485
  "choices"
1486
  ][0]["message"]["content"]
1487
- if "tool_calls" in response_json["choices"][0]["message"]:
1488
- tool_calls = response_json["choices"][0]["message"]["tool_calls"]
1489
- else:
1490
- tool_calls = []
1491
  except (KeyError, ValueError, IndexError) as e:
1492
  logging.error(
1493
  f"解析非流式响应 JSON 失败: {e}, "
@@ -1496,7 +1406,6 @@ def handsome_chat_completions():
1496
  prompt_tokens = 0
1497
  completion_tokens = 0
1498
  response_content = ""
1499
- tool_calls = []
1500
 
1501
  user_content = ""
1502
  messages = data.get("messages", [])
@@ -1511,8 +1420,7 @@ def handsome_chat_completions():
1511
  item.get("type") == "text"
1512
  ):
1513
  user_content += (
1514
- item.get("text", "") +
1515
- " "
1516
  )
1517
 
1518
  user_content = user_content.strip()
@@ -1524,8 +1432,8 @@ def handsome_chat_completions():
1524
  '\n', '\\n'
1525
  ).replace('\r', '\\n')
1526
 
1527
- log_message = (
1528
- f"使用的key: {api_key}, "
1529
  f"提示token: {prompt_tokens}, "
1530
  f"输出token: {completion_tokens}, "
1531
  f"首字用时: 0, "
@@ -1534,59 +1442,14 @@ def handsome_chat_completions():
1534
  f"用户的内容: {user_content_replaced}, "
1535
  f"输出的内容: {response_content_replaced}"
1536
  )
1537
- if tool_calls:
1538
- log_message += f", tool_calls: {tool_calls}"
1539
-
1540
- logging.info(log_message)
1541
-
1542
  with data_lock:
1543
  request_timestamps.append(time.time())
1544
  if "prompt_tokens" in response_json["usage"] and "completion_tokens" in response_json["usage"]:
1545
  token_counts.append(response_json["usage"]["prompt_tokens"] + response_json["usage"]["completion_tokens"])
1546
  else:
1547
  token_counts.append(0)
1548
-
1549
- # 构造 OpenAI 格式的响应数据
1550
- response_data = {
1551
- "id": f"chatcmpl-{uuid.uuid4()}",
1552
- "object": "chat.completion",
1553
- "created": int(time.time()),
1554
- "model": model_name,
1555
- "choices": [
1556
- {
1557
- "index": 0,
1558
- "message": {
1559
- "role": "assistant",
1560
- "content": response_content,
1561
-
1562
- },
1563
- "finish_reason": "stop",
1564
- }
1565
- ],
1566
- }
1567
- if tool_calls:
1568
- if isinstance(tool_calls, list) and len(tool_calls) > 0:
1569
- first_tool_call = tool_calls[0]
1570
- if isinstance(first_tool_call, dict) and "function" in first_tool_call:
1571
- function_call_data = first_tool_call.get("function")
1572
- if isinstance(function_call_data, dict) and "name" in function_call_data and "arguments" in function_call_data:
1573
- function_call = {
1574
- "name": function_call_data["name"],
1575
- "arguments": json.dumps(function_call_data["arguments"]) if isinstance(function_call_data.get("arguments"), dict) else function_call_data["arguments"]
1576
- }
1577
- response_data["choices"][0]["message"]["function_call"] = function_call
1578
- response_data["choices"][0]["message"]["content"] = None
1579
- response_data["choices"][0]["finish_reason"] = "function_call"
1580
- else:
1581
- response_data["choices"][0]["message"]["tool_calls"] = tool_calls
1582
- response_data["choices"][0]["message"]["content"] = None
1583
- else:
1584
- response_data["choices"][0]["message"]["tool_calls"] = tool_calls
1585
- response_data["choices"][0]["message"]["content"] = None
1586
-
1587
-
1588
- return jsonify(response_data)
1589
 
 
1590
 
1591
  except requests.exceptions.RequestException as e:
1592
  logging.error(f"请求转发异常: {e}")
 
944
  return jsonify({"error": "Invalid request data"}), 400
945
 
946
  model_name = data['model']
947
+
948
  request_type = determine_request_type(
949
  model_name,
950
  text_models + image_models,
951
  free_text_models + free_image_models
952
  )
953
+
954
  api_key = select_key(request_type, model_name)
955
 
956
  if not api_key:
 
968
  "Authorization": f"Bearer {api_key}",
969
  "Content-Type": "application/json"
970
  }
971
+
972
  if model_name in image_models:
973
  user_content = ""
974
  messages = data.get("messages", [])
 
991
  siliconflow_data = {
992
  "model": model_name,
993
  "prompt": user_content,
994
+
995
  }
996
  if model_name == "black-forest-labs/FLUX.1-pro":
997
  siliconflow_data["width"] = data.get("width", 1024)
 
1005
  siliconflow_data["output_format"] = data.get("output_format", "png")
1006
  seed = data.get("seed")
1007
  if isinstance(seed, int) and 0 < seed < 9999999999:
1008
+ siliconflow_data["seed"] = seed
1009
  if siliconflow_data["width"] < 256 or siliconflow_data["width"] > 1440 or siliconflow_data["width"] % 32 != 0:
1010
+ siliconflow_data["width"] = 1024
1011
  if siliconflow_data["height"] < 256 or siliconflow_data["height"] > 1440 or siliconflow_data["height"] % 32 != 0:
1012
  siliconflow_data["height"] = 768
1013
+
1014
  if siliconflow_data["steps"] < 1 or siliconflow_data["steps"] > 50:
1015
+ siliconflow_data["steps"] = 20
1016
  if siliconflow_data["guidance"] < 1.5 or siliconflow_data["guidance"] > 5:
1017
+ siliconflow_data["guidance"] = 3
1018
  if siliconflow_data["safety_tolerance"] < 0 or siliconflow_data["safety_tolerance"] > 6:
1019
  siliconflow_data["safety_tolerance"] = 2
1020
  if siliconflow_data["interval"] < 1 or siliconflow_data["interval"] > 4 :
1021
+ siliconflow_data["interval"] = 2
1022
  else:
1023
  siliconflow_data["image_size"] = "1024x1024"
1024
  siliconflow_data["batch_size"] = 1
1025
  siliconflow_data["num_inference_steps"] = 20
1026
  siliconflow_data["guidance_scale"] = 7.5
1027
  siliconflow_data["prompt_enhancement"] = False
1028
+
1029
  if data.get("size"):
1030
  siliconflow_data["image_size"] = data.get("size")
1031
  if data.get("n"):
 
1033
  if data.get("steps"):
1034
  siliconflow_data["num_inference_steps"] = data.get("steps")
1035
  if data.get("guidance_scale"):
1036
+ siliconflow_data["guidance_scale"] = data.get("guidance_scale")
1037
  if data.get("negative_prompt"):
1038
+ siliconflow_data["negative_prompt"] = data.get("negative_prompt")
1039
  if data.get("seed"):
1040
+ siliconflow_data["seed"] = data.get("seed")
1041
  if data.get("prompt_enhancement"):
1042
+ siliconflow_data["prompt_enhancement"] = data.get("prompt_enhancement")
1043
+
1044
  if siliconflow_data["batch_size"] < 1:
1045
+ siliconflow_data["batch_size"] = 1
1046
  if siliconflow_data["batch_size"] > 4:
1047
+ siliconflow_data["batch_size"] = 4
1048
 
1049
  if siliconflow_data["num_inference_steps"] < 1:
1050
  siliconflow_data["num_inference_steps"] = 1
1051
  if siliconflow_data["num_inference_steps"] > 50:
1052
+ siliconflow_data["num_inference_steps"] = 50
1053
+
1054
  if siliconflow_data["guidance_scale"] < 0:
1055
+ siliconflow_data["guidance_scale"] = 0
1056
  if siliconflow_data["guidance_scale"] > 100:
1057
+ siliconflow_data["guidance_scale"] = 100
1058
+
1059
  if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024", "960x1280", "720x1440", "720x1280"]:
1060
  siliconflow_data["image_size"] = "1024x1024"
1061
 
1062
  try:
1063
+ start_time = time.time()
1064
+ response = requests.post(
1065
+ "https://api.siliconflow.cn/v1/images/generations",
1066
  headers=headers,
1067
  json=siliconflow_data,
1068
  timeout=120,
1069
  stream=data.get("stream", False)
1070
+ )
1071
+
1072
+ if response.status_code == 429:
1073
+ return jsonify(response.json()), 429
1074
 
1075
+ if data.get("stream", False):
1076
  def generate():
1077
+ first_chunk_time = None
1078
+ full_response_content = ""
1079
+ try:
1080
+ response.raise_for_status()
1081
+ end_time = time.time()
1082
+ response_json = response.json()
1083
+ total_time = end_time - start_time
1084
+
1085
+ images = response_json.get("images", [])
1086
+
1087
+ image_url = ""
1088
+ if images and isinstance(images[0], dict) and "url" in images[0]:
1089
+ image_url = images[0]["url"]
1090
+ logging.info(f"Extracted image URL: {image_url}")
1091
+ elif images and isinstance(images[0], str):
1092
+ image_url = images[0]
1093
+ logging.info(f"Extracted image URL: {image_url}")
1094
+
1095
+ markdown_image_link = f"![image]({image_url})"
1096
+ if image_url:
1097
+ chunk_data = {
1098
+ "id": f"chatcmpl-{uuid.uuid4()}",
1099
+ "object": "chat.completion.chunk",
1100
+ "created": int(time.time()),
1101
+ "model": model_name,
1102
+ "choices": [
1103
+ {
1104
+ "index": 0,
1105
+ "delta": {
1106
+ "role": "assistant",
1107
+ "content": markdown_image_link
1108
+ },
1109
+ "finish_reason": None
1110
+ }
1111
+ ]
1112
+ }
1113
+ yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
1114
+ full_response_content = markdown_image_link
1115
+ else:
1116
+ chunk_data = {
1117
+ "id": f"chatcmpl-{uuid.uuid4()}",
1118
+ "object": "chat.completion.chunk",
1119
+ "created": int(time.time()),
1120
+ "model": model_name,
1121
+ "choices": [
1122
+ {
1123
+ "index": 0,
1124
+ "delta": {
1125
+ "role": "assistant",
1126
+ "content": "Failed to generate image"
1127
+ },
1128
+ "finish_reason": None
1129
+ }
1130
+ ]
1131
+ }
1132
+ yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
1133
+ full_response_content = "Failed to generate image"
1134
+
1135
+ end_chunk_data = {
1136
  "id": f"chatcmpl-{uuid.uuid4()}",
1137
  "object": "chat.completion.chunk",
1138
  "created": int(time.time()),
 
1140
  "choices": [
1141
  {
1142
  "index": 0,
1143
+ "delta": {},
1144
+ "finish_reason": "stop"
 
 
 
1145
  }
1146
  ]
1147
  }
1148
+ yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
1149
+ with data_lock:
1150
+ request_timestamps.append(time.time())
1151
+ token_counts.append(0)
1152
+ except requests.exceptions.RequestException as e:
1153
+ logging.error(f"请求转发异常: {e}")
1154
+ error_chunk_data = {
1155
  "id": f"chatcmpl-{uuid.uuid4()}",
1156
  "object": "chat.completion.chunk",
1157
  "created": int(time.time()),
 
1161
  "index": 0,
1162
  "delta": {
1163
  "role": "assistant",
1164
+ "content": f"Error: {str(e)}"
1165
  },
1166
  "finish_reason": None
1167
  }
1168
  ]
1169
  }
1170
+ yield f"data: {json.dumps(error_chunk_data)}\n\n".encode('utf-8')
1171
+ end_chunk_data = {
1172
+ "id": f"chatcmpl-{uuid.uuid4()}",
1173
+ "object": "chat.completion.chunk",
1174
+ "created": int(time.time()),
1175
+ "model": model_name,
1176
+ "choices": [
1177
+ {
1178
+ "index": 0,
1179
+ "delta": {},
1180
+ "finish_reason": "stop"
1181
+ }
1182
+ ]
1183
+ }
1184
+ yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
1185
+ logging.info(
1186
+ f"使用的key: {api_key}, "
1187
+ f"使用的模型: {model_name}"
1188
+ )
1189
+ yield "data: [DONE]\n\n".encode('utf-8')
1190
+ return Response(stream_with_context(generate()), content_type='text/event-stream')
1191
+
1192
+ else:
1193
+ response.raise_for_status()
1194
+ end_time = time.time()
1195
+ response_json = response.json()
1196
+ total_time = end_time - start_time
1197
+
1198
+ try:
1199
+ images = response_json.get("images", [])
1200
+
1201
+ image_url = ""
1202
+ if images and isinstance(images[0], dict) and "url" in images[0]:
1203
+ image_url = images[0]["url"]
1204
+ logging.info(f"Extracted image URL: {image_url}")
1205
+ elif images and isinstance(images[0], str):
1206
+ image_url = images[0]
1207
+ logging.info(f"Extracted image URL: {image_url}")
1208
+
1209
+ markdown_image_link = f"![image]({image_url})"
1210
+ response_data = {
1211
  "id": f"chatcmpl-{uuid.uuid4()}",
1212
+ "object": "chat.completion",
1213
  "created": int(time.time()),
1214
  "model": model_name,
1215
  "choices": [
1216
  {
1217
+ "index": 0,
1218
+ "message": {
1219
+ "role": "assistant",
1220
+ "content": markdown_image_link if image_url else "Failed to generate image",
1221
+ },
1222
+ "finish_reason": "stop",
1223
  }
1224
+ ],
1225
+ }
1226
+ except (KeyError, ValueError, IndexError) as e:
1227
+ logging.error(
1228
+ f"解析响应 JSON 失败: {e}, "
1229
+ f"完整内容: {response_json}"
1230
+ )
1231
+ response_data = {
1232
+ "id": f"chatcmpl-{uuid.uuid4()}",
1233
+ "object": "chat.completion",
1234
+ "created": int(time.time()),
1235
+ "model": model_name,
1236
+ "choices": [
1237
+ {
1238
+ "index": 0,
1239
+ "message": {
1240
+ "role": "assistant",
1241
+ "content": "Failed to process image data",
1242
+ },
1243
+ "finish_reason": "stop",
1244
+ }
1245
+ ],
1246
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1247
 
1248
+ logging.info(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1249
  f"使用的key: {api_key}, "
1250
  f"总共用时: {total_time:.4f}秒, "
1251
  f"使用的模型: {model_name}"
1252
+ )
1253
+ with data_lock:
1254
  request_timestamps.append(time.time())
1255
  token_counts.append(0)
1256
+ return jsonify(response_data)
1257
 
1258
  except requests.exceptions.RequestException as e:
1259
+ logging.error(f"请求转发异常: {e}")
1260
+ return jsonify({"error": str(e)}), 500
1261
  else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1262
  try:
1263
  start_time = time.time()
1264
  response = requests.post(
1265
  TEST_MODEL_ENDPOINT,
1266
  headers=headers,
1267
+ json=data,
1268
  stream=data.get("stream", False),
1269
  timeout=60
1270
  )
 
1293
  prompt_tokens = 0
1294
  completion_tokens = 0
1295
  response_content = ""
 
 
 
1296
  for line in full_response_content.splitlines():
1297
  if line.startswith("data:"):
1298
+ line = line[5:].strip()
1299
+ if line == "[DONE]":
1300
  continue
1301
+ try:
1302
  response_json = json.loads(line)
1303
+
1304
  if (
1305
  "usage" in response_json and
1306
  "completion_tokens" in response_json["usage"]
 
1311
 
1312
  if (
1313
  "choices" in response_json and
1314
+ len(response_json["choices"]) > 0 and
1315
+ "delta" in response_json["choices"][0] and
1316
+ "content" in response_json[
1317
+ "choices"
1318
+ ][0]["delta"]
1319
  ):
1320
+ response_content += response_json[
1321
+ "choices"
1322
+ ][0]["delta"]["content"]
 
 
 
 
 
 
 
 
 
 
1323
 
1324
  if (
1325
  "usage" in response_json and
 
1329
  "usage"
1330
  ]["prompt_tokens"]
1331
 
1332
+ except (
 
1333
  KeyError,
1334
  ValueError,
1335
  IndexError
1336
+ ) as e:
1337
+ logging.error(
1338
  f"解析流式响应单行 JSON 失败: {e}, "
1339
  f"行内容: {line}"
1340
+ )
1341
 
1342
  user_content = ""
1343
  messages = data.get("messages", [])
1344
  for message in messages:
1345
+ if message["role"] == "user":
1346
+ if isinstance(message["content"], str):
1347
  user_content += message["content"] + " "
1348
+ elif isinstance(message["content"], list):
1349
  for item in message["content"]:
1350
  if (
1351
  isinstance(item, dict) and
 
1357
  )
1358
 
1359
  user_content = user_content.strip()
1360
+
1361
  user_content_replaced = user_content.replace(
1362
  '\n', '\\n'
1363
  ).replace('\r', '\\n')
1364
  response_content_replaced = response_content.replace(
1365
  '\n', '\\n'
1366
  ).replace('\r', '\\n')
1367
+
1368
+ logging.info(
1369
  f"使用的key: {api_key}, "
1370
  f"提示token: {prompt_tokens}, "
1371
  f"输出token: {completion_tokens}, "
 
1375
  f"用户的内容: {user_content_replaced}, "
1376
  f"输出的内容: {response_content_replaced}"
1377
  )
 
 
 
 
 
1378
 
1379
  with data_lock:
1380
  request_timestamps.append(time.time())
1381
  token_counts.append(prompt_tokens+completion_tokens)
1382
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1383
  return Response(
1384
  stream_with_context(generate()),
1385
  content_type=response.headers['Content-Type']
 
1398
  response_content = response_json[
1399
  "choices"
1400
  ][0]["message"]["content"]
 
 
 
 
1401
  except (KeyError, ValueError, IndexError) as e:
1402
  logging.error(
1403
  f"解析非流式响应 JSON 失败: {e}, "
 
1406
  prompt_tokens = 0
1407
  completion_tokens = 0
1408
  response_content = ""
 
1409
 
1410
  user_content = ""
1411
  messages = data.get("messages", [])
 
1420
  item.get("type") == "text"
1421
  ):
1422
  user_content += (
1423
+ item.get("text", "") + " "
 
1424
  )
1425
 
1426
  user_content = user_content.strip()
 
1432
  '\n', '\\n'
1433
  ).replace('\r', '\\n')
1434
 
1435
+ logging.info(
1436
+ f"使用的key: {api_key}, "
1437
  f"提示token: {prompt_tokens}, "
1438
  f"输出token: {completion_tokens}, "
1439
  f"首字用时: 0, "
 
1442
  f"用户的内容: {user_content_replaced}, "
1443
  f"输出的内容: {response_content_replaced}"
1444
  )
 
 
 
 
 
1445
  with data_lock:
1446
  request_timestamps.append(time.time())
1447
  if "prompt_tokens" in response_json["usage"] and "completion_tokens" in response_json["usage"]:
1448
  token_counts.append(response_json["usage"]["prompt_tokens"] + response_json["usage"]["completion_tokens"])
1449
  else:
1450
  token_counts.append(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1451
 
1452
+ return jsonify(response_json)
1453
 
1454
  except requests.exceptions.RequestException as e:
1455
  logging.error(f"请求转发异常: {e}")