mistpe commited on
Commit
04e80e0
·
verified ·
1 Parent(s): 1f90f30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -158
app.py CHANGED
@@ -403,165 +403,27 @@ 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",
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-2-imageGen",
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
  # def process_long_running_task(messages, message_type='text', image_data=None):
553
  # """
554
  # 处理长时间运行的任务,支持文本对话和图片识别
 
 
 
 
 
 
555
  # """
556
  # try:
557
  # logging.info(f"开始调用AI服务,消息类型: {message_type}")
558
 
559
  # if message_type == 'image':
560
- # # 图片识别逻辑保持不变
561
  # try:
 
562
  # image_content = get_image_content(image_data['media_id'])
 
563
  # image_base64 = base64.b64encode(image_content).decode('utf-8')
564
 
 
565
  # image_messages = [
566
  # {
567
  # "role": "user",
@@ -586,6 +448,7 @@ def process_long_running_task(messages, message_type='text', image_data=None):
586
  # )
587
  # logging.info("图像识别完成")
588
 
 
589
  # if not image_response.choices:
590
  # raise Exception("图像识别服务未返回有效结果")
591
 
@@ -594,6 +457,9 @@ def process_long_running_task(messages, message_type='text', image_data=None):
594
  # "content": image_response.choices[0].message.content
595
  # }
596
 
 
 
 
597
  # except Exception as e:
598
  # logging.error(f"图像识别过程中发生错误: {str(e)}")
599
  # raise
@@ -618,29 +484,35 @@ def process_long_running_task(messages, message_type='text', image_data=None):
618
  # logging.info("检测到图片生成请求")
619
  # args = json.loads(tool_call.function.arguments)
620
 
621
- # # 使用新的DALL-E 3 API进行图片生成
622
  # image_generation_response = requests.post(
623
- # "https://api1.oaipro.com/v1/images/generations",
624
  # headers={
625
  # 'Content-Type': 'application/json',
626
- # 'Authorization': f'Bearer {API_KEY}'
627
  # },
628
  # json={
629
- # "model": "dall-e-3",
630
- # "prompt": args['prompt'],
631
- # "n": 1,
632
- # "size": "1024x1024"
 
633
  # },
634
  # timeout=60
635
  # )
636
  # image_generation_response.raise_for_status()
637
  # generation_result = image_generation_response.json()
638
 
639
- # if 'data' not in generation_result or not generation_result['data']:
640
  # raise Exception("图片生成服务未返回有效结果")
641
-
642
- # # 获取生成的图片URL
643
- # image_url = generation_result['data'][0]['url']
 
 
 
 
 
644
 
645
  # # 下载生成的图片
646
  # img_response = requests.get(image_url, timeout=30)
@@ -677,6 +549,134 @@ def process_long_running_task(messages, message_type='text', image_data=None):
677
  # except Exception as e:
678
  # logging.error(f"API调用错误: {str(e)}")
679
  # raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
680
 
681
  def handle_async_task(session, task_id, messages=None, message_type='text', message_data=None):
