yangtb24 commited on
Commit
47b958d
·
verified ·
1 Parent(s): 733a491

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -1097,20 +1097,27 @@ def handsome_images_generations():
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()),
 
1097
  try:
1098
  images = response_json.get("images", [])
1099
  openai_images = []
1100
+ for item in images:
1101
+ if isinstance(item, dict) and "url" in item:
1102
+ image_url = item["url"]
1103
+ print(f"image_url: {image_url}") # 打印 URL
1104
+ if data.get("response_format") == "b64_json":
1105
+ try:
1106
+ image_data = requests.get(image_url, stream=True).raw
1107
+ image = Image.open(image_data)
1108
+ buffered = io.BytesIO()
1109
+ image.save(buffered, format="PNG")
1110
+ img_str = base64.b64encode(buffered.getvalue()).decode()
1111
+ openai_images.append({"b64_json": img_str})
1112
+ except Exception as e:
1113
+ logging.error(f"图片转base64失败: {e}")
1114
+ openai_images.append({"url": image_url})
1115
+ else:
1116
+ openai_images.append({"url": image_url})
1117
  else:
1118
+ logging.error(f"无效的图片数据: {item}")
1119
+ openai_images.append({"url": item})
1120
+
1121
 
1122
  response_data = {
1123
  "created": int(time.time()),