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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 951 |
@app.route('/handsome/v1/chat/completions', methods=['POST'])
|
| 952 |
def handsome_chat_completions():
|
| 953 |
if not check_authorization(request):
|
|
@@ -1025,6 +1261,8 @@ def handsome_chat_completions():
|
|
| 1025 |
siliconflow_data["negative_prompt"] = data.get("negative_prompt")
|
| 1026 |
if data.get("seed"):
|
| 1027 |
siliconflow_data["seed"] = data.get("seed")
|
|
|
|
|
|
|
| 1028 |
|
| 1029 |
if siliconflow_data["batch_size"] < 1:
|
| 1030 |
siliconflow_data["batch_size"] = 1
|
|
@@ -1441,7 +1679,7 @@ def handsome_chat_completions():
|
|
| 1441 |
|
| 1442 |
except requests.exceptions.RequestException as e:
|
| 1443 |
logging.error(f"请求转发异常: {e}")
|
| 1444 |
-
return jsonify({"error": str(e)}), 500
|
| 1445 |
|
| 1446 |
if __name__ == '__main__':
|
| 1447 |
import json
|
|
|
|
| 948 |
else:
|
| 949 |
return jsonify({"error": "Unsupported model"}), 400
|
| 950 |
|
| 951 |
+
@app.route('/handsome/v1/images/generations', methods=['POST'])
|
| 952 |
+
def handsome_images_generations():
|
| 953 |
+
if not check_authorization(request):
|
| 954 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 955 |
+
|
| 956 |
+
data = request.get_json()
|
| 957 |
+
if not data or 'model' not in data:
|
| 958 |
+
return jsonify({"error": "Invalid request data"}), 400
|
| 959 |
+
|
| 960 |
+
model_name = data.get('model')
|
| 961 |
+
|
| 962 |
+
request_type = determine_request_type(
|
| 963 |
+
model_name,
|
| 964 |
+
image_models,
|
| 965 |
+
free_image_models
|
| 966 |
+
)
|
| 967 |
+
|
| 968 |
+
api_key = select_key(request_type, model_name)
|
| 969 |
+
|
| 970 |
+
if not api_key:
|
| 971 |
+
return jsonify(
|
| 972 |
+
{
|
| 973 |
+
"error": (
|
| 974 |
+
"No available API key for this "
|
| 975 |
+
"request type or all keys have "
|
| 976 |
+
"reached their limits"
|
| 977 |
+
)
|
| 978 |
+
}
|
| 979 |
+
), 429
|
| 980 |
+
|
| 981 |
+
headers = {
|
| 982 |
+
"Authorization": f"Bearer {api_key}",
|
| 983 |
+
"Content-Type": "application/json"
|
| 984 |
+
}
|
| 985 |
+
|
| 986 |
+
response_data = {}
|
| 987 |
+
|
| 988 |
+
if model_name in ["black-forest-labs/FLUX.1-schnell", "Pro/black-forest-labs/FLUX.1-schnell"]:
|
| 989 |
+
siliconflow_data = {
|
| 990 |
+
"model": model_name,
|
| 991 |
+
"prompt": data.get("prompt"),
|
| 992 |
+
"image_size": data.get("image_size", "1024x1024"), # Use image_size directly
|
| 993 |
+
"seed": data.get("seed"),
|
| 994 |
+
"prompt_enhancement": data.get("prompt_enhancement", False),
|
| 995 |
+
}
|
| 996 |
+
# Parameter validation and adjustments
|
| 997 |
+
if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024"]:
|
| 998 |
+
siliconflow_data["image_size"] = "1024x1024"
|
| 999 |
+
|
| 1000 |
+
if siliconflow_data["seed"] is not None:
|
| 1001 |
+
if not isinstance(siliconflow_data["seed"], int):
|
| 1002 |
+
return jsonify({"error": "Invalid seed, must be integer"}), 400
|
| 1003 |
+
if not (0 < siliconflow_data["seed"] < 9999999999):
|
| 1004 |
+
return jsonify({"error": "Invalid seed, must be between 0 and 9999999999"}), 400
|
| 1005 |
+
|
| 1006 |
+
try:
|
| 1007 |
+
start_time = time.time()
|
| 1008 |
+
response = requests.post(
|
| 1009 |
+
"https://api.siliconflow.cn/v1/images/generations",
|
| 1010 |
+
headers=headers,
|
| 1011 |
+
json=siliconflow_data,
|
| 1012 |
+
timeout=120
|
| 1013 |
+
)
|
| 1014 |
+
|
| 1015 |
+
if response.status_code == 429:
|
| 1016 |
+
return jsonify(response.json()), 429
|
| 1017 |
+
|
| 1018 |
+
response.raise_for_status()
|
| 1019 |
+
end_time = time.time()
|
| 1020 |
+
response_json = response.json()
|
| 1021 |
+
total_time = end_time - start_time
|
| 1022 |
+
|
| 1023 |
+
try:
|
| 1024 |
+
images = response_json.get("images", [])
|
| 1025 |
+
openai_images = []
|
| 1026 |
+
for item in images:
|
| 1027 |
+
if isinstance(item, dict) and "url" in item:
|
| 1028 |
+
image_url = item["url"]
|
| 1029 |
+
print(f"image_url: {image_url}")
|
| 1030 |
+
if data.get("response_format") == "b64_json":
|
| 1031 |
+
try:
|
| 1032 |
+
image_data = requests.get(image_url, stream=True).raw
|
| 1033 |
+
image = Image.open(image_data)
|
| 1034 |
+
buffered = io.BytesIO()
|
| 1035 |
+
image.save(buffered, format="PNG")
|
| 1036 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 1037 |
+
openai_images.append({"b64_json": img_str})
|
| 1038 |
+
except Exception as e:
|
| 1039 |
+
logging.error(f"图片转base64失败: {e}")
|
| 1040 |
+
openai_images.append({"url": image_url})
|
| 1041 |
+
else:
|
| 1042 |
+
openai_images.append({"url": image_url})
|
| 1043 |
+
else:
|
| 1044 |
+
logging.error(f"无效的图片数据: {item}")
|
| 1045 |
+
openai_images.append({"url": item})
|
| 1046 |
+
|
| 1047 |
+
|
| 1048 |
+
response_data = {
|
| 1049 |
+
"created": int(time.time()),
|
| 1050 |
+
"data": openai_images
|
| 1051 |
+
}
|
| 1052 |
+
|
| 1053 |
+
except (KeyError, ValueError, IndexError) as e:
|
| 1054 |
+
logging.error(
|
| 1055 |
+
f"解析响应 JSON 失败: {e}, "
|
| 1056 |
+
f"完整内容: {response_json}"
|
| 1057 |
+
)
|
| 1058 |
+
response_data = {
|
| 1059 |
+
"created": int(time.time()),
|
| 1060 |
+
"data": []
|
| 1061 |
+
}
|
| 1062 |
+
|
| 1063 |
+
|
| 1064 |
+
logging.info(
|
| 1065 |
+
f"使用的key: {api_key}, "
|
| 1066 |
+
f"总共用时: {total_time:.4f}秒, "
|
| 1067 |
+
f"使用的模型: {model_name}"
|
| 1068 |
+
)
|
| 1069 |
+
|
| 1070 |
+
with data_lock:
|
| 1071 |
+
request_timestamps.append(time.time())
|
| 1072 |
+
token_counts.append(0)
|
| 1073 |
+
|
| 1074 |
+
return jsonify(response_data)
|
| 1075 |
+
|
| 1076 |
+
except requests.exceptions.RequestException as e:
|
| 1077 |
+
logging.error(f"请求转发异常: {e}")
|
| 1078 |
+
return jsonify({"error": str(e)}), 500
|
| 1079 |
+
elif "stable-diffusion" in model_name:
|
| 1080 |
+
siliconflow_data = {
|
| 1081 |
+
"model": model_name,
|
| 1082 |
+
"prompt": data.get("prompt"),
|
| 1083 |
+
"image_size": data.get("size", "1024x1024"),
|
| 1084 |
+
"batch_size": data.get("n", 1),
|
| 1085 |
+
"num_inference_steps": data.get("steps", 20),
|
| 1086 |
+
"guidance_scale": data.get("guidance_scale", 7.5),
|
| 1087 |
+
"negative_prompt": data.get("negative_prompt"),
|
| 1088 |
+
"seed": data.get("seed"),
|
| 1089 |
+
"prompt_enhancement": data.get("prompt_enhancement", False),
|
| 1090 |
+
}
|
| 1091 |
+
|
| 1092 |
+
# Parameter validation and adjustments
|
| 1093 |
+
if siliconflow_data["batch_size"] < 1:
|
| 1094 |
+
siliconflow_data["batch_size"] = 1
|
| 1095 |
+
if siliconflow_data["batch_size"] > 4:
|
| 1096 |
+
siliconflow_data["batch_size"] = 4
|
| 1097 |
+
|
| 1098 |
+
if siliconflow_data["num_inference_steps"] < 1:
|
| 1099 |
+
siliconflow_data["num_inference_steps"] = 1
|
| 1100 |
+
if siliconflow_data["num_inference_steps"] > 50:
|
| 1101 |
+
siliconflow_data["num_inference_steps"] = 50
|
| 1102 |
+
|
| 1103 |
+
if siliconflow_data["guidance_scale"] < 0:
|
| 1104 |
+
siliconflow_data["guidance_scale"] = 0
|
| 1105 |
+
if siliconflow_data["guidance_scale"] > 100:
|
| 1106 |
+
siliconflow_data["guidance_scale"] = 100
|
| 1107 |
+
|
| 1108 |
+
if siliconflow_data["image_size"] not in ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024"]:
|
| 1109 |
+
siliconflow_data["image_size"] = "1024x1024"
|
| 1110 |
+
|
| 1111 |
+
try:
|
| 1112 |
+
start_time = time.time()
|
| 1113 |
+
response = requests.post(
|
| 1114 |
+
"https://api.siliconflow.cn/v1/images/generations",
|
| 1115 |
+
headers=headers,
|
| 1116 |
+
json=siliconflow_data,
|
| 1117 |
+
timeout=120
|
| 1118 |
+
)
|
| 1119 |
+
|
| 1120 |
+
if response.status_code == 429:
|
| 1121 |
+
return jsonify(response.json()), 429
|
| 1122 |
+
|
| 1123 |
+
response.raise_for_status()
|
| 1124 |
+
end_time = time.time()
|
| 1125 |
+
response_json = response.json()
|
| 1126 |
+
total_time = end_time - start_time
|
| 1127 |
+
|
| 1128 |
+
try:
|
| 1129 |
+
images = response_json.get("images", [])
|
| 1130 |
+
openai_images = []
|
| 1131 |
+
for item in images:
|
| 1132 |
+
if isinstance(item, dict) and "url" in item:
|
| 1133 |
+
image_url = item["url"]
|
| 1134 |
+
print(f"image_url: {image_url}")
|
| 1135 |
+
if data.get("response_format") == "b64_json":
|
| 1136 |
+
try:
|
| 1137 |
+
image_data = requests.get(image_url, stream=True).raw
|
| 1138 |
+
image = Image.open(image_data)
|
| 1139 |
+
buffered = io.BytesIO()
|
| 1140 |
+
image.save(buffered, format="PNG")
|
| 1141 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 1142 |
+
openai_images.append({"b64_json": img_str})
|
| 1143 |
+
except Exception as e:
|
| 1144 |
+
logging.error(f"图片转base64失败: {e}")
|
| 1145 |
+
openai_images.append({"url": image_url})
|
| 1146 |
+
else:
|
| 1147 |
+
openai_images.append({"url": image_url})
|
| 1148 |
+
else:
|
| 1149 |
+
logging.error(f"无效的图片数据: {item}")
|
| 1150 |
+
openai_images.append({"url": item})
|
| 1151 |
+
|
| 1152 |
+
|
| 1153 |
+
response_data = {
|
| 1154 |
+
"created": int(time.time()),
|
| 1155 |
+
"data": openai_images
|
| 1156 |
+
}
|
| 1157 |
+
|
| 1158 |
+
except (KeyError, ValueError, IndexError) as e:
|
| 1159 |
+
logging.error(
|
| 1160 |
+
f"解析响应 JSON 失败: {e}, "
|
| 1161 |
+
f"完整内容: {response_json}"
|
| 1162 |
+
)
|
| 1163 |
+
response_data = {
|
| 1164 |
+
"created": int(time.time()),
|
| 1165 |
+
"data": []
|
| 1166 |
+
}
|
| 1167 |
+
|
| 1168 |
+
|
| 1169 |
+
logging.info(
|
| 1170 |
+
f"使用的key: {api_key}, "
|
| 1171 |
+
f"总共用时: {total_time:.4f}秒, "
|
| 1172 |
+
f"使用的模型: {model_name}"
|
| 1173 |
+
)
|
| 1174 |
+
|
| 1175 |
+
with data_lock:
|
| 1176 |
+
request_timestamps.append(time.time())
|
| 1177 |
+
token_counts.append(0)
|
| 1178 |
+
|
| 1179 |
+
return jsonify(response_data)
|
| 1180 |
+
|
| 1181 |
+
except requests.exceptions.RequestException as e:
|
| 1182 |
+
logging.error(f"请求转发异常: {e}")
|
| 1183 |
+
return jsonify({"error": str(e)}), 500
|
| 1184 |
+
else:
|
| 1185 |
+
return jsonify({"error": "Unsupported model"}), 400
|
| 1186 |
+
|
| 1187 |
@app.route('/handsome/v1/chat/completions', methods=['POST'])
|
| 1188 |
def handsome_chat_completions():
|
| 1189 |
if not check_authorization(request):
|
|
|
|
| 1261 |
siliconflow_data["negative_prompt"] = data.get("negative_prompt")
|
| 1262 |
if data.get("seed"):
|
| 1263 |
siliconflow_data["seed"] = data.get("seed")
|
| 1264 |
+
if data.get("prompt_enhancement"):
|
| 1265 |
+
siliconflow_data["prompt_enhancement"] = data.get("prompt_enhancement")
|
| 1266 |
|
| 1267 |
if siliconflow_data["batch_size"] < 1:
|
| 1268 |
siliconflow_data["batch_size"] = 1
|
|
|
|
| 1679 |
|
| 1680 |
except requests.exceptions.RequestException as e:
|
| 1681 |
logging.error(f"请求转发异常: {e}")
|
| 1682 |
+
return jsonify({"error": str(e)}), 500
|
| 1683 |
|
| 1684 |
if __name__ == '__main__':
|
| 1685 |
import json
|