yangtb24 commited on
Commit
47be5c2
1 Parent(s): b918005

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -1003,6 +1003,10 @@ def handsome_embeddings():
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):
@@ -1037,7 +1041,9 @@ def handsome_images_generations():
1037
  "Authorization": f"Bearer {api_key}",
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 = {
@@ -1090,16 +1096,37 @@ def handsome_images_generations():
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}, "
@@ -1111,10 +1138,7 @@ def handsome_images_generations():
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}")
 
1003
  except requests.exceptions.RequestException as e:
1004
  return jsonify({"error": str(e)}), 500
1005
 
1006
+ import base64
1007
+ import io
1008
+ from PIL import Image
1009
+
1010
  @app.route('/handsome/v1/images/generations', methods=['POST'])
1011
  def handsome_images_generations():
1012
  if not check_authorization(request):
 
1041
  "Authorization": f"Bearer {api_key}",
1042
  "Content-Type": "application/json"
1043
  }
1044
+
1045
+ response_data = {}
1046
+
1047
  if "stable-diffusion" in model_name:
1048
  # Map OpenAI-style parameters to SiliconFlow's parameters
1049
  siliconflow_data = {
 
1096
 
1097
  try:
1098
  images = response_json.get("images", [])
1099
+ openai_images = []
1100
+ for image_url in images:
1101
+ if data.get("response_format") == "b64_json":
1102
+ try:
1103
+ image_data = requests.get(image_url, stream=True).raw
1104
+ image = Image.open(image_data)
1105
+ buffered = io.BytesIO()
1106
+ image.save(buffered, format="PNG")
1107
+ img_str = base64.b64encode(buffered.getvalue()).decode()
1108
+ openai_images.append({"b64_json": img_str})
1109
+ except Exception as e:
1110
+ logging.error(f"图片转base64失败: {e}")
1111
+ openai_images.append({"url": image_url})
1112
+ else:
1113
+ openai_images.append({"url": image_url})
1114
+
1115
+ response_data = {
1116
+ "created": int(time.time()),
1117
+ "data": openai_images
1118
+ }
1119
+
1120
  except (KeyError, ValueError, IndexError) as e:
1121
  logging.error(
1122
  f"解析响应 JSON 失败: {e}, "
1123
  f"完整内容: {response_json}"
1124
  )
1125
+ response_data = {
1126
+ "created": int(time.time()),
1127
+ "data": []
1128
+ }
1129
+
1130
 
1131
  logging.info(
1132
  f"使用的key: {api_key}, "
 
1138
  request_timestamps.append(time.time())
1139
  token_counts.append(0) # Image generation doesn't use tokens
1140
 
1141
+ return jsonify(response_data)
 
 
 
1142
 
1143
  except requests.exceptions.RequestException as e:
1144
  logging.error(f"请求转发异常: {e}")