Update app.py
Browse files
app.py
CHANGED
@@ -585,6 +585,8 @@ def handsome_chat_completions():
|
|
585 |
}
|
586 |
|
587 |
if model_name in image_models:
|
|
|
|
|
588 |
siliconflow_data = {
|
589 |
"model": model_name,
|
590 |
"prompt": data.get("messages", [{}])[0].get("content") if isinstance(data.get("messages"), list) else "",
|
@@ -675,6 +677,7 @@ def handsome_chat_completions():
|
|
675 |
|
676 |
for chunk in image_response.iter_content(chunk_size=1024):
|
677 |
if chunk:
|
|
|
678 |
chunk_data = {
|
679 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
680 |
"object": "chat.completion.chunk",
|
@@ -685,7 +688,7 @@ def handsome_chat_completions():
|
|
685 |
"index": 0,
|
686 |
"delta": {
|
687 |
"role": "assistant",
|
688 |
-
"content":
|
689 |
},
|
690 |
"finish_reason": None
|
691 |
}
|
@@ -723,7 +726,7 @@ def handsome_chat_completions():
|
|
723 |
|
724 |
with data_lock:
|
725 |
request_timestamps.append(time.time())
|
726 |
-
token_counts.append(0)
|
727 |
|
728 |
else:
|
729 |
chunk_data = {
|
@@ -803,6 +806,7 @@ def handsome_chat_completions():
|
|
803 |
try:
|
804 |
images = response_json.get("images", [])
|
805 |
|
|
|
806 |
image_url = ""
|
807 |
if images and isinstance(images[0], dict) and "url" in images[0]:
|
808 |
image_url = images[0]["url"]
|
@@ -859,7 +863,7 @@ def handsome_chat_completions():
|
|
859 |
|
860 |
with data_lock:
|
861 |
request_timestamps.append(time.time())
|
862 |
-
token_counts.append(0)
|
863 |
|
864 |
return jsonify(response_data)
|
865 |
except requests.exceptions.RequestException as e:
|
|
|
585 |
}
|
586 |
|
587 |
if model_name in image_models:
|
588 |
+
# Handle image generation
|
589 |
+
# Map OpenAI-style parameters to SiliconFlow's parameters
|
590 |
siliconflow_data = {
|
591 |
"model": model_name,
|
592 |
"prompt": data.get("messages", [{}])[0].get("content") if isinstance(data.get("messages"), list) else "",
|
|
|
677 |
|
678 |
for chunk in image_response.iter_content(chunk_size=1024):
|
679 |
if chunk:
|
680 |
+
base64_chunk = base64.b64encode(chunk).decode('utf-8')
|
681 |
chunk_data = {
|
682 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
683 |
"object": "chat.completion.chunk",
|
|
|
688 |
"index": 0,
|
689 |
"delta": {
|
690 |
"role": "assistant",
|
691 |
+
"content": base64_chunk
|
692 |
},
|
693 |
"finish_reason": None
|
694 |
}
|
|
|
726 |
|
727 |
with data_lock:
|
728 |
request_timestamps.append(time.time())
|
729 |
+
token_counts.append(0) # Image generation doesn't use tokens
|
730 |
|
731 |
else:
|
732 |
chunk_data = {
|
|
|
806 |
try:
|
807 |
images = response_json.get("images", [])
|
808 |
|
809 |
+
# Extract the first URL if available
|
810 |
image_url = ""
|
811 |
if images and isinstance(images[0], dict) and "url" in images[0]:
|
812 |
image_url = images[0]["url"]
|
|
|
863 |
|
864 |
with data_lock:
|
865 |
request_timestamps.append(time.time())
|
866 |
+
token_counts.append(0) # Image generation doesn't use tokens
|
867 |
|
868 |
return jsonify(response_data)
|
869 |
except requests.exceptions.RequestException as e:
|