-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserverless_api_service.py
More file actions
441 lines (365 loc) · 15.2 KB
/
Copy pathserverless_api_service.py
File metadata and controls
441 lines (365 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import re
import os
import json
import time
import base64
import random
import threading
from traceback import print_exception
import requests
import websocket
from typing import Any
import constants
from store import Store, FileSystem, OSS
from uuid import uuid4
from flask import request
class ComfyUIException(Exception):
def __init__(self, message: str, code: str, raw: str):
super().__init__(message)
self.raw = raw
self.code = code
def response(self):
raw = self.raw
try:
raw = json.loads(raw)
except:
pass
res = {
"type": "error",
"error_code": self.code or constants.ERROR_CODE.UNCLASSIFY.value,
"error_message": str(self),
}
if raw:
res["raw"] = raw
return res
class ServerlessApiService:
def __init__(self):
self.endpoint = f"http://{constants.APP_HOST}"
# 状态持久化
# 在异步调用 Serverless API 时,可以通过将状态写至持久化存储来确保在多个实例同时出图时仍然可以正确获取状态
#
# 默认实现了基于共享存储的方式实现的状态持久化(需要正确挂载 NAS)
# 也可以考虑复用上面的 oss_store,将图片和状态均存储至 OSS 中
# 如 `self.store: Store = self.oss_store`
#
# 必要时,也可以参考对应代码实现基于 Redis、TableStore、MySQL 等方式的状态持久化
self.store: Store = FileSystem(f"{constants.MNT_DIR}/output/serverless_api")
def get_credentials(self):
ak = ""
sk = ""
sts = ""
# 优先尝试从 header 获取
try:
ak = request.headers.get(constants.HEADER_KEY_ACCESS_KEY_ID, "")
sk = request.headers.get(constants.HEADER_KEY_ACCESS_KEY_SECRET, "")
sts = request.headers.get(constants.HEADER_KEY_SECURITY_TOKEN, "")
except Exception as e:
print_exception(e)
print(f"get credentials from header failed, reason: {e}")
# 如果 header 没有,尝试从 env 获取
if ak == "" or sk == "":
ak = constants.ALIBABA_CLOUD_ACCESS_KEY_ID
sk = constants.ALIBABA_CLOUD_ACCESS_KEY_SECRET
sts = constants.ALIBABA_CLOUD_SECURITY_TOKEN
return ak, sk, sts
def get_oss_store(self):
ak, sk, sts = self.get_credentials()
# OSS 存储,需要时,可以将生成的图片同步至 OSS 中
return OSS(
constants.OSS_BUCKET_DOMAIN,
ak,
sk,
sts,
constants.OSS_KEY_PREFIX,
constants.OSS_EXPIRES_IN_SECOND,
)
def api_prompt(self, client_id: str, payload: Any):
"""
出图
"""
req = {"client_id": client_id, **payload}
res = requests.post(
os.path.join(self.endpoint, "prompt"),
json=req,
)
if res.status_code != 200:
print({"prompt request": req})
data = {}
try:
data = res.json()
except:
pass
raise ComfyUIException(
f"ComfyUI prompt api failed with {res.status_code}: {data.get('error', {}).get('message', res.text)}",
constants.ERROR_CODE.PROMPT_ERROR.value,
res.text,
)
return res.json()
def api_websocket(self, client_id: str, on_message):
endpoint = re.subn(r"^http", "ws", self.endpoint, count=1)[0]
ws = websocket.WebSocketApp(
f'{os.path.join(endpoint, "ws")}?clientId={client_id}',
on_message=on_message,
keep_running=True,
)
return ws
def api_upload_image(self, content: bytes, overwrite: bool):
uuid = str(uuid4())
files = {
"image": (uuid, content),
}
if overwrite:
files["overwrite"] = bytes("1")
res = requests.post(
os.path.join(self.endpoint, "upload/image"),
files=files,
)
return res.json()
def api_get_history(self, prompt_id: str):
return requests.get(os.path.join(self.endpoint, "history", prompt_id)).json()
def api_view_image(self, filename: str, img_type: str, sub_folder: str):
return requests.get(
os.path.join(self.endpoint, "view"),
params={
"filename": filename,
"type": img_type,
"subfolder": sub_folder,
"rand": random.random(),
},
).content
def api_clear_history(self):
requests.post(os.path.join(self.endpoint, "history"), json={"clear": True})
def parse_prompt(self, prompt: map):
"""
预处理 prompt 的内容
- 如果以 base64、url 形式传输的图片,自动完成上传行为
"""
ak, sk, sts = self.get_credentials()
for key, value in prompt.items():
if type(value) == dict and (value.get("class_type") == "LoadImage" or value.get("class_type") == "LoadImageMask"):
try:
image = value.get("inputs", {}).get("image", "")
content = ""
if image.startswith("http://") or image.startswith("https://"):
# 图片来源于 url
response = requests.get(image)
if response.status_code >= 400:
raise Exception(
f"can not get image {image} from http url, got status code {response.status_code}"
)
content = response.content
if content == "":
raise Exception(f"can not get image {image} from http url")
elif image.startswith("oss://"):
# 图片来源于 oss
arr = image.split("/")
host = arr[2]
path = "/".join(arr[3:])
oss = OSS(host, ak, sk, sts, "", 0)
content = oss.get(path)
if content == "":
raise Exception(f"can not get image {image} from oss")
elif len(image) > 64:
# 图像可能是 base64,尝试使用 base64 解析
try:
content = base64.b64decode(image.strip())
except:
pass
if content:
res = self.api_upload_image(content, False)
prompt[key]["inputs"]["image"] = res["name"]
except Exception as e:
raise Exception(f"LoadImage failed: {e}")
if type(value) == dict and value.get("class_type") == "KSampler":
if value.get("inputs", {}).get("seed") == -1:
prompt[key]["inputs"]["seed"] = random.randint(0, 4294967296)
if type(value) == dict and value.get("class_type") == "SaveImage":
try:
value["inputs"]["filename_prefix"] = (
value.get("inputs", {}).get("filename_prefix", "ComfyUI")
+ "_"
+ constants.INSTANCE_ID
)
except:
pass
return prompt
def get_history_result(self, prompt_id: str, output_base64=False, output_oss=False):
# 出图结果数组
results = []
history = self.api_get_history(prompt_id)
oss_store = self.get_oss_store()
for node_id, output in history.get(prompt_id, {}).get("outputs", {}).items():
for output_type, imgs in output.items():
for index, img in enumerate(imgs):
if type(img) != dict or not img.get("filename"):
continue
filename = img.get("filename", "")
img_type = img.get("type", "")
sub_folder = img.get("subfolder", "")
img_output = None
oss_object_key = None
oss_url = None
if output_base64 or output_oss:
img_bytes = self.api_view_image(filename, img_type, sub_folder)
if output_base64:
img_output = base64.b64encode(img_bytes).decode("ascii")
if output_oss:
try:
if not oss_store.ready():
print("oss client is not init")
else:
ext = filename.split(".")[-1]
uuid = str(uuid4())
oss_filename = f"{uuid}.{ext}" if ext else uuid
oss_store.put(oss_filename, img_bytes)
oss_object_key = oss_store.object_key(oss_filename)
oss_url = oss_store.sign(oss_filename)
except Exception as e:
print(e)
pass
results.append(
{
"node_id": node_id,
"batch_id": index,
"output": {
"type": output_type,
"raw": {
**img,
"filename": filename,
"type": img_type,
"subfolder": sub_folder,
"filepath": (
os.path.join(img_type, sub_folder, filename)
if sub_folder
else os.path.join(img_type, filename)
),
},
"base64": {"content": img_output},
"oss": {
"region": oss_store.region,
"bucket": oss_store.bucket_name,
"object": oss_object_key,
"url": oss_url,
},
},
}
)
return {
"type": "serverless_api",
"data": {"prompt_id": prompt_id, "results": results},
}
def put_status_to_store(self, task_id: str, status: str):
"""
同步状态至持久化存储
Args:
task_id: 任务 id
status: 增量的状态信息
"""
if task_id and self.store:
try:
value = self.store.get(task_id)
self.store.put(task_id, f"{value}\n{status}")
except Exception as e:
print("put status to store failed, due to", e)
finally:
pass
def get_status_from_store(self, task_id: str):
if self.store:
value = self.store.get(task_id)
return [json.loads(line) for line in value.split("\n") if line]
else:
return []
def run(
self,
payload: map,
output_base64=False,
output_oss=False,
callback=None,
task_id: str = None,
):
"""
Serverless API 的核心逻辑
"""
try:
# 解析请求中是否存在 base64、http url 形式的图片
payload["prompt"] = self.parse_prompt(payload.get("prompt", {}))
client_id = ""
prompt_id = ""
ws_err = None
def on_message(ws: websocket.WebSocket, message: str):
try:
msg = json.loads(message)
msg_type = msg.get("type", "")
node_id = msg.get("data", {}).get("node", "")
current_prompt_id = msg.get("data", {}).get("prompt_id", "")
if msg_type == "status":
nonlocal client_id
client_id = msg.get("data", {}).get("sid", "")
if callback and hasattr(callback, "__call__"):
callback(message)
if task_id:
self.put_status_to_store(task_id, message)
if msg_type == "executing":
# 节点执行
if not node_id and current_prompt_id == prompt_id:
# 当前正在执行的 node 为空,说明 prompt 执行结束了
ws.close()
elif msg_type == "execution_error":
# 执行出错
ws.close()
raise ComfyUIException(
f"ComfyUI execution error: {msg.get('data', {}).get('exception_message', '')}",
constants.ERROR_CODE.EXECUTION_FAILED.value,
msg.get("data"),
)
else:
# 其他不处理的类型,如 "execution_start", "status", "progress", "execution_cached", "executed"
pass
except Exception as e:
print(e)
nonlocal ws_err
ws_err = e
ws.close()
ws = self.api_websocket(client_id, on_message)
ws_threading = threading.Thread(target=ws.run_forever)
ws_threading.start()
# 提交出图任务
while client_id == "":
time.sleep(0.1)
prompt_result = self.api_prompt(client_id, payload)
prompt_id = prompt_result.get("prompt_id", "")
# 如果 task id 未指定,则使用 prompt id
if not task_id:
task_id = prompt_id
if not prompt_id:
raise Exception("can not get prompt_id from ComfyUI")
# 已经有结果,则不必等待
if len(self.api_get_history(prompt_id)) > 0:
ws.close()
else:
ws_threading.join()
if ws_err:
raise ws_err
result = self.get_history_result(
prompt_id, output_base64=output_base64, output_oss=output_oss
)
self.put_status_to_store(task_id, json.dumps(result))
return result
except ComfyUIException as e:
self.put_status_to_store(
task_id,
json.dumps(e.response()),
)
raise e
except Exception as e:
self.put_status_to_store(
task_id,
json.dumps(
{
"type": "error",
"error_code": constants.ERROR_CODE.UNCLASSIFY.value,
"error_message": str(e),
}
),
)
raise e