Update app.py
Browse files
app.py
CHANGED
@@ -1092,59 +1092,26 @@ def handsome_chat_completions():
|
|
1092 |
image_url = images[0]
|
1093 |
logging.info(f"Extracted image URL: {image_url}")
|
1094 |
|
|
|
1095 |
if image_url:
|
1096 |
-
|
1097 |
-
|
1098 |
-
|
1099 |
-
|
1100 |
-
|
1101 |
-
|
1102 |
-
|
1103 |
-
|
1104 |
-
|
1105 |
-
|
1106 |
-
|
1107 |
-
|
1108 |
-
|
1109 |
-
|
1110 |
-
|
1111 |
-
|
1112 |
-
|
1113 |
-
|
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,75 +1206,23 @@ def handsome_chat_completions():
|
|
1239 |
image_url = images[0]
|
1240 |
logging.info(f"Extracted image URL: {image_url}")
|
1241 |
|
1242 |
-
|
1243 |
-
|
1244 |
-
|
1245 |
-
|
1246 |
-
|
1247 |
-
|
1248 |
-
|
1249 |
-
|
1250 |
-
|
1251 |
-
|
1252 |
-
|
1253 |
-
|
1254 |
-
|
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 |
-
|
1294 |
-
|
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}, "
|
|
|
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 |
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}, "
|