Spaces:
Paused
Paused
Upload main.py
Browse files
main.py
CHANGED
|
@@ -233,16 +233,19 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
|
|
| 233 |
images = []
|
| 234 |
for item in content:
|
| 235 |
if not isinstance(item, dict):
|
|
|
|
| 236 |
continue
|
| 237 |
|
| 238 |
-
if "text" in item:
|
| 239 |
-
text_parts.append(item
|
| 240 |
-
elif "image_url" in item:
|
| 241 |
has_image = True
|
| 242 |
-
image_info = item
|
| 243 |
-
if not isinstance(image_info, dict):
|
| 244 |
-
continue
|
| 245 |
url = image_info.get("url", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
if is_base64_image(url):
|
| 247 |
try:
|
| 248 |
base64_str = url.split(",")[1]
|
|
@@ -255,34 +258,43 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
|
|
| 255 |
continue
|
| 256 |
else:
|
| 257 |
images.append({"data": url})
|
|
|
|
|
|
|
| 258 |
extracted_content = " ".join(text_parts).strip()
|
| 259 |
if extracted_content:
|
| 260 |
has_text = True
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
| 262 |
if images:
|
| 263 |
-
|
| 264 |
-
cleaned_messages.append(
|
| 265 |
print("Extracted:", extracted_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
else:
|
| 267 |
-
|
| 268 |
-
has_image = True
|
| 269 |
-
message["content"] = ""
|
| 270 |
-
message["images"] = images
|
| 271 |
-
cleaned_messages.append(message)
|
| 272 |
-
print("Extracted image only.")
|
| 273 |
-
else:
|
| 274 |
-
print("Deleted message with empty content.")
|
| 275 |
elif isinstance(content, str):
|
| 276 |
content_str = content.strip()
|
| 277 |
if content_str:
|
| 278 |
has_text = True
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
| 281 |
print("Retained content:", content_str)
|
| 282 |
else:
|
| 283 |
print("Deleted message with empty content.")
|
| 284 |
else:
|
| 285 |
-
print("Deleted non-expected type of content message
|
| 286 |
|
| 287 |
if not cleaned_messages:
|
| 288 |
raise HTTPException(status_code=400, detail="所有消息的内容均为空。")
|
|
@@ -324,7 +336,7 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
|
|
| 324 |
}
|
| 325 |
|
| 326 |
if is_stream:
|
| 327 |
-
#
|
| 328 |
async def event_generator():
|
| 329 |
async with httpx.AsyncClient(timeout=None) as client_stream:
|
| 330 |
try:
|
|
@@ -460,7 +472,7 @@ async def images_generations(request: Request, api_key: str = Depends(verify_api
|
|
| 460 |
|
| 461 |
print("Received Image Generations JSON:", json.dumps(request_body, ensure_ascii=False))
|
| 462 |
|
| 463 |
-
#
|
| 464 |
if "prompt" not in request_body:
|
| 465 |
return send_error_response("缺少必需的字段: prompt", status_code=400)
|
| 466 |
|
|
@@ -572,7 +584,7 @@ async def images_generations(request: Request, api_key: str = Depends(verify_api
|
|
| 572 |
if not image_bytes:
|
| 573 |
return send_error_response("无法从 URL 下载图像。", status_code=500)
|
| 574 |
|
| 575 |
-
#
|
| 576 |
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
|
| 577 |
|
| 578 |
# 将图片保存到images目录并构建可访问的URL
|
|
|
|
| 233 |
images = []
|
| 234 |
for item in content:
|
| 235 |
if not isinstance(item, dict):
|
| 236 |
+
print(f"跳过非字典类型的内容项: {item}")
|
| 237 |
continue
|
| 238 |
|
| 239 |
+
if "text" in item and isinstance(item["text"], str):
|
| 240 |
+
text_parts.append(item["text"])
|
| 241 |
+
elif "image_url" in item and isinstance(item["image_url"], dict):
|
| 242 |
has_image = True
|
| 243 |
+
image_info = item["image_url"]
|
|
|
|
|
|
|
| 244 |
url = image_info.get("url", "")
|
| 245 |
+
if not isinstance(url, str):
|
| 246 |
+
print(f"跳过非字符串类型的URL: {url}")
|
| 247 |
+
continue
|
| 248 |
+
|
| 249 |
if is_base64_image(url):
|
| 250 |
try:
|
| 251 |
base64_str = url.split(",")[1]
|
|
|
|
| 258 |
continue
|
| 259 |
else:
|
| 260 |
images.append({"data": url})
|
| 261 |
+
|
| 262 |
+
# 合并文本部分
|
| 263 |
extracted_content = " ".join(text_parts).strip()
|
| 264 |
if extracted_content:
|
| 265 |
has_text = True
|
| 266 |
+
new_message = {
|
| 267 |
+
"role": role,
|
| 268 |
+
"content": extracted_content
|
| 269 |
+
}
|
| 270 |
if images:
|
| 271 |
+
new_message["images"] = images
|
| 272 |
+
cleaned_messages.append(new_message)
|
| 273 |
print("Extracted:", extracted_content)
|
| 274 |
+
elif images:
|
| 275 |
+
has_image = True
|
| 276 |
+
new_message = {
|
| 277 |
+
"role": role,
|
| 278 |
+
"content": "",
|
| 279 |
+
"images": images
|
| 280 |
+
}
|
| 281 |
+
cleaned_messages.append(new_message)
|
| 282 |
+
print("Extracted image only.")
|
| 283 |
else:
|
| 284 |
+
print("Deleted message with empty content.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
elif isinstance(content, str):
|
| 286 |
content_str = content.strip()
|
| 287 |
if content_str:
|
| 288 |
has_text = True
|
| 289 |
+
cleaned_messages.append({
|
| 290 |
+
"role": role,
|
| 291 |
+
"content": content_str
|
| 292 |
+
})
|
| 293 |
print("Retained content:", content_str)
|
| 294 |
else:
|
| 295 |
print("Deleted message with empty content.")
|
| 296 |
else:
|
| 297 |
+
print(f"Deleted non-expected type of content message: {type(content)}")
|
| 298 |
|
| 299 |
if not cleaned_messages:
|
| 300 |
raise HTTPException(status_code=400, detail="所有消息的内容均为空。")
|
|
|
|
| 336 |
}
|
| 337 |
|
| 338 |
if is_stream:
|
| 339 |
+
# 流式响��处理
|
| 340 |
async def event_generator():
|
| 341 |
async with httpx.AsyncClient(timeout=None) as client_stream:
|
| 342 |
try:
|
|
|
|
| 472 |
|
| 473 |
print("Received Image Generations JSON:", json.dumps(request_body, ensure_ascii=False))
|
| 474 |
|
| 475 |
+
# 验证必的字段
|
| 476 |
if "prompt" not in request_body:
|
| 477 |
return send_error_response("缺少必需的字段: prompt", status_code=400)
|
| 478 |
|
|
|
|
| 584 |
if not image_bytes:
|
| 585 |
return send_error_response("无法从 URL 下载图像。", status_code=500)
|
| 586 |
|
| 587 |
+
# 转为 Base64
|
| 588 |
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
|
| 589 |
|
| 590 |
# 将图片保存到images目录并构建可访问的URL
|