yangtb24 commited on
Commit
b918005
·
verified ·
1 Parent(s): 4497eab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -68
app.py CHANGED
@@ -1038,84 +1038,89 @@ def handsome_images_generations():
1038
  "Content-Type": "application/json"
1039
  }
1040
 
1041
- siliconflow_data = {
1042
- "model": model_name,
1043
- "prompt": data.get("prompt"),
1044
- "image_size": data.get("size", "1024x1024"),
1045
- "batch_size": data.get("n", 1),
1046
- "num_inference_steps": data.get("steps", 20),
1047
- "guidance_scale": data.get("guidance_scale", 7.5),
1048
- "negative_prompt": data.get("negative_prompt"),
1049
- "seed": data.get("seed"),
1050
- "prompt_enhancement": False,
1051
- }
1052
-
1053
- if siliconflow_data["batch_size"] < 1:
1054
- siliconflow_data["batch_size"] = 1
1055
- if siliconflow_data["batch_size"] > 4:
1056
- siliconflow_data["batch_size"] = 4
1057
 
1058
- if siliconflow_data["num_inference_steps"] < 1:
1059
- siliconflow_data["num_inference_steps"] = 1
1060
- if siliconflow_data["num_inference_steps"] > 50:
1061
- siliconflow_data["num_inference_steps"] = 50
 
 
 
 
 
 
 
 
 
 
 
1062
 
1063
- if siliconflow_data["guidance_scale"] < 0:
1064
- siliconflow_data["guidance_scale"] = 0
1065
- if siliconflow_data["guidance_scale"] > 100:
1066
- siliconflow_data["guidance_scale"] = 100
1067
 
1068
- if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024"]:
1069
- siliconflow_data["image_size"] = "1024x1024"
1070
-
1071
- try:
1072
- start_time = time.time()
1073
- response = requests.post(
1074
- TEST_MODEL_ENDPOINT, # Assuming the same endpoint for image generation
1075
- headers=headers,
1076
- json=siliconflow_data,
1077
- timeout=120
1078
- )
1079
 
1080
- if response.status_code == 429:
1081
- return jsonify(response.json()), 429
1082
 
1083
- response.raise_for_status()
1084
- end_time = time.time()
1085
- response_json = response.json()
1086
- total_time = end_time - start_time
1087
-
1088
- try:
1089
- images = response_json.get("images", [])
1090
- if isinstance(images, list):
1091
- openai_images = [{"url": image} for image in images]
1092
- else:
 
 
 
 
 
 
1093
  openai_images = []
1094
- except (KeyError, ValueError, IndexError) as e:
1095
- logging.error(
1096
- f"解析响应 JSON 失败: {e}, "
1097
- f"完整内容: {response_json}"
1098
- )
1099
- openai_images = []
1100
 
1101
- logging.info(
1102
- f"使用的key: {api_key}, "
1103
- f"总共用时: {total_time:.4f}秒, "
1104
- f"使用的模型: {model_name}"
1105
- )
1106
 
1107
- with data_lock:
1108
- request_timestamps.append(time.time())
1109
- token_counts.append(0) # Image generation doesn't use tokens
1110
 
1111
- return jsonify({
1112
- "created": int(time.time()),
1113
- "data": openai_images
1114
- })
1115
 
1116
- except requests.exceptions.RequestException as e:
1117
- logging.error(f"请求转发异常: {e}")
1118
- return jsonify({"error": str(e)}), 500
 
 
1119
 
1120
  if __name__ == '__main__':
1121
  import json
 
1038
  "Content-Type": "application/json"
1039
  }
1040
 
1041
+ if "stable-diffusion" in model_name:
1042
+ # Map OpenAI-style parameters to SiliconFlow's parameters
1043
+ siliconflow_data = {
1044
+ "model": model_name,
1045
+ "prompt": data.get("prompt"),
1046
+ "image_size": data.get("size", "1024x1024"),
1047
+ "batch_size": data.get("n", 1),
1048
+ "num_inference_steps": data.get("steps", 20),
1049
+ "guidance_scale": data.get("guidance_scale", 7.5),
1050
+ "negative_prompt": data.get("negative_prompt"),
1051
+ "seed": data.get("seed"),
1052
+ "prompt_enhancement": False,
1053
+ }
 
 
 
1054
 
1055
+ # Parameter validation and adjustments
1056
+ if siliconflow_data["batch_size"] < 1:
1057
+ siliconflow_data["batch_size"] = 1
1058
+ if siliconflow_data["batch_size"] > 4:
1059
+ siliconflow_data["batch_size"] = 4
1060
+
1061
+ if siliconflow_data["num_inference_steps"] < 1:
1062
+ siliconflow_data["num_inference_steps"] = 1
1063
+ if siliconflow_data["num_inference_steps"] > 50:
1064
+ siliconflow_data["num_inference_steps"] = 50
1065
+
1066
+ if siliconflow_data["guidance_scale"] < 0:
1067
+ siliconflow_data["guidance_scale"] = 0
1068
+ if siliconflow_data["guidance_scale"] > 100:
1069
+ siliconflow_data["guidance_scale"] = 100
1070
 
1071
+ if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024"]:
1072
+ siliconflow_data["image_size"] = "1024x1024"
 
 
1073
 
1074
+ try:
1075
+ start_time = time.time()
1076
+ response = requests.post(
1077
+ "https://api.siliconflow.cn/v1/images/generations",
1078
+ headers=headers,
1079
+ json=siliconflow_data,
1080
+ timeout=120
1081
+ )
 
 
 
1082
 
1083
+ if response.status_code == 429:
1084
+ return jsonify(response.json()), 429
1085
 
1086
+ response.raise_for_status()
1087
+ end_time = time.time()
1088
+ response_json = response.json()
1089
+ total_time = end_time - start_time
1090
+
1091
+ try:
1092
+ images = response_json.get("images", [])
1093
+ if isinstance(images, list):
1094
+ openai_images = [{"url": image} for image in images]
1095
+ else:
1096
+ openai_images = []
1097
+ except (KeyError, ValueError, IndexError) as e:
1098
+ logging.error(
1099
+ f"解析响应 JSON 失败: {e}, "
1100
+ f"完整内容: {response_json}"
1101
+ )
1102
  openai_images = []
 
 
 
 
 
 
1103
 
1104
+ logging.info(
1105
+ f"使用的key: {api_key}, "
1106
+ f"总共用时: {total_time:.4f}秒, "
1107
+ f"使用的模型: {model_name}"
1108
+ )
1109
 
1110
+ with data_lock:
1111
+ request_timestamps.append(time.time())
1112
+ token_counts.append(0) # Image generation doesn't use tokens
1113
 
1114
+ return jsonify({
1115
+ "created": int(time.time()),
1116
+ "data": openai_images
1117
+ })
1118
 
1119
+ except requests.exceptions.RequestException as e:
1120
+ logging.error(f"请求转发异常: {e}")
1121
+ return jsonify({"error": str(e)}), 500
1122
+ else:
1123
+ return jsonify({"error": "Unsupported model"}), 400
1124
 
1125
  if __name__ == '__main__':
1126
  import json