Update app.py
Browse files
app.py
CHANGED
|
@@ -1038,84 +1038,89 @@ def handsome_images_generations():
|
|
| 1038 |
"Content-Type": "application/json"
|
| 1039 |
}
|
| 1040 |
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
siliconflow_data["batch_size"] = 1
|
| 1055 |
-
if siliconflow_data["batch_size"] > 4:
|
| 1056 |
-
siliconflow_data["batch_size"] = 4
|
| 1057 |
|
| 1058 |
-
|
| 1059 |
-
siliconflow_data["
|
| 1060 |
-
|
| 1061 |
-
siliconflow_data["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1062 |
|
| 1063 |
-
|
| 1064 |
-
|
| 1065 |
-
if siliconflow_data["guidance_scale"] > 100:
|
| 1066 |
-
siliconflow_data["guidance_scale"] = 100
|
| 1067 |
|
| 1068 |
-
|
| 1069 |
-
|
| 1070 |
-
|
| 1071 |
-
|
| 1072 |
-
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
|
| 1076 |
-
json=siliconflow_data,
|
| 1077 |
-
timeout=120
|
| 1078 |
-
)
|
| 1079 |
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
| 1090 |
-
|
| 1091 |
-
|
| 1092 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
-
|
| 1110 |
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
-
|
|
|
|
|
|
|
| 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
|