forked from hayden-fr/ComfyUI-Model-Manager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
178 lines (137 loc) · 5.4 KB
/
__init__.py
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
from aiohttp import web
import server
import os
model_uri = os.path.join(os.getcwd(), "models")
extension_uri = os.path.join(os.getcwd(), "custom_nodes/ComfyUI-Model-Manager")
model_type_dir_dict = {
"checkpoint": "checkpoints",
"clip": "clip",
"clip_vision": "clip_vision",
"controlnet": "controlnet",
"diffuser": "diffusers",
"embedding": "embeddings",
"gligen": "gligen",
"hypernetwork": "hypernetworks",
"lora": "loras",
"style_models": "style_models",
"unet": "unet",
"upscale_model": "upscale_models",
"vae": "vae",
"vae_approx": "vae_approx",
}
@server.PromptServer.instance.routes.get("/model-manager/imgPreview")
async def img_preview(request):
uri = request.query.get("uri")
filepath = os.path.join(model_uri, uri)
if os.path.exists(filepath):
with open(filepath, "rb") as img_file:
image_data = img_file.read()
else:
with open(os.path.join(extension_uri, "no-preview.png"), "rb") as img_file:
image_data = img_file.read()
return web.Response(body=image_data, content_type="image/png")
import json
@server.PromptServer.instance.routes.get("/model-manager/source")
async def load_source_from(request):
uri = request.query.get("uri", "local")
if uri == "local":
with open(os.path.join(extension_uri, "index.json")) as file:
dataSource = json.load(file)
else:
response = requests.get(uri)
dataSource = response.json()
# check if it installed
for item in dataSource:
model_type = item.get("type")
model_name = item.get("name")
model_type_path = model_type_dir_dict.get(model_type)
if model_type_path is None:
continue
if os.path.exists(os.path.join(model_uri, model_type_path, model_name)):
item["installed"] = True
return web.json_response(dataSource)
@server.PromptServer.instance.routes.get("/model-manager/models")
async def load_download_models(request):
model_types = os.listdir(model_uri)
model_types = sorted(model_types)
model_types = [content for content in model_types if content != "configs"]
model_suffix = (".safetensors", ".pt", ".pth", ".bin", ".ckpt")
models = {}
for model_type in model_types:
model_type_uri = os.path.join(model_uri, model_type)
filenames = os.listdir(model_type_uri)
filenames = sorted(filenames)
model_files = [f for f in filenames if f.endswith(model_suffix)]
def name2item(name):
item = {"name": name}
file_name, ext = os.path.splitext(name)
post_name = file_name + ".png"
if post_name in filenames:
post_path = os.path.join(model_type, post_name)
item["post"] = post_path
return item
model_items = list(map(name2item, model_files))
models[model_type] = model_items
return web.json_response(models)
import sys
import requests
requests.packages.urllib3.disable_warnings()
def_headers = {
"User-Agent": "Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
}
def download_model_file(url, filename):
dl_filename = filename + ".download"
rh = requests.get(
url=url, stream=True, verify=False, headers=def_headers, proxies=None
)
print("temp file is " + dl_filename)
total_size = int(rh.headers["Content-Length"])
basename, ext = os.path.splitext(filename)
print("Start download {}, file size: {}".format(basename, total_size))
downloaded_size = 0
if os.path.exists(dl_filename):
# downloaded_size = os.path.getsize(download_file)
downloaded_size = os.path.getsize(dl_filename)
headers = {"Range": "bytes=%d-" % downloaded_size}
headers["User-Agent"] = def_headers["User-Agent"]
r = requests.get(url=url, stream=True, verify=False, headers=headers, proxies=None)
with open(dl_filename, "ab") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
downloaded_size += len(chunk)
f.write(chunk)
f.flush()
progress = int(50 * downloaded_size / total_size)
try:
sys.stdout.reconfigure(encoding="utf-8")
except AttributeError:
pass
sys.stdout.write(
"\r[%s%s] %d%%"
% (
"-" * progress,
" " * (50 - progress),
100 * downloaded_size / total_size,
)
)
sys.stdout.flush()
print()
os.rename(dl_filename, filename)
@server.PromptServer.instance.routes.post("/model-manager/download")
async def download_file(request):
body = await request.json()
model_type = body.get("type")
model_type_path = model_type_dir_dict.get(model_type)
if model_type_path is None:
return web.json_response({"success": False})
download_uri = body.get("download")
if download_uri is None:
return web.json_response({"success": False})
model_name = body.get("name")
file_name = os.path.join(model_uri, model_type_path, model_name)
download_model_file(download_uri, file_name)
print("文件下载完成!")
return web.json_response({"success": True})
WEB_DIRECTORY = "web"
NODE_CLASS_MAPPINGS = {}
__all__ = ["NODE_CLASS_MAPPINGS"]