mistpe commited on
Commit
59bf28d
·
verified ·
1 Parent(s): d5e38b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -47
app.py CHANGED
@@ -403,27 +403,164 @@ def upload_image_to_wechat(image_data):
403
  logging.error(error_msg)
404
  raise
405
 
406
- def process_long_running_task(messages, message_type='text', image_data=None):
407
- """
408
- 处理长时间运行的任务,支持文本对话和图片识别
409
- Args:
410
- messages: 消息历史记录列表
411
- message_type: 消息类型,'text'或'image'
412
- image_data: 图片相关数据,包含media_id等信息
413
- Returns:
414
- dict: 包含处理结果的字典
415
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  try:
417
- logging.info(f"开始调用AI服务,消息类型: {message_type}")
418
 
419
  if message_type == 'image':
420
  try:
421
- # 获取图片内容
422
  image_content = get_image_content(image_data['media_id'])
423
- # 将图片内容转换为base64
424
  image_base64 = base64.b64encode(image_content).decode('utf-8')
425
 
426
- # 构建图像识别请求
427
  image_messages = [
428
  {
429
  "role": "user",
@@ -439,18 +576,17 @@ def process_long_running_task(messages, message_type='text', image_data=None):
439
  }
440
  ]
441
 
442
- logging.info("开始调用图像识别模型")
443
  image_response = client.chat.completions.create(
444
  model="gpt-4o-mini",
445
  messages=image_messages,
446
  max_tokens=300,
447
  timeout=60
448
  )
449
- logging.info("图像识别完成")
450
 
451
- # 检查响应是否成功
452
  if not image_response.choices:
453
- raise Exception("图像识别服务未返回有效结果")
454
 
455
  return {
456
  "type": "text",
@@ -458,16 +594,16 @@ def process_long_running_task(messages, message_type='text', image_data=None):
458
  }
459
 
460
  except requests.exceptions.RequestException as e:
461
- logging.error(f"获取或处理图片时发生网络错误: {str(e)}")
462
  raise
463
  except Exception as e:
464
- logging.error(f"图像识别过程中发生错误: {str(e)}")
465
  raise
466
 
467
  else:
468
- # 处理文本消息
469
  try:
470
- logging.info("开始处理文本消息")
471
  response = client.chat.completions.create(
472
  model="o3-mini",
473
  messages=messages,
@@ -476,15 +612,15 @@ def process_long_running_task(messages, message_type='text', image_data=None):
476
  timeout=60
477
  )
478
 
479
- # 检查是否需要生成图片
480
  if response.choices[0].message.tool_calls:
481
  tool_call = response.choices[0].message.tool_calls[0]
482
  if tool_call.function.name == "generate_image":
483
  try:
484
- logging.info("检测到图片生成请求")
485
  args = json.loads(tool_call.function.arguments)
486
 
487
- # 调用图片生成服务
488
  image_generation_response = requests.post(
489
  IMAGE_MODEL_URL,
490
  headers={
@@ -492,7 +628,7 @@ def process_long_running_task(messages, message_type='text', image_data=None):
492
  'Authorization': f'Bearer {IMAGE_MODEL_KEY}'
493
  },
494
  json={
495
- "model": "grok-latest-image",
496
  "messages": [{
497
  "role": "user",
498
  "content": args['prompt']
@@ -504,22 +640,20 @@ def process_long_running_task(messages, message_type='text', image_data=None):
504
  generation_result = image_generation_response.json()
505
 
506
  if 'choices' not in generation_result or not generation_result['choices']:
507
- raise Exception("图片生成服务未返回有效结果")
508
-
509
- # 从markdown格式中提取URL
510
  markdown_content = generation_result['choices'][0]['message']['content']
511
- image_url_match = re.search(r'\!\[image\]\((.*?)\)', markdown_content)
512
- if not image_url_match:
513
- raise Exception("无法从响应中提取图片URL")
514
-
515
- image_url = image_url_match.group(1)
516
 
517
- # 下载生成的图片
518
- img_response = requests.get(image_url, timeout=30)
519
- img_response.raise_for_status()
520
 
521
- # 上传图片到微信服务器
522
- media_id = upload_image_to_wechat(img_response.content)
523
 
524
  return {
525
  "type": "image",
@@ -527,30 +661,29 @@ def process_long_running_task(messages, message_type='text', image_data=None):
527
  }
528
 
529
  except requests.exceptions.RequestException as e:
530
- logging.error(f"图片生成过程中发生网络错误: {str(e)}")
531
  raise
532
  except Exception as e:
533
- logging.error(f"图片生成过程中发生错误: {str(e)}")
534
  raise
535
 
536
- # 返回文本响应
537
  return {
538
  "type": "text",
539
  "content": response.choices[0].message.content
540
  }
541
 
542
  except requests.exceptions.RequestException as e:
543
- logging.error(f"处理文本消息时发生网络错误: {str(e)}")
544
  raise
545
  except Exception as e:
546
- logging.error(f"处理文本消息时发生错误: {str(e)}")
547
  raise
548
-
549
  except Exception as e:
550
- logging.error(f"API调用错误: {str(e)}")
551
  raise
552
 
553
-
554
  def handle_async_task(session, task_id, messages=None, message_type='text', message_data=None):
555
  """
