yangtb24 commited on
Commit
ae4ac7f
·
verified ·
1 Parent(s): 1d34c84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -24
app.py CHANGED
@@ -634,25 +634,16 @@ def handsome_chat_completions():
634
 
635
  try:
636
  images = response_json.get("images", [])
637
- openai_images = []
638
- for item in images:
639
- if isinstance(item, dict) and "url" in item:
640
- image_url = item["url"]
641
- logging.info(f"Extracted image URL: {image_url}") # Log the URL
642
- try:
643
- image_data = requests.get(image_url, stream=True).raw
644
- image = Image.open(image_data)
645
- buffered = io.BytesIO()
646
- image.save(buffered, format="PNG")
647
- img_str = base64.b64encode(buffered.getvalue()).decode()
648
- openai_images.append({"b64_json": img_str})
649
- except Exception as e:
650
- logging.error(f"图片转base64失败: {e}")
651
- openai_images.append({"url": image_url})
652
- else:
653
- logging.error(f"无效的图片数据: {item}")
654
- openai_images.append({"url": item})
655
-
656
  # Construct the expected JSON output - Mimicking OpenAI
657
  response_data = {
658
  "id": f"chatcmpl-{uuid.uuid4()}",
@@ -664,8 +655,7 @@ def handsome_chat_completions():
664
  "index": 0,
665
  "message": {
666
  "role": "assistant",
667
- "content": "Generated Image:", # Changed content here
668
- "images": openai_images
669
  },
670
  "finish_reason": "stop",
671
  }
@@ -687,8 +677,7 @@ def handsome_chat_completions():
687
  "index": 0,
688
  "message": {
689
  "role": "assistant",
690
- "content": "Error Generating Image:", # Changed content here
691
- "images": []
692
  },
693
  "finish_reason": "stop",
694
  }
@@ -710,7 +699,7 @@ def handsome_chat_completions():
710
  logging.error(f"请求转发异常: {e}")
711
  return jsonify({"error": str(e)}), 500
712
  else:
713
- # Existing text-based model handling logic
714
  try:
715
  start_time = time.time()
716
  response = requests.post(
 
634
 
635
  try:
636
  images = response_json.get("images", [])
637
+
638
+ # Extract the first URL if available
639
+ image_url = ""
640
+ if images and isinstance(images[0], dict) and "url" in images[0]:
641
+ image_url = images[0]["url"]
642
+ logging.info(f"Extracted image URL: {image_url}")
643
+ elif images and isinstance(images[0], str):
644
+ image_url = images[0]
645
+ logging.info(f"Extracted image URL: {image_url}")
646
+
 
 
 
 
 
 
 
 
 
647
  # Construct the expected JSON output - Mimicking OpenAI
648
  response_data = {
649
  "id": f"chatcmpl-{uuid.uuid4()}",
 
655
  "index": 0,
656
  "message": {
657
  "role": "assistant",
658
+ "content": image_url if image_url else "Failed to generate image", # Directly return the URL in content
 
659
  },
660
  "finish_reason": "stop",
661
  }
 
677
  "index": 0,
678
  "message": {
679
  "role": "assistant",
680
+ "content": "Failed to process image data",
 
681
  },
682
  "finish_reason": "stop",
683
  }
 
699
  logging.error(f"请求转发异常: {e}")
700
  return jsonify({"error": str(e)}), 500
701
  else:
702
+ # Existing text-based model handling logic
703
  try:
704
  start_time = time.time()
705
  response = requests.post(