auto-diffusers 1.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- auto_diffusers/__init__.py +6 -0
- auto_diffusers/checker/__init__.py +1 -0
- auto_diffusers/checker/get_checkpoint_type.py +139 -0
- auto_diffusers/checker/memorize_config.py +80 -0
- auto_diffusers/model_path/Perform_path_search.py +228 -0
- auto_diffusers/model_path/__init__.py +7 -0
- auto_diffusers/model_path/create_pipeline.py +253 -0
- auto_diffusers/model_path/flax_config.py +70 -0
- auto_diffusers/model_path/mix_class.py +15 -0
- auto_diffusers/model_path/search_civitai.py +385 -0
- auto_diffusers/model_path/search_hugface.py +418 -0
- auto_diffusers/py.typed +1 -0
- auto_diffusers/setup/Base_config.py +213 -0
- auto_diffusers/setup/__init__.py +1 -0
- auto_diffusers/setup/data_class.py +130 -0
- auto_diffusers/setup/device_config.py +42 -0
- auto_diffusers/setup/runtime_config.py +49 -0
- auto_diffusers/utils/__init__.py +1 -0
- auto_diffusers/utils/device_config.py +40 -0
- auto_diffusers/utils/get_custom_logger.py +22 -0
- auto_diffusers/utils/hide_warning.py +8 -0
- auto_diffusers/utils/process_bar.py +173 -0
- auto_diffusers/utils/run_download.py +140 -0
- auto_diffusers-1.3.1.dist-info/LICENSE +28 -0
- auto_diffusers-1.3.1.dist-info/METADATA +112 -0
- auto_diffusers-1.3.1.dist-info/RECORD +28 -0
- auto_diffusers-1.3.1.dist-info/WHEEL +5 -0
- auto_diffusers-1.3.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
from diffusers import StableDiffusionPipeline
|
|
5
|
+
|
|
6
|
+
from .mix_class import Config_Mix
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class pipeline_setup(Config_Mix):
|
|
10
|
+
def __init__(self):
|
|
11
|
+
self.use_TPU = self.is_TPU()
|
|
12
|
+
super().__init__()
|
|
13
|
+
self.model_path = ""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def File_search(self):
|
|
17
|
+
"""
|
|
18
|
+
only single file
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
search_path=""
|
|
22
|
+
paths = []
|
|
23
|
+
for root, dirs, files in os.walk("/"):
|
|
24
|
+
for file in files:
|
|
25
|
+
if any(file.endswith(ext) for ext in self.exts):
|
|
26
|
+
path = os.path.join(root, file)
|
|
27
|
+
if path not in self.exclude:
|
|
28
|
+
if not path.startswith("/root/.cache"):
|
|
29
|
+
paths.append(path)
|
|
30
|
+
num_path=len(paths)
|
|
31
|
+
if not num_path:
|
|
32
|
+
raise FileNotFoundError("\033[33mModel File not found\033[0m")
|
|
33
|
+
else:
|
|
34
|
+
print(f"{num_path} candidate model files found.")
|
|
35
|
+
for s, path in enumerate(paths, 1):
|
|
36
|
+
print(f"{s}: {path}")
|
|
37
|
+
num = int(input(f"Please enter a number(1〜{num_path}): "))
|
|
38
|
+
if 1 <= num <= len(paths):
|
|
39
|
+
search_path=(paths[num-1])
|
|
40
|
+
print(f"Selected model file: {search_path}\n")
|
|
41
|
+
else:
|
|
42
|
+
raise TypeError(f"\033[33mOnly natural numbers in the following range are valid : (1〜{len(paths)})\033[0m")
|
|
43
|
+
return search_path
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def model_set(self,
|
|
47
|
+
model_select,
|
|
48
|
+
auto = True,
|
|
49
|
+
model_type = "Checkpoint",
|
|
50
|
+
branch = "main",
|
|
51
|
+
download: bool = False,
|
|
52
|
+
path_only: bool=True):
|
|
53
|
+
"""
|
|
54
|
+
return:
|
|
55
|
+
if path_only is false
|
|
56
|
+
[model_path:str, {base_model_path: str,from_single_file: bool}]
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
if not model_type in ["Checkpoint", "TextualInversion", "LORA", "Hypernetwork", "AestheticGradient", "Controlnet", "Poses"]:
|
|
60
|
+
raise TypeError(f'Wrong argument. Valid values are "Checkpoint", "TextualInversion", "LORA", "Hypernetwork", "AestheticGradient", "Controlnet", "Poses". What was passed on {model_type}')
|
|
61
|
+
local = True if download else False
|
|
62
|
+
return_dict = {"base_model_path":model_select,
|
|
63
|
+
"from_dingle_file":False,
|
|
64
|
+
"local":local,
|
|
65
|
+
"url_or_path":"",
|
|
66
|
+
}
|
|
67
|
+
model_path = ""
|
|
68
|
+
file_path = ""
|
|
69
|
+
if model_select in self.model_dict:
|
|
70
|
+
model_path_to_check = self.model_dict[model_select]
|
|
71
|
+
if self.is_url_valid(f"https://huggingface.co/{model_path_to_check}"):
|
|
72
|
+
model_select = model_path_to_check
|
|
73
|
+
|
|
74
|
+
if model_select == "search":
|
|
75
|
+
#only file
|
|
76
|
+
model_path = self.File_search()
|
|
77
|
+
return_dict["from_single_file"] = False
|
|
78
|
+
return_dict["url_or_path"] = model_path
|
|
79
|
+
|
|
80
|
+
elif model_select.startswith("https://huggingface.co/"):
|
|
81
|
+
if not self.is_url_valid(model_select):
|
|
82
|
+
raise ValueError(self.Error_M1)
|
|
83
|
+
else:
|
|
84
|
+
if download:
|
|
85
|
+
model_path = self.run_hf_download(model_select)
|
|
86
|
+
return_dict["from_single_file"] = False
|
|
87
|
+
return_dict["url_or_path"] = model_path
|
|
88
|
+
else:
|
|
89
|
+
model_path = model_select
|
|
90
|
+
return_dict["from_single_file"] = True
|
|
91
|
+
return_dict["url_or_path"] = model_path
|
|
92
|
+
|
|
93
|
+
elif model_select.startswith("https://civitai.com/"):
|
|
94
|
+
#local file
|
|
95
|
+
model_path = self.public_civiai(model_select,
|
|
96
|
+
auto,
|
|
97
|
+
model_type)
|
|
98
|
+
return_dict["from_single_file"] = True
|
|
99
|
+
|
|
100
|
+
elif os.path.isfile(model_select):
|
|
101
|
+
model_path = model_select
|
|
102
|
+
return_dict["from_single_file"] = True
|
|
103
|
+
return_dict["local"] = True
|
|
104
|
+
|
|
105
|
+
elif os.path.isdir(model_select):
|
|
106
|
+
if os.path.exists(os.path.join(model_select,self.Config_file)):
|
|
107
|
+
return_dict["model_path"] = model_select
|
|
108
|
+
return_dict["from_single_file"] = False
|
|
109
|
+
return_dict["local"] = True
|
|
110
|
+
else:
|
|
111
|
+
raise FileNotFoundError(f"model_index.json not found in {model_select}")
|
|
112
|
+
|
|
113
|
+
elif model_select.count("/") == 1:
|
|
114
|
+
if auto and self.diffusers_model_check(model_select):
|
|
115
|
+
if download:
|
|
116
|
+
model_path = self.run_hf_download(model_select)
|
|
117
|
+
return_dict["from_single_file"] = False
|
|
118
|
+
else:
|
|
119
|
+
model_path = model_select
|
|
120
|
+
return_dict["from_single_file"] = False
|
|
121
|
+
elif auto and (not self.hf_model_check(model_select)):
|
|
122
|
+
raise ValueError(f'The specified repository could not be found, please try turning off "auto" (model_select:{model_select})')
|
|
123
|
+
else:
|
|
124
|
+
file_path=self.file_name_set(model_select,auto,model_type)
|
|
125
|
+
if file_path == "_hf_no_model":
|
|
126
|
+
raise ValueError("Model not found")
|
|
127
|
+
elif file_path == "_DFmodel":
|
|
128
|
+
if download:
|
|
129
|
+
model_path = self.run_hf_download(model_select)
|
|
130
|
+
return_dict["from_single_file"] = False
|
|
131
|
+
else:
|
|
132
|
+
model_path = model_select
|
|
133
|
+
return_dict["from_single_file"] = False
|
|
134
|
+
else:
|
|
135
|
+
hf_model_path=f"https://huggingface.co/{model_select}/blob/{branch}/{file_path}"
|
|
136
|
+
if download:
|
|
137
|
+
model_path = self.run_hf_download(hf_model_path)
|
|
138
|
+
return_dict["from_single_file"] = True
|
|
139
|
+
|
|
140
|
+
else:
|
|
141
|
+
model_path = hf_model_path
|
|
142
|
+
return_dict["from_single_file"] = True
|
|
143
|
+
|
|
144
|
+
else:
|
|
145
|
+
model_name = self.model_name_search(model_select,auto)
|
|
146
|
+
#self.hf_repo_id = model_name
|
|
147
|
+
#hf->civit
|
|
148
|
+
if not model_name == "_hf_no_model":
|
|
149
|
+
file_path = self.file_name_set(model_name,auto,model_type)
|
|
150
|
+
if model_path == "_DFmodel":
|
|
151
|
+
if download:
|
|
152
|
+
model_path = self.run_hf_download(file_path)
|
|
153
|
+
return_dict["from_single_file"] = False
|
|
154
|
+
else:
|
|
155
|
+
model_path = model_name #f"https://huggingface.co/{model_name}"
|
|
156
|
+
return_dict["from_single_file"] = False
|
|
157
|
+
|
|
158
|
+
else:
|
|
159
|
+
hf_model_path = f"https://huggingface.co/{model_name}/blob/{branch}/{file_path}"
|
|
160
|
+
if download:
|
|
161
|
+
model_path = self.run_hf_download(hf_model_path)
|
|
162
|
+
return_dict["from_single_file"] = True
|
|
163
|
+
else:
|
|
164
|
+
model_path = hf_model_path
|
|
165
|
+
return_dict["from_single_file"] = True
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
else:
|
|
169
|
+
model_url, model_path = self.civitai_download(
|
|
170
|
+
model_select,
|
|
171
|
+
auto,
|
|
172
|
+
model_type)
|
|
173
|
+
|
|
174
|
+
return_dict["from_single_file"] = True
|
|
175
|
+
return_dict["url_or_path"] = model_url
|
|
176
|
+
|
|
177
|
+
if not return_dict["url_or_path"]:
|
|
178
|
+
return_dict["url_or_path"] = model_path
|
|
179
|
+
|
|
180
|
+
if path_only:
|
|
181
|
+
return model_path
|
|
182
|
+
else:
|
|
183
|
+
return [model_path,return_dict]
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def pipe_status_check(self,pipeline):
|
|
188
|
+
from diffusers.pipelines.stable_diffusion import (StableDiffusionSafetyChecker,FlaxStableDiffusionSafetyChecker)
|
|
189
|
+
from transformers import CLIPImageProcessor
|
|
190
|
+
pipe_class_name_ = pipeline.__class__.__name__
|
|
191
|
+
pipe_type = self.pipeline_metod_type(self.import_on_str(pipe_class_name_,"diffusers"))
|
|
192
|
+
if hasattr(pipeline,"safety_checker"):
|
|
193
|
+
if getattr(pipeline,"safety_checker") is None:
|
|
194
|
+
if pipe_type == "flax":
|
|
195
|
+
pipeline.safety_checker = FlaxStableDiffusionSafetyChecker.from_pretrained(
|
|
196
|
+
"CompVis/stable-diffusion-safety-checker", from_pt=True
|
|
197
|
+
)
|
|
198
|
+
elif pipe_type in ["torch", "onnx"]:
|
|
199
|
+
pipeline.safety_checker = StableDiffusionSafetyChecker.from_pretrained(
|
|
200
|
+
"CompVis/stable-diffusion-safety-checker"
|
|
201
|
+
)
|
|
202
|
+
if hasattr(pipeline,"feature_extractor"):
|
|
203
|
+
if getattr(pipeline,"feature_extractor") is None:
|
|
204
|
+
pipeline.feature_extractor = CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
|
205
|
+
return pipeline
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def pipe_create(self,
|
|
209
|
+
model_path,
|
|
210
|
+
from_single_file):
|
|
211
|
+
if from_single_file:
|
|
212
|
+
#not from_confg
|
|
213
|
+
base_pipe = StableDiffusionPipeline.from_single_file(
|
|
214
|
+
model_path
|
|
215
|
+
).to(self.device)
|
|
216
|
+
else:
|
|
217
|
+
base_pipe = StableDiffusionPipeline.from_pretrained(
|
|
218
|
+
model_path
|
|
219
|
+
).to(self.device)
|
|
220
|
+
|
|
221
|
+
if self.device == "cuda":
|
|
222
|
+
base_pipe.to(torch_dtype = torch.float16)
|
|
223
|
+
|
|
224
|
+
return base_pipe
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def pipeline_task(self,model_select,auto):
|
|
228
|
+
params = None
|
|
229
|
+
model_path,model_dict = self.model_set(model_select,
|
|
230
|
+
auto = auto,
|
|
231
|
+
download = False)
|
|
232
|
+
#model_path = model_dict["base_model_path"]
|
|
233
|
+
from_single_file = model_dict["from_single_file"]
|
|
234
|
+
|
|
235
|
+
update_model_path = self.check_func_hist(key="model_path",value=model_path)
|
|
236
|
+
|
|
237
|
+
if self.use_TPU:
|
|
238
|
+
try:
|
|
239
|
+
base_pipe,params = self.Flax_pipe_create(model_path)
|
|
240
|
+
except OSError as a:
|
|
241
|
+
self.logger.debug(a)
|
|
242
|
+
raise OSError("Check your internet connection")
|
|
243
|
+
|
|
244
|
+
else:
|
|
245
|
+
try:
|
|
246
|
+
base_pipe = self.pipe_create(model_path, from_single_file)
|
|
247
|
+
except OSError as a:
|
|
248
|
+
self.logger.debug(a)
|
|
249
|
+
raise OSError("Check your internet connection")
|
|
250
|
+
base_pipe = self.pipe_status_check(base_pipe)
|
|
251
|
+
if self.device_type == "cpu" and base_pipe.dtype == torch.float16:
|
|
252
|
+
self.logger.warning("CPU cannot use base_pipe with half precision (torch.float16)")
|
|
253
|
+
return base_pipe, params, model_dict["url_or_path"]
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
import torch
|
|
4
|
+
import requests
|
|
5
|
+
import gc
|
|
6
|
+
import jax
|
|
7
|
+
import json
|
|
8
|
+
from requests import HTTPError
|
|
9
|
+
from tqdm.auto import tqdm
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
from diffusers import FlaxDiffusionPipeline
|
|
13
|
+
|
|
14
|
+
from ..setup.Base_config import Basic_config
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class With_Flax(Basic_config):
|
|
18
|
+
def __init__(self):
|
|
19
|
+
super().__init__()
|
|
20
|
+
|
|
21
|
+
def sd_to_flax(self,url_or_path):
|
|
22
|
+
hf_path,file_name,model_file_path="","",""
|
|
23
|
+
#from_config or from_single_file
|
|
24
|
+
if os.path.isfile(url_or_path):
|
|
25
|
+
model_file_path=url_or_path
|
|
26
|
+
|
|
27
|
+
#from_pretrain
|
|
28
|
+
elif os.path.isdir(url_or_path):
|
|
29
|
+
if os.path.exists(os.path.join(url_or_path, self.Config_file)):
|
|
30
|
+
return url_or_path
|
|
31
|
+
|
|
32
|
+
else:
|
|
33
|
+
raise FileNotFoundError(f"model_index.json not found in '{url_or_path}'")
|
|
34
|
+
else:
|
|
35
|
+
raise FileNotFoundError("Invalid dir_path.")
|
|
36
|
+
|
|
37
|
+
model_saved_dir = os.path.join(os.path.dirname(model_file_path),"converted")
|
|
38
|
+
os.makedirs(model_saved_dir,exist_ok=True)
|
|
39
|
+
if not os.path.isfile(os.path.join(model_saved_dir,"model_index.json")):
|
|
40
|
+
print("Converting the model...")
|
|
41
|
+
#from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt
|
|
42
|
+
is_from_safetensors = self.check_for_safetensors(url_or_path)
|
|
43
|
+
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt
|
|
44
|
+
save_pipeline = download_from_original_stable_diffusion_ckpt(url_or_path, from_safetensors = is_from_safetensors)
|
|
45
|
+
#can not use output format :safetensors
|
|
46
|
+
save_pipeline.save_pretrained(model_saved_dir, safe_serialization = False)
|
|
47
|
+
print("End of model conversion")
|
|
48
|
+
del save_pipeline
|
|
49
|
+
return model_saved_dir
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def Flax_pipe_create(self,url_or_path):
|
|
53
|
+
model_dir_path=self.sd_to_flax(url_or_path)
|
|
54
|
+
model_index_path=os.path.join(model_dir_path,self.Config_file)
|
|
55
|
+
with open(model_index_path, "r") as f:
|
|
56
|
+
pipeline_class_name = json.load(f)["_class_name"]
|
|
57
|
+
#pipeline_class = getattr(diffusers, pipeline_class_name)
|
|
58
|
+
self.logger.info(f"Pipeline class imported: {pipeline_class_name}.")
|
|
59
|
+
try:
|
|
60
|
+
base_pipe,base_params = FlaxDiffusionPipeline.from_pretrained(
|
|
61
|
+
model_dir_path,
|
|
62
|
+
dtype=jax.numpy.bfloat16,
|
|
63
|
+
use_safetensors=True
|
|
64
|
+
)
|
|
65
|
+
except ValueError:
|
|
66
|
+
raise ValueError("Insufficient memory.")
|
|
67
|
+
#from flax.jax_utils import replicate
|
|
68
|
+
#params = replicate(base_params)
|
|
69
|
+
params=base_params
|
|
70
|
+
return base_pipe,params
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .search_hugface import Huggingface
|
|
2
|
+
from .search_civitai import Civitai
|
|
3
|
+
from .flax_config import With_Flax
|
|
4
|
+
from ..setup.Base_config import Basic_config
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Config_Mix(
|
|
8
|
+
Huggingface,
|
|
9
|
+
Civitai,
|
|
10
|
+
With_Flax,
|
|
11
|
+
Basic_config
|
|
12
|
+
):
|
|
13
|
+
#fix MMO error
|
|
14
|
+
def __init__(self):
|
|
15
|
+
super().__init__()
|