556
  处理异步任务,支持文本对话和图片识别
 
403
  logging.error(error_msg)
404
  raise
405
 
406
+ # def process_long_running_task(messages, message_type='text', image_data=None):
407
+ # """
408
+ # 处理长时间运行的任务,支持文本对话和图片识别
409
+ # Args:
410
+ # messages: 消息历史记录列表
411
+ # message_type: 消息类型,'text'或'image'
412
+ # image_data: 图片相关数据,包含media_id等信息
413
+ # Returns:
414
+ # dict: 包含处理结果的字典
415
+ # """
416
+ # try:
417
+ # logging.info(f"开始调用AI服务,消息类型: {message_type}")
418
+
419
+ # if message_type == 'image':
420
+ # try:
421
+ # # 获取图片内容
422
+ # image_content = get_image_content(image_data['media_id'])
423
+ # # 将图片内容转换为base64
424
+ # image_base64 = base64.b64encode(image_content).decode('utf-8')
425
+
426
+ # # 构建图像识别请求
427
+ # image_messages = [
428
+ # {
429
+ # "role": "user",
430
+ # "content": [
431
+ # {"type": "text", "text": "请详细描述这张图片中的内容,包括主要对象、场景、活动等关键信息"},
432
+ # {
433
+ # "type": "image_url",
434
+ # "image_url": {
435
+ # "url": f"data:image/jpeg;base64,{image_base64}"
436
+ # }
437
+ # }
438
+ # ]
439
+ # }
440
+ # ]
441
+
442
+ # logging.info("开始调用图像识别模型")
443
+ # image_response = client.chat.completions.create(
444
+ # model="gpt-4o-mini",
445
+ # messages=image_messages,
446
+ # max_tokens=300,
447
+ # timeout=60
448
+ # )
449
+ # logging.info("图像识别完成")
450
+
451
+ # # 检查响应是否成功
452
+ # if not image_response.choices:
453
+ # raise Exception("图像识别服务未返回有效结果")
454
+
455
+ # return {
456
+ # "type": "text",
457
+ # "content": image_response.choices[0].message.content
458
+ # }
459
+
460
+ # except requests.exceptions.RequestException as e:
461
+ # logging.error(f"获取或处理图片时发生网络错误: {str(e)}")
462
+ # raise
463
+ # except Exception as e:
464
+ # logging.error(f"图像识别过程中发生错误: {str(e)}")
465
+ # raise
466
+
467
+ # else:
468
+ # # 处理文本消息
469
+ # try:
470
+ # logging.info("开始处理文本消息")
471
+ # response = client.chat.completions.create(
472
+ # model="o3-mini",
473
+ # messages=messages,
474
+ # tools=TOOLS,
475
+ # tool_choice="auto",
476
+ # timeout=60
477
+ # )
478
+
479
+ # # 检查是否需要生成图片
480
+ # if response.choices[0].message.tool_calls:
481
+ # tool_call = response.choices[0].message.tool_calls[0]
482
+ # if tool_call.function.name == "generate_image":
483
+ # try:
484
+ # logging.info("检测到图片生成请求")
485
+ # args = json.loads(tool_call.function.arguments)
486
+
487
+ # # 调用图片生成服务
488
+ # image_generation_response = requests.post(
489
+ # IMAGE_MODEL_URL,
490
+ # headers={
491
+ # 'Content-Type': 'application/json',
492
+ # 'Authorization': f'Bearer {IMAGE_MODEL_KEY}'
493
+ # },
494
+ # json={
495
+ # "model": "grok-latest-image",
496
+ # "messages": [{
497
+ # "role": "user",
498
+ # "content": args['prompt']
499
+ # }]
500
+ # },
501
+ # timeout=60
502
+ # )
503
+ # image_generation_response.raise_for_status()
504
+ # generation_result = image_generation_response.json()
505
+
506
+ # if 'choices' not in generation_result or not generation_result['choices']:
507
+ # raise Exception("图片生成服务未返回有效结果")
508
+
509
+ # # 从markdown格式中提取URL
510
+ # markdown_content = generation_result['choices'][0]['message']['content']
511
+ # image_url_match = re.search(r'\!\[image\]\((.*?)\)', markdown_content)
512
+ # if not image_url_match:
513
+ # raise Exception("无法从响应中提取图片URL")
514
+
515
+ # image_url = image_url_match.group(1)
516
+
517
+ # # 下载生成的图片
518
+ # img_response = requests.get(image_url, timeout=30)
519
+ # img_response.raise_for_status()
520
+
521
+ # # 上传图片到微信服务器
522
+ # media_id = upload_image_to_wechat(img_response.content)
523
+
524
+ # return {
525
+ # "type": "image",
526
+ # "media_id": media_id
527
+ # }
528
+
529
+ # except requests.exceptions.RequestException as e:
530
+ # logging.error(f"图片生成过程中发生网络错误: {str(e)}")
531
+ # raise
532
+ # except Exception as e:
533
+ # logging.error(f"图片生成过程中发生错误: {str(e)}")
534
+ # raise
535
+
536
+ # # 返回文本响应
537
+ # return {
538
+ # "type": "text",
539
+ # "content": response.choices[0].message.content
540
+ # }
541
+
542
+ # except requests.exceptions.RequestException as e:
543
+ # logging.error(f"处理文本消息时发生网络错误: {str(e)}")
544
+ # raise
545
+ # except Exception as e:
546
+ # logging.error(f"处理文本消息时发生错误: {str(e)}")
547
+ # raise
548
+
549
+ # except Exception as e:
550
+ # logging.error(f"API调用错误: {str(e)}")
551
+ # raise
552
+ async def process_long_running_task(messages, message_type='text', image_data=None):
553
  try:
