hzruo commited on
Commit
c78f509
·
verified ·
1 Parent(s): 6f52fff

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +54 -28
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
- end_msg = await process_image_generation(msg_data, session, headers, chat_id)
196
- if end_msg:
197
- chunk = {
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
- result = loop.run_until_complete(process_and_send())
216
  finally:
217
  loop.close()
218
 
219
- if result:
220
- yield result
 
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) -> 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
- end_msg = think_msg + "\n\n" # 添加空行分隔
436
 
437
- if result: # result 现在是上传后的图片URL
438
- end_msg += f"![Generated Image]({result})"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
  else:
440
- end_msg += "*Image generation or upload failed.*\n"
441
-
442
- return end_msg
443
- return ""
 
 
 
 
 
 
 
 
 
 
 
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![Generated Image]({result})"
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