yangtb24 commited on
Commit
9bef950
·
verified ·
1 Parent(s): 3a2edd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -1067,7 +1067,7 @@ def handsome_chat_completions():
1067
  siliconflow_data["num_inference_steps"] = 20
1068
  siliconflow_data["guidance_scale"] = 7.5
1069
  siliconflow_data["prompt_enhancement"] = False
1070
-
1071
  if data.get("size"):
1072
  siliconflow_data["image_size"] = data.get("size")
1073
  if data.get("n"):
@@ -1097,7 +1097,7 @@ def handsome_chat_completions():
1097
  siliconflow_data["guidance_scale"] = 0
1098
  if siliconflow_data["guidance_scale"] > 100:
1099
  siliconflow_data["guidance_scale"] = 100
1100
-
1101
  if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024", "960x1280", "720x1440", "720x1280"]:
1102
  siliconflow_data["image_size"] = "1024x1024"
1103
 
@@ -1188,7 +1188,6 @@ def handsome_chat_completions():
1188
  ]
1189
  }
1190
  yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
1191
-
1192
  with data_lock:
1193
  request_timestamps.append(time.time())
1194
  token_counts.append(0)
@@ -1204,15 +1203,34 @@ def handsome_chat_completions():
1204
  "index": 0,
1205
  "delta": {
1206
  "role": "assistant",
1207
- "content": "Failed to process image data"
1208
  },
1209
- "finish_reason": "stop"
1210
  }
1211
  ]
1212
  }
1213
  yield f"data: {json.dumps(error_chunk_data)}\n\n".encode('utf-8')
1214
- yield "data: [DONE]\n\n".encode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1215
  return Response(stream_with_context(generate()), content_type='text/event-stream')
 
1216
  else:
1217
  response.raise_for_status()
1218
  end_time = time.time()
@@ -1274,12 +1292,14 @@ def handsome_chat_completions():
1274
  f"总共用时: {total_time:.4f}秒, "
1275
  f"使用的模型: {model_name}"
1276
  )
1277
-
1278
  with data_lock:
1279
  request_timestamps.append(time.time())
1280
  token_counts.append(0)
1281
-
1282
  return jsonify(response_data)
 
 
 
 
1283
  else:
1284
  try:
1285
  start_time = time.time()
@@ -1394,11 +1414,9 @@ def handsome_chat_completions():
1394
  f"总共用时: {total_time:.4f}秒, "
1395
  f"使用的模型: {model_name}"
1396
  )
1397
-
1398
  with data_lock:
1399
  request_timestamps.append(time.time())
1400
  token_counts.append(0)
1401
-
1402
  return jsonify(response_data)
1403
  except requests.exceptions.RequestException as e:
1404
  logging.error(f"请求转发异常: {e}")
 
1067
  siliconflow_data["num_inference_steps"] = 20
1068
  siliconflow_data["guidance_scale"] = 7.5
1069
  siliconflow_data["prompt_enhancement"] = False
1070
+
1071
  if data.get("size"):
1072
  siliconflow_data["image_size"] = data.get("size")
1073
  if data.get("n"):
 
1097
  siliconflow_data["guidance_scale"] = 0
1098
  if siliconflow_data["guidance_scale"] > 100:
1099
  siliconflow_data["guidance_scale"] = 100
1100
+
1101
  if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024", "960x1280", "720x1440", "720x1280"]:
1102
  siliconflow_data["image_size"] = "1024x1024"
1103
 
 
1188
  ]
1189
  }
1190
  yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
 
1191
  with data_lock:
1192
  request_timestamps.append(time.time())
1193
  token_counts.append(0)
 
1203
  "index": 0,
1204
  "delta": {
1205
  "role": "assistant",
1206
+ "content": f"Error: {str(e)}"
1207
  },
1208
+ "finish_reason": None
1209
  }
1210
  ]
1211
  }
1212
  yield f"data: {json.dumps(error_chunk_data)}\n\n".encode('utf-8')
1213
+ end_chunk_data = {
1214
+ "id": f"chatcmpl-{uuid.uuid4()}",
1215
+ "object": "chat.completion.chunk",
1216
+ "created": int(time.time()),
1217
+ "model": model_name,
1218
+ "choices": [
1219
+ {
1220
+ "index": 0,
1221
+ "delta": {},
1222
+ "finish_reason": "stop"
1223
+ }
1224
+ ]
1225
+ }
1226
+ yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
1227
+ logging.info(
1228
+ f"使用的key: {api_key}, "
1229
+ f"使用的模型: {model_name}"
1230
+ )
1231
+ yield "data: [DONE]\n\n".encode('utf-8')
1232
  return Response(stream_with_context(generate()), content_type='text/event-stream')
1233
+
1234
  else:
1235
  response.raise_for_status()
1236
  end_time = time.time()
 
1292
  f"总共用时: {total_time:.4f}秒, "
1293
  f"使用的模型: {model_name}"
1294
  )
 
1295
  with data_lock:
1296
  request_timestamps.append(time.time())
1297
  token_counts.append(0)
 
1298
  return jsonify(response_data)
1299
+
1300
+ except requests.exceptions.RequestException as e:
1301
+ logging.error(f"请求转发异常: {e}")
1302
+ return jsonify({"error": str(e)}), 500
1303
  else:
1304
  try:
1305
  start_time = time.time()
 
1414
  f"总共用时: {total_time:.4f}秒, "
1415
  f"使用的模型: {model_name}"
1416
  )
 
1417
  with data_lock:
1418
  request_timestamps.append(time.time())
1419
  token_counts.append(0)
 
1420
  return jsonify(response_data)
1421
  except requests.exceptions.RequestException as e:
1422
  logging.error(f"请求转发异常: {e}")