554
+ logging.info(f"Starting AI service call, message type: {message_type}")
555
 
556
  if message_type == 'image':
557
  try:
558
+ # Get image content
559
  image_content = get_image_content(image_data['media_id'])
560
+ # Convert image content to base64
561
  image_base64 = base64.b64encode(image_content).decode('utf-8')
562
 
563
+ # Construct image recognition request
564
  image_messages = [
565
  {
566
  "role": "user",
 
576
  }
577
  ]
578
 
579
+ logging.info("Starting image recognition model")
580
  image_response = client.chat.completions.create(
581
  model="gpt-4o-mini",
582
  messages=image_messages,
583
  max_tokens=300,
584
  timeout=60
585
  )
586
+ logging.info("Image recognition completed")
587
 
 
588
  if not image_response.choices:
589
+ raise Exception("Image recognition service did not return valid results")
590
 
591
  return {
592
  "type": "text",
 
594
  }
595
 
596
  except requests.exceptions.RequestException as e:
597
+ logging.error(f"Network error during image processing: {str(e)}")
598
  raise
599
  except Exception as e:
600
+ logging.error(f"Error during image recognition: {str(e)}")
601
  raise
602
 
603
  else:
604
+ # Process text messages
605
  try:
606
+ logging.info("Processing text message")
607
  response = client.chat.completions.create(
608
  model="o3-mini",
609
  messages=messages,
 
612
  timeout=60
613
  )
614
 
615
+ # Check if image generation is needed
616
  if response.choices[0].message.tool_calls:
617
  tool_call = response.choices[0].message.tool_calls[0]
618
  if tool_call.function.name == "generate_image":
619
  try:
620
+ logging.info("Image generation request detected")
621
  args = json.loads(tool_call.function.arguments)
622
 
623
+ # Call image generation service
624
  image_generation_response = requests.post(
625
  IMAGE_MODEL_URL,
626
  headers={
 
628
  'Authorization': f'Bearer {IMAGE_MODEL_KEY}'
629
  },
630
  json={
631
+ "model": "grok-3-imageGen",
632
  "messages": [{
633
  "role": "user",
634
  "content": args['prompt']
 
640
  generation_result = image_generation_response.json()
641
 
642
  if 'choices' not in generation_result or not generation_result['choices']:
643
+ raise Exception("Image generation service did not return valid results")
644
+
645
+ # Extract base64 data from markdown response
646
  markdown_content = generation_result['choices'][0]['message']['content']
647
+ base64_match = re.search(r'data:image\/[^;]+;base64,([^)]+)', markdown_content)
648
+
649
+ if not base64_match:
650
+ raise Exception("Could not extract base64 image data from response")
 
651
 
652
+ # Decode base64 data
653
+ image_data = base64.b64decode(base64_match.group(1))
 
654
 
655
+ # Upload to WeChat server
656
+ media_id = upload_image_to_wechat(image_data)
657
 
658
  return {
659
  "type": "image",
 
661
  }
662
 
663
  except requests.exceptions.RequestException as e:
664
+ logging.error(f"Network error during image generation: {str(e)}")
665
  raise
666
  except Exception as e:
667
+ logging.error(f"Error during image generation: {str(e)}")
668
  raise
669
 
670
+ # Return text response
671
  return {
672
  "type": "text",
673
  "content": response.choices[0].message.content
674
  }
675
 
676
  except requests.exceptions.RequestException as e:
677
+ logging.error(f"Network error while processing text message: {str(e)}")
678
  raise
679
  except Exception as e:
680
+ logging.error(f"Error while processing text message: {str(e)}")
681
  raise
682
+
683
  except Exception as e:
684
+ logging.error(f"API call error: {str(e)}")
685
  raise
686
 
 
687
  def handle_async_task(session, task_id, messages=None, message_type='text', message_data=None):
688
  """
689
  处理异步任务,支持文本对话和图片识别