Update app.py
Browse files
app.py
CHANGED
@@ -15,8 +15,8 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
|
15 |
from flask import Flask, request, jsonify, Response, stream_with_context
|
16 |
from werkzeug.middleware.proxy_fix import ProxyFix
|
17 |
|
18 |
-
os.environ['TZ'] = 'Asia/Shanghai'
|
19 |
-
time.tzset()
|
20 |
|
21 |
logging.basicConfig(level=logging.INFO,
|
22 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -123,7 +123,6 @@ def create_base64_markdown_image(image_url):
|
|
123 |
base64_encoded = base64.b64encode(image_data).decode('utf-8')
|
124 |
mime_type = response.headers.get('Content-Type', 'image/png')
|
125 |
markdown_image_link = f"![](data:{mime_type};base64,{base64_encoded})"
|
126 |
-
# print(markdown_image_link)
|
127 |
logging.info(f"Created base64 markdown image link.")
|
128 |
return markdown_image_link
|
129 |
except requests.exceptions.RequestException as e:
|
@@ -956,23 +955,26 @@ def handsome_chat_completions():
|
|
956 |
|
957 |
markdown_image_link = create_base64_markdown_image(image_url)
|
958 |
if image_url:
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
"
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
|
|
|
|
|
|
|
976 |
else:
|
977 |
chunk_data = {
|
978 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
|
15 |
from flask import Flask, request, jsonify, Response, stream_with_context
|
16 |
from werkzeug.middleware.proxy_fix import ProxyFix
|
17 |
|
18 |
+
# os.environ['TZ'] = 'Asia/Shanghai'
|
19 |
+
# time.tzset()
|
20 |
|
21 |
logging.basicConfig(level=logging.INFO,
|
22 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
123 |
base64_encoded = base64.b64encode(image_data).decode('utf-8')
|
124 |
mime_type = response.headers.get('Content-Type', 'image/png')
|
125 |
markdown_image_link = f"![](data:{mime_type};base64,{base64_encoded})"
|
|
|
126 |
logging.info(f"Created base64 markdown image link.")
|
127 |
return markdown_image_link
|
128 |
except requests.exceptions.RequestException as e:
|
|
|
955 |
|
956 |
markdown_image_link = create_base64_markdown_image(image_url)
|
957 |
if image_url:
|
958 |
+
chunk_size = 2048
|
959 |
+
for i in range(0, len(markdown_image_link), chunk_size):
|
960 |
+
chunk = markdown_image_link[i:i + chunk_size]
|
961 |
+
chunk_data = {
|
962 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
963 |
+
"object": "chat.completion.chunk",
|
964 |
+
"created": int(time.time()),
|
965 |
+
"model": model_name,
|
966 |
+
"choices": [
|
967 |
+
{
|
968 |
+
"index": 0,
|
969 |
+
"delta": {
|
970 |
+
"role": "assistant",
|
971 |
+
"content": chunk
|
972 |
+
},
|
973 |
+
"finish_reason": None
|
974 |
+
}
|
975 |
+
]
|
976 |
+
}
|
977 |
+
yield f"data: {json.dumps(chunk_data)}\n\n".encode('utf-8')
|
978 |
else:
|
979 |
chunk_data = {
|
980 |
"id": f"chatcmpl-{uuid.uuid4()}",
|