Update main.py
Browse files
main.py
CHANGED
|
@@ -172,60 +172,6 @@ async def chat_completions(
|
|
| 172 |
stream=True
|
| 173 |
)
|
| 174 |
|
| 175 |
-
async def process_image_gen(job_id, prompt, negative):
|
| 176 |
-
"""处理图片生成的异步函数"""
|
| 177 |
-
print(f"Starting image generation process for job_id: {job_id}")
|
| 178 |
-
|
| 179 |
-
# 立即发送思考开始的消息
|
| 180 |
-
start_time = time.time()
|
| 181 |
-
think_msg = "<think>\n"
|
| 182 |
-
think_msg += "🎨 Generating image...\n\n"
|
| 183 |
-
think_msg += f"Prompt: {prompt}\n"
|
| 184 |
-
|
| 185 |
-
# 发送思考开始消息
|
| 186 |
-
chunk1 = {
|
| 187 |
-
"id": f"chatcmpl-{chat_id}",
|
| 188 |
-
"object": "chat.completion.chunk",
|
| 189 |
-
"created": int(time.time()),
|
| 190 |
-
"model": data.get('model'),
|
| 191 |
-
"choices": [{
|
| 192 |
-
"delta": {"content": think_msg},
|
| 193 |
-
"index": 0,
|
| 194 |
-
"finish_reason": None
|
| 195 |
-
}]
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
# 检查图片状态和上传
|
| 199 |
-
image_base64 = await check_image_status(session, job_id, headers)
|
| 200 |
-
if image_base64:
|
| 201 |
-
result = await upload_to_xinyew(image_base64, job_id)
|
| 202 |
-
else:
|
| 203 |
-
result = None
|
| 204 |
-
|
| 205 |
-
# 发送结束消息
|
| 206 |
-
elapsed_time = time.time() - start_time
|
| 207 |
-
end_msg = f"\n🤔 Thinking for {elapsed_time:.1f}s...\n"
|
| 208 |
-
end_msg += "</think>\n\n"
|
| 209 |
-
if result:
|
| 210 |
-
end_msg += f""
|
| 211 |
-
else:
|
| 212 |
-
end_msg += "*Image generation or upload failed.*\n"
|
| 213 |
-
|
| 214 |
-
# 发送结束消息
|
| 215 |
-
chunk2 = {
|
| 216 |
-
"id": f"chatcmpl-{chat_id}",
|
| 217 |
-
"object": "chat.completion.chunk",
|
| 218 |
-
"created": int(time.time()),
|
| 219 |
-
"model": data.get('model'),
|
| 220 |
-
"choices": [{
|
| 221 |
-
"delta": {"content": end_msg},
|
| 222 |
-
"index": 0,
|
| 223 |
-
"finish_reason": None
|
| 224 |
-
}]
|
| 225 |
-
}
|
| 226 |
-
|
| 227 |
-
return [chunk1, chunk2]
|
| 228 |
-
|
| 229 |
def generate():
|
| 230 |
content_buffer = ""
|
| 231 |
for line in response.iter_lines():
|
|
@@ -245,22 +191,24 @@ async def chat_completions(
|
|
| 245 |
# 在处理消息时先判断模型类型
|
| 246 |
if data.get('model') == 'AkashGen' and "<image_generation>" in msg_data:
|
| 247 |
# 图片生成模型的特殊处理
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
| 264 |
|
| 265 |
content_buffer += msg_data
|
| 266 |
|
|
@@ -282,7 +230,7 @@ async def chat_completions(
|
|
| 282 |
"id": f"chatcmpl-{chat_id}",
|
| 283 |
"object": "chat.completion.chunk",
|
| 284 |
"created": int(time.time()),
|
| 285 |
-
"model": data.get('model'),
|
| 286 |
"choices": [{
|
| 287 |
"delta": {},
|
| 288 |
"index": 0,
|
|
@@ -459,104 +407,59 @@ async def process_image_generation(msg_data: str, session: requests.Session, hea
|
|
| 459 |
job_id, prompt, negative = match.groups()
|
| 460 |
print(f"Starting image generation process for job_id: {job_id}")
|
| 461 |
|
| 462 |
-
#
|
| 463 |
-
messages = []
|
| 464 |
-
|
| 465 |
-
# 发送思考开始标记
|
| 466 |
-
messages.append({
|
| 467 |
-
"id": f"chatcmpl-{chat_id}-1",
|
| 468 |
-
"object": "chat.completion.chunk",
|
| 469 |
-
"created": int(time.time()),
|
| 470 |
-
"model": "AkashGen",
|
| 471 |
-
"choices": [{
|
| 472 |
-
"delta": {"content": "<think>\n"},
|
| 473 |
-
"index": 0,
|
| 474 |
-
"finish_reason": None
|
| 475 |
-
}]
|
| 476 |
-
})
|
| 477 |
-
|
| 478 |
-
# 发送生成图片的提示
|
| 479 |
-
messages.append({
|
| 480 |
-
"id": f"chatcmpl-{chat_id}-2",
|
| 481 |
-
"object": "chat.completion.chunk",
|
| 482 |
-
"created": int(time.time()),
|
| 483 |
-
"model": "AkashGen",
|
| 484 |
-
"choices": [{
|
| 485 |
-
"delta": {"content": "🎨 Generating image...\n\n"},
|
| 486 |
-
"index": 0,
|
| 487 |
-
"finish_reason": None
|
| 488 |
-
}]
|
| 489 |
-
})
|
| 490 |
-
|
| 491 |
-
# 发送提示词
|
| 492 |
-
messages.append({
|
| 493 |
-
"id": f"chatcmpl-{chat_id}-3",
|
| 494 |
-
"object": "chat.completion.chunk",
|
| 495 |
-
"created": int(time.time()),
|
| 496 |
-
"model": "AkashGen",
|
| 497 |
-
"choices": [{
|
| 498 |
-
"delta": {"content": f"Prompt: {prompt}\n\n"},
|
| 499 |
-
"index": 0,
|
| 500 |
-
"finish_reason": None
|
| 501 |
-
}]
|
| 502 |
-
})
|
| 503 |
-
|
| 504 |
-
# 记录开始时间
|
| 505 |
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
| 506 |
|
| 507 |
# 检查图片状态和上传
|
| 508 |
result = await check_image_status(session, job_id, headers)
|
| 509 |
|
| 510 |
-
#
|
| 511 |
elapsed_time = time.time() - start_time
|
|
|
|
|
|
|
| 512 |
|
| 513 |
-
#
|
| 514 |
-
messages
|
| 515 |
-
"id": f"chatcmpl-{chat_id}-4",
|
| 516 |
-
"object": "chat.completion.chunk",
|
| 517 |
-
"created": int(time.time()),
|
| 518 |
-
"model": "AkashGen",
|
| 519 |
-
"choices": [{
|
| 520 |
-
"delta": {"content": f"🤔 Thinking for {elapsed_time:.1f}s...\n"},
|
| 521 |
-
"index": 0,
|
| 522 |
-
"finish_reason": None
|
| 523 |
-
}]
|
| 524 |
-
})
|
| 525 |
|
| 526 |
-
#
|
| 527 |
messages.append({
|
| 528 |
-
"id": f"chatcmpl-{chat_id}-
|
| 529 |
"object": "chat.completion.chunk",
|
| 530 |
"created": int(time.time()),
|
| 531 |
"model": "AkashGen",
|
| 532 |
"choices": [{
|
| 533 |
-
"delta": {"content":
|
| 534 |
"index": 0,
|
| 535 |
"finish_reason": None
|
| 536 |
}]
|
| 537 |
})
|
| 538 |
|
| 539 |
-
#
|
| 540 |
if result:
|
|
|
|
| 541 |
messages.append({
|
| 542 |
-
"id": f"chatcmpl-{chat_id}-
|
| 543 |
"object": "chat.completion.chunk",
|
| 544 |
"created": int(time.time()),
|
| 545 |
"model": "AkashGen",
|
| 546 |
"choices": [{
|
| 547 |
-
"delta": {"content":
|
| 548 |
"index": 0,
|
| 549 |
"finish_reason": None
|
| 550 |
}]
|
| 551 |
})
|
| 552 |
else:
|
|
|
|
| 553 |
messages.append({
|
| 554 |
-
"id": f"chatcmpl-{chat_id}-
|
| 555 |
"object": "chat.completion.chunk",
|
| 556 |
"created": int(time.time()),
|
| 557 |
"model": "AkashGen",
|
| 558 |
"choices": [{
|
| 559 |
-
"delta": {"content":
|
| 560 |
"index": 0,
|
| 561 |
"finish_reason": None
|
| 562 |
}]
|
|
|
|
| 172 |
stream=True
|
| 173 |
)
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
def generate():
|
| 176 |
content_buffer = ""
|
| 177 |
for line in response.iter_lines():
|
|
|
|
| 191 |
# 在处理消息时先判断模型类型
|
| 192 |
if data.get('model') == 'AkashGen' and "<image_generation>" in msg_data:
|
| 193 |
# 图片生成模型的特殊处理
|
| 194 |
+
async def process_and_send():
|
| 195 |
+
messages = await process_image_generation(msg_data, session, headers, chat_id)
|
| 196 |
+
if messages:
|
| 197 |
+
return messages
|
| 198 |
+
return None
|
| 199 |
+
|
| 200 |
+
# 创建新的事件循环
|
| 201 |
+
loop = asyncio.new_event_loop()
|
| 202 |
+
asyncio.set_event_loop(loop)
|
| 203 |
+
try:
|
| 204 |
+
result_messages = loop.run_until_complete(process_and_send())
|
| 205 |
+
finally:
|
| 206 |
+
loop.close()
|
| 207 |
+
|
| 208 |
+
if result_messages:
|
| 209 |
+
for message in result_messages:
|
| 210 |
+
yield f"data: {json.dumps(message)}\n\n"
|
| 211 |
+
continue
|
| 212 |
|
| 213 |
content_buffer += msg_data
|
| 214 |
|
|
|
|
| 230 |
"id": f"chatcmpl-{chat_id}",
|
| 231 |
"object": "chat.completion.chunk",
|
| 232 |
"created": int(time.time()),
|
| 233 |
+
"model": data.get('model'), # 使用请求中指定的模型
|
| 234 |
"choices": [{
|
| 235 |
"delta": {},
|
| 236 |
"index": 0,
|
|
|
|
| 407 |
job_id, prompt, negative = match.groups()
|
| 408 |
print(f"Starting image generation process for job_id: {job_id}")
|
| 409 |
|
| 410 |
+
# 发送思考开始的消息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
start_time = time.time()
|
| 412 |
+
think_msg = "<think>\n"
|
| 413 |
+
think_msg += "🎨 Generating image...\n\n"
|
| 414 |
+
think_msg += f"Prompt: {prompt}\n"
|
| 415 |
|
| 416 |
# 检查图片状态和上传
|
| 417 |
result = await check_image_status(session, job_id, headers)
|
| 418 |
|
| 419 |
+
# 完成思考部分
|
| 420 |
elapsed_time = time.time() - start_time
|
| 421 |
+
think_msg += f"\n🤔 Thinking for {elapsed_time:.1f}s...\n"
|
| 422 |
+
think_msg += "</think>"
|
| 423 |
|
| 424 |
+
# 返回两个独立的消息块
|
| 425 |
+
messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
+
# 第一个消息块:思考过程
|
| 428 |
messages.append({
|
| 429 |
+
"id": f"chatcmpl-{chat_id}-think",
|
| 430 |
"object": "chat.completion.chunk",
|
| 431 |
"created": int(time.time()),
|
| 432 |
"model": "AkashGen",
|
| 433 |
"choices": [{
|
| 434 |
+
"delta": {"content": think_msg},
|
| 435 |
"index": 0,
|
| 436 |
"finish_reason": None
|
| 437 |
}]
|
| 438 |
})
|
| 439 |
|
| 440 |
+
# 第二个消息块:图片结果
|
| 441 |
if result:
|
| 442 |
+
image_msg = f"\n\n"
|
| 443 |
messages.append({
|
| 444 |
+
"id": f"chatcmpl-{chat_id}-image",
|
| 445 |
"object": "chat.completion.chunk",
|
| 446 |
"created": int(time.time()),
|
| 447 |
"model": "AkashGen",
|
| 448 |
"choices": [{
|
| 449 |
+
"delta": {"content": image_msg},
|
| 450 |
"index": 0,
|
| 451 |
"finish_reason": None
|
| 452 |
}]
|
| 453 |
})
|
| 454 |
else:
|
| 455 |
+
fail_msg = "\n\n*Image generation or upload failed.*"
|
| 456 |
messages.append({
|
| 457 |
+
"id": f"chatcmpl-{chat_id}-fail",
|
| 458 |
"object": "chat.completion.chunk",
|
| 459 |
"created": int(time.time()),
|
| 460 |
"model": "AkashGen",
|
| 461 |
"choices": [{
|
| 462 |
+
"delta": {"content": fail_msg},
|
| 463 |
"index": 0,
|
| 464 |
"finish_reason": None
|
| 465 |
}]
|