Spaces:
Paused
Paused
Upload main.py
Browse files
main.py
CHANGED
|
@@ -232,29 +232,30 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
|
|
| 232 |
print(f"跳过空内容的消息: {message}")
|
| 233 |
continue
|
| 234 |
|
|
|
|
|
|
|
|
|
|
| 235 |
if isinstance(content, list):
|
| 236 |
text_parts = []
|
| 237 |
images = []
|
|
|
|
| 238 |
for item in content:
|
| 239 |
if not isinstance(item, dict):
|
| 240 |
print(f"跳过非字典类型的内容项: {item}")
|
| 241 |
continue
|
| 242 |
-
|
| 243 |
-
item_type = item.get("type")
|
| 244 |
-
if not item_type:
|
| 245 |
-
print(f"跳过没有type的内容项: {item}")
|
| 246 |
-
continue
|
| 247 |
|
| 248 |
-
|
|
|
|
| 249 |
text_parts.append(item["text"])
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
has_image = True
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
if not isinstance(url, str):
|
| 255 |
-
print(f"跳过非字符串类型的URL: {url}")
|
| 256 |
-
continue
|
| 257 |
-
|
| 258 |
if is_base64_image(url):
|
| 259 |
try:
|
| 260 |
base64_str = url.split(",")[1]
|
|
@@ -268,23 +269,20 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
|
|
| 268 |
else:
|
| 269 |
images.append({"data": url})
|
| 270 |
else:
|
| 271 |
-
print(f"
|
| 272 |
-
continue
|
| 273 |
|
| 274 |
# 合并文本部分
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
if extracted_content:
|
| 279 |
-
has_text = True
|
| 280 |
-
new_message["content"] = extracted_content
|
| 281 |
else:
|
| 282 |
-
new_message["content"] = ""
|
| 283 |
|
|
|
|
| 284 |
if images:
|
| 285 |
new_message["images"] = images
|
| 286 |
|
| 287 |
-
|
|
|
|
| 288 |
cleaned_messages.append(new_message)
|
| 289 |
print(f"添加了新消息: {new_message}")
|
| 290 |
else:
|
|
@@ -294,10 +292,8 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
|
|
| 294 |
content_str = content.strip()
|
| 295 |
if content_str:
|
| 296 |
has_text = True
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
"content": content_str
|
| 300 |
-
})
|
| 301 |
print("保留了文本内容:", content_str)
|
| 302 |
else:
|
| 303 |
print("删除了空内容的消息")
|
|
@@ -382,7 +378,7 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
|
|
| 382 |
new_sse_line = f"data: {json.dumps(new_sse_json, ensure_ascii=False)}\n\n"
|
| 383 |
yield new_sse_line
|
| 384 |
except json.JSONDecodeError:
|
| 385 |
-
print("JSON
|
| 386 |
continue
|
| 387 |
except httpx.RequestError as exc:
|
| 388 |
print(f"外部API请求失败: {exc}")
|
|
@@ -576,7 +572,7 @@ async def images_generations(request: Request, api_key: str = Depends(verify_api
|
|
| 576 |
|
| 577 |
print(f"提取的路径: {extracted_path}")
|
| 578 |
|
| 579 |
-
# Step 5:
|
| 580 |
storage_url = f"https://api.chaton.ai/storage/{extracted_path}"
|
| 581 |
print(f"存储URL: {storage_url}")
|
| 582 |
|
|
@@ -638,7 +634,7 @@ def main():
|
|
| 638 |
|
| 639 |
# 检查 API_KEY 是否设置
|
| 640 |
if not os.environ.get("API_KEY"):
|
| 641 |
-
print("警告: API_KEY
|
| 642 |
|
| 643 |
# 确保 images 目录存在
|
| 644 |
if not os.path.exists("images"):
|
|
|
|
| 232 |
print(f"跳过空内容的消息: {message}")
|
| 233 |
continue
|
| 234 |
|
| 235 |
+
# 创建新的消息字典
|
| 236 |
+
new_message = {"role": role}
|
| 237 |
+
|
| 238 |
if isinstance(content, list):
|
| 239 |
text_parts = []
|
| 240 |
images = []
|
| 241 |
+
|
| 242 |
for item in content:
|
| 243 |
if not isinstance(item, dict):
|
| 244 |
print(f"跳过非字典类型的内容项: {item}")
|
| 245 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
# 处理文本内容
|
| 248 |
+
if isinstance(item.get("text"), str):
|
| 249 |
text_parts.append(item["text"])
|
| 250 |
+
has_text = True
|
| 251 |
+
continue
|
| 252 |
+
|
| 253 |
+
# 处理图片内容
|
| 254 |
+
image_url = item.get("image_url")
|
| 255 |
+
if isinstance(image_url, dict) and isinstance(image_url.get("url"), str):
|
| 256 |
has_image = True
|
| 257 |
+
url = image_url["url"]
|
| 258 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
if is_base64_image(url):
|
| 260 |
try:
|
| 261 |
base64_str = url.split(",")[1]
|
|
|
|
| 269 |
else:
|
| 270 |
images.append({"data": url})
|
| 271 |
else:
|
| 272 |
+
print(f"跳过无效的图片URL: {image_url}")
|
|
|
|
| 273 |
|
| 274 |
# 合并文本部分
|
| 275 |
+
if text_parts:
|
| 276 |
+
new_message["content"] = " ".join(text_parts).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
else:
|
| 278 |
+
new_message["content"] = ""
|
| 279 |
|
| 280 |
+
# 添加图片(如果有)
|
| 281 |
if images:
|
| 282 |
new_message["images"] = images
|
| 283 |
|
| 284 |
+
# 只有当消息有文本或图片时才添加
|
| 285 |
+
if text_parts or images:
|
| 286 |
cleaned_messages.append(new_message)
|
| 287 |
print(f"添加了新消息: {new_message}")
|
| 288 |
else:
|
|
|
|
| 292 |
content_str = content.strip()
|
| 293 |
if content_str:
|
| 294 |
has_text = True
|
| 295 |
+
new_message["content"] = content_str
|
| 296 |
+
cleaned_messages.append(new_message)
|
|
|
|
|
|
|
| 297 |
print("保留了文本内容:", content_str)
|
| 298 |
else:
|
| 299 |
print("删除了空内容的消息")
|
|
|
|
| 378 |
new_sse_line = f"data: {json.dumps(new_sse_json, ensure_ascii=False)}\n\n"
|
| 379 |
yield new_sse_line
|
| 380 |
except json.JSONDecodeError:
|
| 381 |
+
print("JSON解析错误")
|
| 382 |
continue
|
| 383 |
except httpx.RequestError as exc:
|
| 384 |
print(f"外部API请求失败: {exc}")
|
|
|
|
| 572 |
|
| 573 |
print(f"提取的路径: {extracted_path}")
|
| 574 |
|
| 575 |
+
# Step 5: 拼接最���的存储URL
|
| 576 |
storage_url = f"https://api.chaton.ai/storage/{extracted_path}"
|
| 577 |
print(f"存储URL: {storage_url}")
|
| 578 |
|
|
|
|
| 634 |
|
| 635 |
# 检查 API_KEY 是否设置
|
| 636 |
if not os.environ.get("API_KEY"):
|
| 637 |
+
print("警告: API_KEY 环境变量未设置。客户端验���将无法正常工作。")
|
| 638 |
|
| 639 |
# 确保 images 目录存在
|
| 640 |
if not os.path.exists("images"):
|