Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -632,47 +632,118 @@ def handsome_chat_completions():
|
|
632 |
def generate():
|
633 |
first_chunk_time = None
|
634 |
full_response_content = ""
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
chunk_json = json.loads(chunk.decode("utf-8"))
|
641 |
-
if "images" in chunk_json and chunk_json["images"]:
|
642 |
-
image_url = chunk_json["images"][0].get("url", "")
|
643 |
-
if image_url:
|
644 |
-
full_response_content += json.dumps({"url":image_url})
|
645 |
-
yield f"data: {json.dumps({'url': image_url})}\n\n".encode('utf-8')
|
646 |
-
else:
|
647 |
-
full_response_content += json.dumps({"content":"Failed to generate image"})
|
648 |
-
yield f"data: {json.dumps({'content': 'Failed to generate image'})}\n\n".encode('utf-8')
|
649 |
-
else:
|
650 |
-
full_response_content += json.dumps({"content":"Failed to generate image"})
|
651 |
-
yield f"data: {json.dumps({'content': 'Failed to generate image'})}\n\n".encode('utf-8')
|
652 |
-
except json.JSONDecodeError:
|
653 |
-
logging.error(f"Failed to decode chunk JSON: {chunk.decode('utf-8')}")
|
654 |
-
full_response_content += json.dumps({"content":"Failed to generate image"})
|
655 |
-
yield f"data: {json.dumps({'content': 'Failed to generate image'})}\n\n".encode('utf-8')
|
656 |
-
|
657 |
-
end_time = time.time()
|
658 |
-
first_token_time = (
|
659 |
-
first_chunk_time - start_time
|
660 |
-
if first_chunk_time else 0
|
661 |
-
)
|
662 |
-
total_time = end_time - start_time
|
663 |
-
|
664 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
665 |
logging.info(
|
666 |
f"使用的key: {api_key}, "
|
667 |
-
f"首字用时: {first_token_time:.4f}秒, "
|
668 |
-
f"总共用时: {total_time:.4f}秒, "
|
669 |
f"使用的模型: {model_name}"
|
670 |
)
|
671 |
-
|
672 |
-
with data_lock:
|
673 |
-
request_timestamps.append(time.time())
|
674 |
-
token_counts.append(0) # Image generation doesn't use tokens
|
675 |
-
|
676 |
yield "data: [DONE]\n\n".encode('utf-8')
|
677 |
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
678 |
else:
|
@@ -944,7 +1015,7 @@ def handsome_chat_completions():
|
|
944 |
except requests.exceptions.RequestException as e:
|
945 |
logging.error(f"请求转发异常: {e}")
|
946 |
return jsonify({"error": str(e)}), 500
|
947 |
-
|
948 |
@app.route('/handsome/v1/models', methods=['GET'])
|
949 |
def list_models():
|
950 |
if not check_authorization(request):
|
|
|
632 |
def generate():
|
633 |
first_chunk_time = None
|
634 |
full_response_content = ""
|
635 |
+
try:
|
636 |
+
response.raise_for_status()
|
637 |
+
end_time = time.time()
|
638 |
+
response_json = response.json()
|
639 |
+
total_time = end_time - start_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
640 |
|
641 |
+
images = response_json.get("images", [])
|
642 |
+
|
643 |
+
# Extract the first URL if available
|
644 |
+
image_url = ""
|
645 |
+
if images and isinstance(images[0], dict) and "url" in images[0]:
|
646 |
+
image_url = images[0]["url"]
|
647 |
+
logging.info(f"Extracted image URL: {image_url}")
|
648 |
+
elif images and isinstance(images[0], str):
|
649 |
+
image_url = images[0]
|
650 |
+
logging.info(f"Extracted image URL: {image_url}")
|
651 |
+
|
652 |
+
if image_url:
|
653 |
+
chunk_data = {
|
654 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
655 |
+
"object": "chat.completion.chunk",
|
656 |
+
"created": int(time.time()),
|
657 |
+
"model": model_name,
|
658 |
+
"choices": [
|
659 |
+
{
|
660 |
+
"index": 0,
|
661 |
+
"delta": {
|
662 |
+
"role": "assistant",
|
663 |
+
"content": image_url
|
664 |
+
},
|
665 |
+
"finish_reason": None
|
666 |
+
}
|
667 |
+
]
|
668 |
+
}
|
669 |
+
yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
|
670 |
+
full_response_content = image_url
|
671 |
+
else:
|
672 |
+
chunk_data = {
|
673 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
674 |
+
"object": "chat.completion.chunk",
|
675 |
+
"created": int(time.time()),
|
676 |
+
"model": model_name,
|
677 |
+
"choices": [
|
678 |
+
{
|
679 |
+
"index": 0,
|
680 |
+
"delta": {
|
681 |
+
"role": "assistant",
|
682 |
+
"content": "Failed to generate image"
|
683 |
+
},
|
684 |
+
"finish_reason": None
|
685 |
+
}
|
686 |
+
]
|
687 |
+
}
|
688 |
+
yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
|
689 |
+
full_response_content = "Failed to generate image"
|
690 |
+
|
691 |
+
end_chunk_data = {
|
692 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
693 |
+
"object": "chat.completion.chunk",
|
694 |
+
"created": int(time.time()),
|
695 |
+
"model": model_name,
|
696 |
+
"choices": [
|
697 |
+
{
|
698 |
+
"index": 0,
|
699 |
+
"delta": {},
|
700 |
+
"finish_reason": "stop"
|
701 |
+
}
|
702 |
+
]
|
703 |
+
}
|
704 |
+
yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
|
705 |
+
|
706 |
+
with data_lock:
|
707 |
+
request_timestamps.append(time.time())
|
708 |
+
token_counts.append(0) # Image generation doesn't use tokens
|
709 |
+
except requests.exceptions.RequestException as e:
|
710 |
+
logging.error(f"请求转发异常: {e}")
|
711 |
+
error_chunk_data = {
|
712 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
713 |
+
"object": "chat.completion.chunk",
|
714 |
+
"created": int(time.time()),
|
715 |
+
"model": model_name,
|
716 |
+
"choices": [
|
717 |
+
{
|
718 |
+
"index": 0,
|
719 |
+
"delta": {
|
720 |
+
"role": "assistant",
|
721 |
+
"content": f"Error: {str(e)}"
|
722 |
+
},
|
723 |
+
"finish_reason": None
|
724 |
+
}
|
725 |
+
]
|
726 |
+
}
|
727 |
+
yield f"data: {json.dumps(error_chunk_data)}\n\n".encode('utf-8')
|
728 |
+
end_chunk_data = {
|
729 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
730 |
+
"object": "chat.completion.chunk",
|
731 |
+
"created": int(time.time()),
|
732 |
+
"model": model_name,
|
733 |
+
"choices": [
|
734 |
+
{
|
735 |
+
"index": 0,
|
736 |
+
"delta": {},
|
737 |
+
"finish_reason": "stop"
|
738 |
+
}
|
739 |
+
]
|
740 |
+
}
|
741 |
+
yield f"data: {json.dumps(end_chunk_data)}\n\n".encode('utf-8')
|
742 |
+
|
743 |
logging.info(
|
744 |
f"使用的key: {api_key}, "
|
|
|
|
|
745 |
f"使用的模型: {model_name}"
|
746 |
)
|
|
|
|
|
|
|
|
|
|
|
747 |
yield "data: [DONE]\n\n".encode('utf-8')
|
748 |
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
749 |
else:
|
|
|
1015 |
except requests.exceptions.RequestException as e:
|
1016 |
logging.error(f"请求转发异常: {e}")
|
1017 |
return jsonify({"error": str(e)}), 500
|
1018 |
+
|
1019 |
@app.route('/handsome/v1/models', methods=['GET'])
|
1020 |
def list_models():
|
1021 |
if not check_authorization(request):
|