Update app.py
Browse files
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""
|
| 1096 |
if image_url:
|
| 1097 |
-
|
| 1098 |
-
|
| 1099 |
-
|
| 1100 |
-
|
| 1101 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
-
|
| 1110 |
-
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 1210 |
-
|
| 1211 |
-
|
| 1212 |
-
|
| 1213 |
-
|
| 1214 |
-
|
| 1215 |
-
|
| 1216 |
-
|
| 1217 |
-
|
| 1218 |
-
|
| 1219 |
-
|
| 1220 |
-
|
| 1221 |
-
|
| 1222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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""
|
| 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""
|
| 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()
|