Update app.py
Browse files
app.py
CHANGED
|
@@ -634,25 +634,16 @@ def handsome_chat_completions():
|
|
| 634 |
|
| 635 |
try:
|
| 636 |
images = response_json.get("images", [])
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 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": "
|
| 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": "
|
| 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 |
-
|
| 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(
|