Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -192,32 +192,22 @@ async def chat_completions(
|
|
| 192 |
if data.get('model') == 'AkashGen' and "<image_generation>" in msg_data:
|
| 193 |
# 图片生成模型的特殊处理
|
| 194 |
async def process_and_send():
|
| 195 |
-
|
| 196 |
-
if
|
| 197 |
-
|
| 198 |
-
"id": f"chatcmpl-{chat_id}",
|
| 199 |
-
"object": "chat.completion.chunk",
|
| 200 |
-
"created": int(time.time()),
|
| 201 |
-
"model": data.get('model'),
|
| 202 |
-
"choices": [{
|
| 203 |
-
"delta": {"content": end_msg},
|
| 204 |
-
"index": 0,
|
| 205 |
-
"finish_reason": None
|
| 206 |
-
}]
|
| 207 |
-
}
|
| 208 |
-
return f"data: {json.dumps(chunk)}\n\n"
|
| 209 |
return None
|
| 210 |
|
| 211 |
# 创建新的事件循环
|
| 212 |
loop = asyncio.new_event_loop()
|
| 213 |
asyncio.set_event_loop(loop)
|
| 214 |
try:
|
| 215 |
-
|
| 216 |
finally:
|
| 217 |
loop.close()
|
| 218 |
|
| 219 |
-
if
|
| 220 |
-
|
|
|
|
| 221 |
continue
|
| 222 |
|
| 223 |
content_buffer += msg_data
|
|
@@ -410,8 +400,8 @@ async def upload_to_xinyew(image_base64: str, job_id: str) -> Optional[str]:
|
|
| 410 |
print(traceback.format_exc())
|
| 411 |
return None
|
| 412 |
|
| 413 |
-
async def process_image_generation(msg_data: str, session: requests.Session, headers: dict, chat_id: str) ->
|
| 414 |
-
"""
|
| 415 |
match = re.search(r"jobId='([^']+)' prompt='([^']+)' negative='([^']*)'", msg_data)
|
| 416 |
if match:
|
| 417 |
job_id, prompt, negative = match.groups()
|
|
@@ -429,18 +419,54 @@ async def process_image_generation(msg_data: str, session: requests.Session, hea
|
|
| 429 |
# 完成思考部分
|
| 430 |
elapsed_time = time.time() - start_time
|
| 431 |
think_msg += f"\n🤔 Thinking for {elapsed_time:.1f}s...\n"
|
| 432 |
-
think_msg += "</think>"
|
| 433 |
|
| 434 |
-
#
|
| 435 |
-
|
| 436 |
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
else:
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
|
| 445 |
if __name__ == '__main__':
|
| 446 |
import uvicorn
|
|
|
|
| 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
|
|
|
|
| 400 |
print(traceback.format_exc())
|
| 401 |
return None
|
| 402 |
|
| 403 |
+
async def process_image_generation(msg_data: str, session: requests.Session, headers: dict, chat_id: str) -> Optional[list]:
|
| 404 |
+
"""处理图片生成的逻辑,返回多个消息块"""
|
| 405 |
match = re.search(r"jobId='([^']+)' prompt='([^']+)' negative='([^']*)'", msg_data)
|
| 406 |
if match:
|
| 407 |
job_id, prompt, negative = match.groups()
|
|
|
|
| 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 |
+
}]
|
| 466 |
+
})
|
| 467 |
+
|
| 468 |
+
return messages
|
| 469 |
+
return None
|
| 470 |
|
| 471 |
if __name__ == '__main__':
|
| 472 |
import uvicorn
|