yangtb24 commited on
Commit
4497eab
1 Parent(s): 182b468

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py CHANGED
@@ -1003,6 +1003,120 @@ def handsome_embeddings():
1003
  except requests.exceptions.RequestException as e:
1004
  return jsonify({"error": str(e)}), 500
1005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1006
  if __name__ == '__main__':
1007
  import json
1008
  logging.info(f"环境变量:{os.environ}")
 
1003
  except requests.exceptions.RequestException as e:
1004
  return jsonify({"error": str(e)}), 500
1005
 
1006
+ @app.route('/handsome/v1/images/generations', methods=['POST'])
1007
+ def handsome_images_generations():
1008
+ if not check_authorization(request):
1009
+ return jsonify({"error": "Unauthorized"}), 401
1010
+
1011
+ data = request.get_json()
1012
+ if not data or 'model' not in data:
1013
+ return jsonify({"error": "Invalid request data"}), 400
1014
+
1015
+ model_name = data.get('model')
1016
+
1017
+ request_type = determine_request_type(
1018
+ model_name,
1019
+ image_models,
1020
+ free_image_models
1021
+ )
1022
+
1023
+ api_key = select_key(request_type, model_name)
1024
+
1025
+ if not api_key:
1026
+ return jsonify(
1027
+ {
1028
+ "error": (
1029
+ "No available API key for this "
1030
+ "request type or all keys have "
1031
+ "reached their limits"
1032
+ )
1033
+ }
1034
+ ), 429
1035
+
1036
+ headers = {
1037
+ "Authorization": f"Bearer {api_key}",
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
1122
  logging.info(f"环境变量:{os.environ}")