Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -848,7 +848,6 @@ def handsome_images_generations():
|
|
848 |
"image_size": data.get("image_size", "1024x1024"), # Use 'image_size' directly
|
849 |
"prompt_enhancement": data.get("prompt_enhancement", False),
|
850 |
}
|
851 |
-
|
852 |
seed = data.get("seed")
|
853 |
if isinstance(seed, int) and 0 < seed < 9999999999:
|
854 |
siliconflow_data["seed"] = seed
|
@@ -872,7 +871,6 @@ def handsome_images_generations():
|
|
872 |
siliconflow_data["guidance_scale"] = 0
|
873 |
if siliconflow_data["guidance_scale"] > 100:
|
874 |
siliconflow_data["guidance_scale"] = 100
|
875 |
-
|
876 |
# Validate image_size
|
877 |
if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024"]:
|
878 |
siliconflow_data["image_size"] = "1024x1024"
|
@@ -923,7 +921,6 @@ def handsome_images_generations():
|
|
923 |
"created": int(time.time()),
|
924 |
"data": openai_images
|
925 |
}
|
926 |
-
|
927 |
except (KeyError, ValueError, IndexError) as e:
|
928 |
logging.error(
|
929 |
f"解析响应 JSON 失败: {e}, "
|
@@ -1048,7 +1045,7 @@ def handsome_chat_completions():
|
|
1048 |
if siliconflow_data["guidance_scale"] > 100:
|
1049 |
siliconflow_data["guidance_scale"] = 100
|
1050 |
|
1051 |
-
if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024"]:
|
1052 |
siliconflow_data["image_size"] = "1024x1024"
|
1053 |
|
1054 |
try:
|
@@ -1216,7 +1213,6 @@ def handsome_chat_completions():
|
|
1216 |
}
|
1217 |
],
|
1218 |
}
|
1219 |
-
|
1220 |
except (KeyError, ValueError, IndexError) as e:
|
1221 |
logging.error(
|
1222 |
f"解析响应 JSON 失败: {e}, "
|
@@ -1254,201 +1250,176 @@ def handsome_chat_completions():
|
|
1254 |
logging.error(f"请求转发异常: {e}")
|
1255 |
return jsonify({"error": str(e)}), 500
|
1256 |
else:
|
|
|
1257 |
try:
|
1258 |
start_time = time.time()
|
1259 |
response = requests.post(
|
1260 |
TEST_MODEL_ENDPOINT,
|
1261 |
headers=headers,
|
1262 |
json=data,
|
1263 |
-
|
1264 |
-
|
1265 |
)
|
1266 |
-
if response.status_code == 429:
|
1267 |
-
return jsonify(response.json()), 429
|
1268 |
|
|
|
|
|
|
|
1269 |
if data.get("stream", False):
|
1270 |
def generate():
|
1271 |
first_chunk_time = None
|
1272 |
full_response_content = ""
|
1273 |
-
|
1274 |
-
|
1275 |
-
if
|
1276 |
-
first_chunk_time
|
1277 |
-
|
1278 |
-
|
1279 |
-
|
1280 |
-
|
1281 |
-
|
1282 |
-
|
1283 |
-
|
1284 |
-
|
1285 |
-
|
1286 |
-
|
1287 |
-
|
1288 |
-
|
1289 |
-
|
1290 |
-
|
1291 |
-
|
1292 |
-
|
1293 |
-
|
1294 |
-
|
1295 |
-
|
1296 |
-
|
1297 |
-
|
1298 |
-
|
1299 |
-
|
1300 |
-
|
1301 |
-
|
1302 |
-
completion_tokens = response_json[
|
1303 |
-
"usage"
|
1304 |
-
]["completion_tokens"]
|
1305 |
-
|
1306 |
-
if (
|
1307 |
-
"choices" in response_json and
|
1308 |
-
len(response_json["choices"]) > 0 and
|
1309 |
-
"delta" in response_json["choices"][0] and
|
1310 |
-
"content" in response_json[
|
1311 |
-
"choices"
|
1312 |
-
][0]["delta"]
|
1313 |
-
):
|
1314 |
-
response_content += response_json[
|
1315 |
-
"choices"
|
1316 |
-
][0]["delta"]["content"]
|
1317 |
-
|
1318 |
-
if (
|
1319 |
-
"usage" in response_json and
|
1320 |
-
"prompt_tokens" in response_json["usage"]
|
1321 |
-
):
|
1322 |
-
prompt_tokens = response_json[
|
1323 |
-
"usage"
|
1324 |
-
]["prompt_tokens"]
|
1325 |
-
|
1326 |
-
except (
|
1327 |
-
KeyError,
|
1328 |
-
ValueError,
|
1329 |
-
IndexError
|
1330 |
-
) as e:
|
1331 |
-
logging.error(
|
1332 |
-
f"解析流式响应单行 JSON 失败: {e}, "
|
1333 |
-
f"行内容: {line}"
|
1334 |
-
)
|
1335 |
-
|
1336 |
-
user_content = ""
|
1337 |
-
messages = data.get("messages", [])
|
1338 |
-
for message in messages:
|
1339 |
-
if message["role"] == "user":
|
1340 |
-
if isinstance(message["content"], str):
|
1341 |
-
user_content += message["content"] + " "
|
1342 |
-
elif isinstance(message["content"], list):
|
1343 |
-
for item in message["content"]:
|
1344 |
-
if (
|
1345 |
-
isinstance(item, dict) and
|
1346 |
-
item.get("type") == "text"
|
1347 |
-
):
|
1348 |
-
user_content += (
|
1349 |
-
item.get("text", "") +
|
1350 |
-
" "
|
1351 |
-
)
|
1352 |
-
|
1353 |
-
user_content = user_content.strip()
|
1354 |
-
|
1355 |
-
user_content_replaced = user_content.replace(
|
1356 |
-
'\n', '\\n'
|
1357 |
-
).replace('\r', '\\n')
|
1358 |
-
response_content_replaced = response_content.replace(
|
1359 |
-
'\n', '\\n'
|
1360 |
-
).replace('\r', '\\n')
|
1361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1362 |
logging.info(
|
1363 |
f"使用的key: {api_key}, "
|
1364 |
-
f"
|
1365 |
-
f"输出token: {completion_tokens}, "
|
1366 |
-
f"首字用时: {first_token_time:.4f}秒, "
|
1367 |
-
f"总共用时: {total_time:.4f}秒, "
|
1368 |
-
f"使用的模型: {model_name}, "
|
1369 |
-
f"用户的内容: {user_content_replaced}, "
|
1370 |
-
f"输出的内容: {response_content_replaced}"
|
1371 |
)
|
|
|
|
|
1372 |
|
1373 |
-
with data_lock:
|
1374 |
-
request_timestamps.append(time.time())
|
1375 |
-
token_counts.append(prompt_tokens+completion_tokens)
|
1376 |
-
|
1377 |
-
return Response(
|
1378 |
-
stream_with_context(generate()),
|
1379 |
-
content_type=response.headers['Content-Type']
|
1380 |
-
)
|
1381 |
else:
|
1382 |
response.raise_for_status()
|
1383 |
end_time = time.time()
|
1384 |
response_json = response.json()
|
1385 |
total_time = end_time - start_time
|
1386 |
-
|
1387 |
try:
|
1388 |
-
|
1389 |
-
|
1390 |
-
|
1391 |
-
|
1392 |
-
|
1393 |
-
|
1394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1395 |
except (KeyError, ValueError, IndexError) as e:
|
1396 |
logging.error(
|
1397 |
-
f"
|
1398 |
f"完整内容: {response_json}"
|
1399 |
)
|
1400 |
-
|
1401 |
-
|
1402 |
-
|
1403 |
-
|
1404 |
-
|
1405 |
-
|
1406 |
-
|
1407 |
-
|
1408 |
-
|
1409 |
-
|
1410 |
-
|
1411 |
-
|
1412 |
-
|
1413 |
-
|
1414 |
-
|
1415 |
-
|
1416 |
-
user_content += (
|
1417 |
-
item.get("text", "") +
|
1418 |
-
" "
|
1419 |
-
)
|
1420 |
-
|
1421 |
-
user_content = user_content.strip()
|
1422 |
-
|
1423 |
-
user_content_replaced = user_content.replace(
|
1424 |
-
'\n', '\\n'
|
1425 |
-
).replace('\r', '\\n')
|
1426 |
-
response_content_replaced = response_content.replace(
|
1427 |
-
'\n', '\\n'
|
1428 |
-
).replace('\r', '\\n')
|
1429 |
|
1430 |
logging.info(
|
1431 |
f"使用的key: {api_key}, "
|
1432 |
-
f"提示token: {prompt_tokens}, "
|
1433 |
-
f"输出token: {completion_tokens}, "
|
1434 |
-
f"首字用时: 0, "
|
1435 |
f"总共用时: {total_time:.4f}秒, "
|
1436 |
-
f"使用的模型: {model_name}
|
1437 |
-
f"用户的内容: {user_content_replaced}, "
|
1438 |
-
f"输出的内容: {response_content_replaced}"
|
1439 |
)
|
1440 |
with data_lock:
|
1441 |
-
|
1442 |
-
if "prompt_tokens" in response_json["usage"] and "completion_tokens" in response_json["usage"]:
|
1443 |
-
token_counts.append(response_json["usage"]["prompt_tokens"] + response_json["usage"]["completion_tokens"])
|
1444 |
-
else:
|
1445 |
token_counts.append(0)
|
1446 |
-
|
1447 |
-
|
1448 |
-
|
1449 |
except requests.exceptions.RequestException as e:
|
1450 |
-
|
1451 |
-
|
1452 |
|
1453 |
if __name__ == '__main__':
|
1454 |
import json
|
|
|
848 |
"image_size": data.get("image_size", "1024x1024"), # Use 'image_size' directly
|
849 |
"prompt_enhancement": data.get("prompt_enhancement", False),
|
850 |
}
|
|
|
851 |
seed = data.get("seed")
|
852 |
if isinstance(seed, int) and 0 < seed < 9999999999:
|
853 |
siliconflow_data["seed"] = seed
|
|
|
871 |
siliconflow_data["guidance_scale"] = 0
|
872 |
if siliconflow_data["guidance_scale"] > 100:
|
873 |
siliconflow_data["guidance_scale"] = 100
|
|
|
874 |
# Validate image_size
|
875 |
if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024"]:
|
876 |
siliconflow_data["image_size"] = "1024x1024"
|
|
|
921 |
"created": int(time.time()),
|
922 |
"data": openai_images
|
923 |
}
|
|
|
924 |
except (KeyError, ValueError, IndexError) as e:
|
925 |
logging.error(
|
926 |
f"解析响应 JSON 失败: {e}, "
|
|
|
1045 |
if siliconflow_data["guidance_scale"] > 100:
|
1046 |
siliconflow_data["guidance_scale"] = 100
|
1047 |
|
1048 |
+
if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024", "960x1280", "720x1440", "720x1280"]:
|
1049 |
siliconflow_data["image_size"] = "1024x1024"
|
1050 |
|
1051 |
try:
|
|
|
1213 |
}
|
1214 |
],
|
1215 |
}
|
|
|
1216 |
except (KeyError, ValueError, IndexError) as e:
|
1217 |
logging.error(
|
1218 |
f"解析响应 JSON 失败: {e}, "
|
|
|
1250 |
logging.error(f"请求转发异常: {e}")
|
1251 |
return jsonify({"error": str(e)}), 500
|
1252 |
else:
|
1253 |
+
# Handle text completion
|
1254 |
try:
|
1255 |
start_time = time.time()
|
1256 |
response = requests.post(
|
1257 |
TEST_MODEL_ENDPOINT,
|
1258 |
headers=headers,
|
1259 |
json=data,
|
1260 |
+
timeout=120,
|
1261 |
+
stream=data.get("stream", False)
|
1262 |
)
|
|
|
|
|
1263 |
|
1264 |
+
if response.status_code == 429:
|
1265 |
+
return jsonify(response.json()), 429
|
1266 |
+
|
1267 |
if data.get("stream", False):
|
1268 |
def generate():
|
1269 |
first_chunk_time = None
|
1270 |
full_response_content = ""
|
1271 |
+
try:
|
1272 |
+
for chunk in response.iter_lines(decode_unicode=True):
|
1273 |
+
if chunk:
|
1274 |
+
if first_chunk_time is None:
|
1275 |
+
first_chunk_time = time.time()
|
1276 |
+
try:
|
1277 |
+
json_chunk = json.loads(chunk.lstrip("data: "))
|
1278 |
+
if "choices" in json_chunk and json_chunk["choices"]:
|
1279 |
+
delta = json_chunk["choices"][0].get("delta", {})
|
1280 |
+
content = delta.get("content", "")
|
1281 |
+
full_response_content += content
|
1282 |
+
yield f"data: {json.dumps(json_chunk)}\n\n".encode('utf-8')
|
1283 |
+
except json.JSONDecodeError:
|
1284 |
+
logging.error(f"JSON解析失败: {chunk}")
|
1285 |
+
|
1286 |
+
end_chunk_data = {
|
1287 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
1288 |
+
"object": "chat.completion.chunk",
|
1289 |
+
"created": int(time.time()),
|
1290 |
+
"model": model_name,
|
1291 |
+
"choices": [
|
1292 |
+
{
|
1293 |
+
"index": 0,
|
1294 |
+
"delta": {},
|
1295 |
+
"finish_reason": "stop"
|
1296 |
+
}
|
1297 |
+
]
|
1298 |
+
}
|
1299 |
+
yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1300 |
|
1301 |
+
with data_lock:
|
1302 |
+
request_timestamps.append(time.time())
|
1303 |
+
token_counts.append(0)
|
1304 |
+
except requests.exceptions.RequestException as e:
|
1305 |
+
logging.error(f"请求转发异常: {e}")
|
1306 |
+
error_chunk_data = {
|
1307 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
1308 |
+
"object": "chat.completion.chunk",
|
1309 |
+
"created": int(time.time()),
|
1310 |
+
"model": model_name,
|
1311 |
+
"choices": [
|
1312 |
+
{
|
1313 |
+
"index": 0,
|
1314 |
+
"delta": {
|
1315 |
+
"role": "assistant",
|
1316 |
+
"content": f"Error: {str(e)}"
|
1317 |
+
},
|
1318 |
+
"finish_reason": None
|
1319 |
+
}
|
1320 |
+
]
|
1321 |
+
}
|
1322 |
+
yield f"data: {json.dumps(error_chunk_data)}\n\n".encode('utf-8')
|
1323 |
+
end_chunk_data = {
|
1324 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
1325 |
+
"object": "chat.completion.chunk",
|
1326 |
+
"created": int(time.time()),
|
1327 |
+
"model": model_name,
|
1328 |
+
"choices": [
|
1329 |
+
{
|
1330 |
+
"index": 0,
|
1331 |
+
"delta": {},
|
1332 |
+
"finish_reason": "stop"
|
1333 |
+
}
|
1334 |
+
]
|
1335 |
+
}
|
1336 |
+
yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
|
1337 |
logging.info(
|
1338 |
f"使用的key: {api_key}, "
|
1339 |
+
f"使用的模型: {model_name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
1340 |
)
|
1341 |
+
yield "data: [DONE]\n\n".encode('utf-8')
|
1342 |
+
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
1343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1344 |
else:
|
1345 |
response.raise_for_status()
|
1346 |
end_time = time.time()
|
1347 |
response_json = response.json()
|
1348 |
total_time = end_time - start_time
|
1349 |
+
|
1350 |
try:
|
1351 |
+
choices = response_json.get("choices", [])
|
1352 |
+
if choices and isinstance(choices[0], dict):
|
1353 |
+
message = choices[0].get("message",{})
|
1354 |
+
content = message.get("content")
|
1355 |
+
response_data = {
|
1356 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
1357 |
+
"object": "chat.completion",
|
1358 |
+
"created": int(time.time()),
|
1359 |
+
"model": model_name,
|
1360 |
+
"choices": [
|
1361 |
+
{
|
1362 |
+
"index": 0,
|
1363 |
+
"message": {
|
1364 |
+
"role": "assistant",
|
1365 |
+
"content": content
|
1366 |
+
},
|
1367 |
+
"finish_reason": "stop"
|
1368 |
+
}
|
1369 |
+
],
|
1370 |
+
}
|
1371 |
+
else:
|
1372 |
+
response_data = {
|
1373 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
1374 |
+
"object": "chat.completion",
|
1375 |
+
"created": int(time.time()),
|
1376 |
+
"model": model_name,
|
1377 |
+
"choices": [
|
1378 |
+
{
|
1379 |
+
"index": 0,
|
1380 |
+
"message": {
|
1381 |
+
"role": "assistant",
|
1382 |
+
"content": "No response content"
|
1383 |
+
},
|
1384 |
+
"finish_reason": "stop"
|
1385 |
+
}
|
1386 |
+
]
|
1387 |
+
}
|
1388 |
except (KeyError, ValueError, IndexError) as e:
|
1389 |
logging.error(
|
1390 |
+
f"解析响应 JSON 失败: {e}, "
|
1391 |
f"完整内容: {response_json}"
|
1392 |
)
|
1393 |
+
response_data = {
|
1394 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
1395 |
+
"object": "chat.completion",
|
1396 |
+
"created": int(time.time()),
|
1397 |
+
"model": model_name,
|
1398 |
+
"choices": [
|
1399 |
+
{
|
1400 |
+
"index": 0,
|
1401 |
+
"message": {
|
1402 |
+
"role": "assistant",
|
1403 |
+
"content": "Failed to process data"
|
1404 |
+
},
|
1405 |
+
"finish_reason": "stop"
|
1406 |
+
}
|
1407 |
+
]
|
1408 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1409 |
|
1410 |
logging.info(
|
1411 |
f"使用的key: {api_key}, "
|
|
|
|
|
|
|
1412 |
f"总共用时: {total_time:.4f}秒, "
|
1413 |
+
f"使用的模型: {model_name}"
|
|
|
|
|
1414 |
)
|
1415 |
with data_lock:
|
1416 |
+
request_timestamps.append(time.time())
|
|
|
|
|
|
|
1417 |
token_counts.append(0)
|
1418 |
+
return jsonify(response_data)
|
1419 |
+
|
|
|
1420 |
except requests.exceptions.RequestException as e:
|
1421 |
+
logging.error(f"请求转发异常: {e}")
|
1422 |
+
return jsonify({"error": str(e)}), 500
|
1423 |
|
1424 |
if __name__ == '__main__':
|
1425 |
import json
|