682
  """
 
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",
 
448
  # )
449
  # logging.info("图像识别完成")
450
 
451
+ # # 检查响应是否成功
452
  # if not image_response.choices:
453
  # raise Exception("图像识别服务未返回有效结果")
454
 
 
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
 
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-2-imageGen",
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)
 
549
  # except Exception as e:
550
  # logging.error(f"API调用错误: {str(e)}")
551
  # raise
552
+ def process_long_running_task(messages, message_type='text', image_data=None):
553
+ """
554
+ 处理长时间运行的任务,支持文本对话和图片识别
555
+ """
556
+ try:
557
+ logging.info(f"开始调用AI服务,消息类型: {message_type}")
558
+
559
+ if message_type == 'image':
560
+ # 图片识别逻辑保持不变
561
+ try:
562
+ image_content = get_image_content(image_data['media_id'])
563
+ image_base64 = base64.b64encode(image_content).decode('utf-8')
564
+
565
+ image_messages = [
566
+ {
567
+ "role": "user",
568
+ "content": [
569
+ {"type": "text", "text": "请详细描述这张图片中的内容,包括主要对象、场景、活动等关键信息"},
570
+ {
571
+ "type": "image_url",
572
+ "image_url": {
573
+ "url": f"data:image/jpeg;base64,{image_base64}"
574
+ }
575
+ }
576
+ ]
577
+ }
578
+ ]
579
+
580
+ logging.info("开始调用图像识别模型")
581
+ image_response = client.chat.completions.create(
582
+ model="gpt-4o-mini",
583
+ messages=image_messages,
584
+ max_tokens=300,
585
+ timeout=60
586
+ )
587
+ logging.info("图像识别完成")
588
+
589
+ if not image_response.choices:
590
+ raise Exception("图像识别服务未返回有效结果")
591
+
592
+ return {
593
+ "type": "text",
594
+ "content": image_response.choices[0].message.content
595
+ }
596
+
597
+ except Exception as e:
598
+ logging.error(f"图像识别过程中发生错误: {str(e)}")
599
+ raise
600
+
601
+ else:
602
+ # 处理文本消息
603
+ try:
604
+ logging.info("开始处理文本消息")
605
+ response = client.chat.completions.create(
606
+ model="o3-mini",
607
+ messages=messages,
608
+ tools=TOOLS,
609
+ tool_choice="auto",
610
+ timeout=60
611
+ )
612
+
613
+ # 检查是否需要生成图片
614
+ if response.choices[0].message.tool_calls:
615
+ tool_call = response.choices[0].message.tool_calls[0]
616
+ if tool_call.function.name == "generate_image":
617
+ try:
618
+ logging.info("检测到图片生成请求")
619
+ args = json.loads(tool_call.function.arguments)
620
+
621
+ # 使用新的DALL-E 3 API进行图片生成
622
+ image_generation_response = requests.post(
623
+ "https://api1.oaipro.com/v1/images/generations",
624
+ headers={
625
+ 'Content-Type': 'application/json',
626
+ 'Authorization': f'Bearer {API_KEY}'
627
+ },
628
+ json={
629
+ "model": "dall-e-3",
630
+ "prompt": args['prompt'],
631
+ "n": 1,
632
+ "size": "1024x1024"
633
+ },
634
+ timeout=60
635
+ )
636
+ image_generation_response.raise_for_status()
637
+ generation_result = image_generation_response.json()
638
+
639
+ if 'data' not in generation_result or not generation_result['data']:
640
+ raise Exception("图片生成服务未返回有效结果")
641
+
642
+ # 获取生成的图片URL
643
+ image_url = generation_result['data'][0]['url']
644
+
645
+ # 下载生成的图片
646
+ img_response = requests.get(image_url, timeout=30)
647
+ img_response.raise_for_status()
648
+
649
+ # 上传图片到微信服务器
650
+ media_id = upload_image_to_wechat(img_response.content)
651
+
652
+ return {
653
+ "type": "image",
654
+ "media_id": media_id
655
+ }
656
+
657
+ except requests.exceptions.RequestException as e:
658
+ logging.error(f"图片生成过程中发生网络错误: {str(e)}")
659
+ raise
660
+ except Exception as e:
661
+ logging.error(f"图片生成过程中发生错误: {str(e)}")
662
+ raise
663
+
664
+ # 返回文本响应
665
+ return {
666
+ "type": "text",
667
+ "content": response.choices[0].message.content
668
+ }
669
+
670
+ except requests.exceptions.RequestException as e:
671
+ logging.error(f"处理文本消息时发生网络错误: {str(e)}")
672
+ raise
673
+ except Exception as e:
674
+ logging.error(f"处理文本消息时发生错误: {str(e)}")
675
+ raise
676
+
677
+ except Exception as e:
678
+ logging.error(f"API调用错误: {str(e)}")
679
+ raise
680
 
681
  def handle_async_task(session, task_id, messages=None, message_type='text', message_data=None):
682
  """