yangtb24 commited on
Commit
e41a0bb
·
verified ·
1 Parent(s): 470a33a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -35
app.py CHANGED
@@ -1092,26 +1092,59 @@ def handsome_chat_completions():
1092
  image_url = images[0]
1093
  logging.info(f"Extracted image URL: {image_url}")
1094
 
1095
- markdown_image_link = f"![image]({image_url})"
1096
  if image_url:
1097
- chunk_data = {
1098
- "id": f"chatcmpl-{uuid.uuid4()}",
1099
- "object": "chat.completion.chunk",
1100
- "created": int(time.time()),
1101
- "model": model_name,
1102
- "choices": [
1103
- {
1104
- "index": 0,
1105
- "delta": {
1106
- "role": "assistant",
1107
- "content": markdown_image_link
1108
- },
1109
- "finish_reason": None
1110
- }
1111
- ]
1112
- }
1113
- yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
1114
- full_response_content = markdown_image_link
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1115
  else:
1116
  chunk_data = {
1117
  "id": f"chatcmpl-{uuid.uuid4()}",
@@ -1206,23 +1239,75 @@ def handsome_chat_completions():
1206
  image_url = images[0]
1207
  logging.info(f"Extracted image URL: {image_url}")
1208
 
1209
- markdown_image_link = f"![image]({image_url})"
1210
- response_data = {
1211
- "id": f"chatcmpl-{uuid.uuid4()}",
1212
- "object": "chat.completion",
1213
- "created": int(time.time()),
1214
- "model": model_name,
1215
- "choices": [
1216
- {
1217
- "index": 0,
1218
- "message": {
1219
- "role": "assistant",
1220
- "content": markdown_image_link if image_url else "Failed to generate image",
1221
- },
1222
- "finish_reason": "stop",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1223
  }
1224
- ],
1225
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1226
  except (KeyError, ValueError, IndexError) as e:
1227
  logging.error(
1228
  f"解析响应 JSON 失败: {e}, "
@@ -1258,6 +1343,7 @@ def handsome_chat_completions():
1258
  except requests.exceptions.RequestException as e:
1259
  logging.error(f"请求转发异常: {e}")
1260
  return jsonify({"error": str(e)}), 500
 
1261
  else:
1262
  try:
1263
  start_time = time.time()
 
1092
  image_url = images[0]
1093
  logging.info(f"Extracted image URL: {image_url}")
1094
 
 
1095
  if image_url:
1096
+ try:
1097
+ # Fetch the image data
1098
+ image_response = requests.get(image_url, stream=True, timeout=30)
1099
+ image_response.raise_for_status()
1100
+ image_data = image_response.content
1101
+ # Determine image format
1102
+ image_format = image_response.headers.get('Content-Type', '').split('/')[-1].lower()
1103
+ if image_format not in ['png', 'jpeg', 'jpg']:
1104
+ image_format = 'png' # Default to png if format is not recognized
1105
+ # Encode to base64
1106
+ base64_encoded_image = base64.b64encode(image_data).decode('utf-8')
1107
+ # Construct the markdown with base64 image data
1108
+ markdown_image = f"![image](data:image/{image_format};base64,{base64_encoded_image})"
1109
+ chunk_data = {
1110
+ "id": f"chatcmpl-{uuid.uuid4()}",
1111
+ "object": "chat.completion.chunk",
1112
+ "created": int(time.time()),
1113
+ "model": model_name,
1114
+ "choices": [
1115
+ {
1116
+ "index": 0,
1117
+ "delta": {
1118
+ "role": "assistant",
1119
+ "content": markdown_image
1120
+ },
1121
+ "finish_reason": None
1122
+ }
1123
+ ]
1124
+ }
1125
+ yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
1126
+ full_response_content = markdown_image
1127
+ except requests.exceptions.RequestException as e:
1128
+ logging.error(f"Failed to fetch or process image from {image_url}: {e}")
1129
+ chunk_data = {
1130
+ "id": f"chatcmpl-{uuid.uuid4()}",
1131
+ "object": "chat.completion.chunk",
1132
+ "created": int(time.time()),
1133
+ "model": model_name,
1134
+ "choices": [
1135
+ {
1136
+ "index": 0,
1137
+ "delta": {
1138
+ "role": "assistant",
1139
+ "content": "Failed to retrieve and embed image."
1140
+ },
1141
+ "finish_reason": None
1142
+ }
1143
+ ]
1144
+ }
1145
+ yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
1146
+ full_response_content = "Failed to retrieve and embed image."
1147
+
1148
  else:
1149
  chunk_data = {
1150
  "id": f"chatcmpl-{uuid.uuid4()}",
 
1239
  image_url = images[0]
1240
  logging.info(f"Extracted image URL: {image_url}")
1241
 
1242
+ if image_url:
1243
+ try:
1244
+ # Fetch the image data
1245
+ image_response = requests.get(image_url, stream=True, timeout=30)
1246
+ image_response.raise_for_status()
1247
+ image_data = image_response.content
1248
+ # Determine image format
1249
+ image_format = image_response.headers.get('Content-Type', '').split('/')[-1].lower()
1250
+ if image_format not in ['png', 'jpeg', 'jpg']:
1251
+ image_format = 'png' # Default to png if format is not recognized
1252
+
1253
+ # Encode to base64
1254
+ base64_encoded_image = base64.b64encode(image_data).decode('utf-8')
1255
+
1256
+ # Construct the markdown with base64 image data
1257
+ markdown_image = f"![image](data:image/{image_format};base64,{base64_encoded_image})"
1258
+
1259
+ response_data = {
1260
+ "id": f"chatcmpl-{uuid.uuid4()}",
1261
+ "object": "chat.completion",
1262
+ "created": int(time.time()),
1263
+ "model": model_name,
1264
+ "choices": [
1265
+ {
1266
+ "index": 0,
1267
+ "message": {
1268
+ "role": "assistant",
1269
+ "content": markdown_image,
1270
+ },
1271
+ "finish_reason": "stop",
1272
+ }
1273
+ ],
1274
  }
1275
+ except requests.exceptions.RequestException as e:
1276
+ logging.error(f"Failed to fetch or process image from {image_url}: {e}")
1277
+ response_data = {
1278
+ "id": f"chatcmpl-{uuid.uuid4()}",
1279
+ "object": "chat.completion",
1280
+ "created": int(time.time()),
1281
+ "model": model_name,
1282
+ "choices": [
1283
+ {
1284
+ "index": 0,
1285
+ "message": {
1286
+ "role": "assistant",
1287
+ "content": "Failed to retrieve and embed image.",
1288
+ },
1289
+ "finish_reason": "stop",
1290
+ }
1291
+ ],
1292
+ }
1293
+ else:
1294
+ response_data = {
1295
+ "id": f"chatcmpl-{uuid.uuid4()}",
1296
+ "object": "chat.completion",
1297
+ "created": int(time.time()),
1298
+ "model": model_name,
1299
+ "choices": [
1300
+ {
1301
+ "index": 0,
1302
+ "message": {
1303
+ "role": "assistant",
1304
+ "content": "Failed to generate image",
1305
+ },
1306
+ "finish_reason": "stop",
1307
+ }
1308
+ ],
1309
+ }
1310
+
1311
  except (KeyError, ValueError, IndexError) as e:
1312
  logging.error(
1313
  f"解析响应 JSON 失败: {e}, "
 
1343
  except requests.exceptions.RequestException as e:
1344
  logging.error(f"请求转发异常: {e}")
1345
  return jsonify({"error": str(e)}), 500
1346
+ return jsonify({"error": "model not supported"}), 400
1347
  else:
1348
  try:
1349
  start_time = time.time()