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.
@@ -0,0 +1,6 @@
1
+ from .setup.Base_config import Basic_config
2
+ from .utils.run_download import download
3
+ from .checker.get_checkpoint_type import checkpoint_type
4
+ from .model_path.Perform_path_search import Search_cls
5
+
6
+ run_search = Search_cls()
@@ -0,0 +1 @@
1
+ from .memorize_config import config_check
@@ -0,0 +1,139 @@
1
+ import os
2
+ import torch
3
+ import requests
4
+ import yaml
5
+ from io import BytesIO
6
+
7
+ from ..setup.Base_config import Basic_config
8
+
9
+
10
+ class checkpoint_type(Basic_config):
11
+ def __init__(self):
12
+ super().__init__()
13
+
14
+
15
+ def checkpoint_type_get(
16
+ self,
17
+ checkpoint_path_or_dict,
18
+ config_files = None,
19
+ original_config_file = None,
20
+ model_type = None,
21
+ is_upscale = False
22
+ ):
23
+ """
24
+ NOTE:
25
+ Return SD if you do not know, because an error may occur if None is used.
26
+
27
+ About model_type:
28
+ The model_type itself will be left for possible use at some point.
29
+ """
30
+
31
+ from_safetensors = False
32
+ checkpoint = None
33
+ if isinstance(checkpoint_path_or_dict, str):
34
+ if os.path.isfile(checkpoint_path_or_dict):
35
+ from_safetensors: bool = self.check_for_safetensors(checkpoint_path_or_dict)
36
+ if from_safetensors:
37
+ from safetensors.torch import load_file as safe_load
38
+ checkpoint = safe_load(checkpoint_path_or_dict, device=self.device)
39
+ else:
40
+ checkpoint = torch.load(checkpoint_path_or_dict, map_location=self.device)
41
+ elif os.path.isdir(checkpoint_path_or_dict):
42
+ model_index_path = os.path.join(checkpoint_path_or_dict,"model_index.json")
43
+ if os.path.isfile(model_index_path):
44
+ with open(model_index_path,"r") as loaded_model_index:
45
+ cls_name = loaded_model_index["_class_name"]
46
+ #Fixed in due course.
47
+ if "XL" in cls_name:
48
+ return "SDXL"
49
+ else:
50
+ return "SD"
51
+
52
+ elif isinstance(checkpoint_path_or_dict, dict):
53
+ checkpoint = checkpoint_path_or_dict
54
+ else:
55
+ raise TypeError(f"checkpoint_path_or_dict: {checkpoint_path_or_dict}")
56
+
57
+ if "global_step" in checkpoint:
58
+ global_step = checkpoint["global_step"]
59
+ else:
60
+ global_step = None
61
+
62
+ while "state_dict" in checkpoint:
63
+ checkpoint = checkpoint["state_dict"]
64
+ if original_config_file is None:
65
+ key_name_v2_1 = "model.diffusion_model.input_blocks.2.1.transformer_blocks.0.attn2.to_k.weight"
66
+ key_name_sd_xl_base = "conditioner.embedders.1.model.transformer.resblocks.9.mlp.c_proj.bias"
67
+ key_name_sd_xl_refiner = "conditioner.embedders.0.model.transformer.resblocks.9.mlp.c_proj.bias"
68
+ #is_upscale = pipeline_class == StableDiffusionUpscalePipeline
69
+ config_url = None
70
+ # model_type = "v1"
71
+ if config_files is not None and "v1" in config_files:
72
+ original_config_file = config_files["v1"]
73
+ else:
74
+ config_url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"
75
+
76
+ if key_name_v2_1 in checkpoint and checkpoint[key_name_v2_1].shape[-1] == 1024:
77
+ # model_type = "v2"
78
+ if config_files is not None and "v2" in config_files:
79
+ original_config_file = config_files["v2"]
80
+ else:
81
+ config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference-v.yaml"
82
+ if global_step == 110000:
83
+ # v2.1 needs to upcast attention
84
+ upcast_attention = True
85
+
86
+ elif key_name_sd_xl_base in checkpoint:
87
+ # only base xl has two text embedders
88
+ if config_files is not None and "xl" in config_files:
89
+ original_config_file = config_files["xl"]
90
+ else:
91
+ config_url = "https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_base.yaml"
92
+
93
+ elif key_name_sd_xl_refiner in checkpoint:
94
+ # only refiner xl has embedder and one text embedders
95
+ if config_files is not None and "xl_refiner" in config_files:
96
+ original_config_file = config_files["xl_refiner"]
97
+ else:
98
+ config_url = "https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_refiner.yaml"
99
+
100
+ if is_upscale:
101
+ config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/x4-upscaling.yaml"
102
+
103
+ if config_url is not None:
104
+ try:
105
+ original_config_file = BytesIO(requests.get(config_url).content)
106
+ except:
107
+ self.logger.error(f"Could not download the Config_file to find out the model type from the following URL: {config_url}")
108
+ if model_type is None:
109
+ self.logger.warning("model_type is set to None")
110
+ return model_type
111
+ else:
112
+ with open(original_config_file, "r") as f:
113
+ original_config_file = f.read()
114
+ else:
115
+ with open(original_config_file, "r") as f:
116
+ original_config_file = f.read()
117
+
118
+ original_config = yaml.safe_load(original_config_file)
119
+
120
+ if (
121
+ model_type is None
122
+ and "cond_stage_config" in original_config["model"]["params"]
123
+ and original_config["model"]["params"]["cond_stage_config"] is not None
124
+ ):
125
+
126
+ model_type = original_config["model"]["params"]["cond_stage_config"]["target"].split(".")[-1]
127
+ return "SD"
128
+
129
+ elif model_type is None and original_config["model"]["params"]["network_config"] is not None:
130
+ if original_config["model"]["params"]["network_config"]["params"]["context_dim"] == 2048:
131
+ model_type = "SDXL"
132
+ else:
133
+ model_type = "SDXL-Refiner"
134
+ return "SDXL"
135
+
136
+ else:
137
+ if model_type is None:
138
+ self.logger.warning("model_type is set to None")
139
+ return model_type
@@ -0,0 +1,80 @@
1
+ import os
2
+ import json
3
+
4
+
5
+ class config_check:
6
+ base_config_json = "/tmp/auto_diffusers_config.json"
7
+ def __init__(self):
8
+ pass
9
+
10
+
11
+ def get_json_dict(self):
12
+ """Retrieve the JSON dictionary from the config file."""
13
+ config_dict = {}
14
+ if os.path.isfile(self.base_config_json):
15
+ try:
16
+ with open(self.base_config_json, "r") as basic_json:
17
+ config_dict = json.load(basic_json)
18
+ except json.JSONDecodeError:
19
+ pass
20
+ return config_dict
21
+
22
+
23
+ def update_json_dict(self, key, value):
24
+ """Update the JSON dictionary with a new key-value pair."""
25
+ basic_json_dict = self.get_json_dict()
26
+ basic_json_dict[key] = value
27
+ with open(self.base_config_json, "w") as json_file:
28
+ json.dump(basic_json_dict, json_file, indent=4)
29
+
30
+
31
+ def check_func_hist(self,*return_key, **kwargs):
32
+ """
33
+ Check and optionally update the history of a given element.
34
+
35
+ Args:
36
+ *return_key (str): Variable for which to get the history.
37
+ **kwargs: Keyword arguments for additional options.
38
+ - update (bool): Whether to update the dictionary. Default is True.
39
+ - return_value (bool): Whether to return the element value. Default is False.
40
+ - key (str): Specific key to look up in the dictionary.
41
+ - value (Any): Value to be matched or updated in the dictionary.
42
+
43
+ Returns:
44
+ Any: The historical value if `return_value` is True, or a boolean indicating
45
+ if the value matches the historical value.
46
+ """
47
+ update = kwargs.pop("update", True)
48
+ return_value = kwargs.pop("return_value", False)
49
+ if kwargs:
50
+ if "key" in kwargs:
51
+ key = kwargs["key"]
52
+ if "value" in kwargs:
53
+ value = kwargs["value"]
54
+ else:
55
+ value = None
56
+ update = False
57
+ return_value = True
58
+ else:
59
+ key, value = next(iter(kwargs.items()))
60
+ elif return_key:
61
+ key, value = return_key[0], None
62
+ update = False
63
+ return_value = True
64
+ else:
65
+ raise TypeError("Missing 'key' argument.")
66
+
67
+ basic_json_dict = self.get_json_dict()
68
+ hist_value = basic_json_dict.get(key)
69
+ if hist_value == value:
70
+ value_match = True
71
+ else:
72
+ value_match = False
73
+
74
+ if update:
75
+ self.update_json_dict(key, value)
76
+
77
+ if return_value:
78
+ return hist_value
79
+ else:
80
+ return value_match
@@ -0,0 +1,228 @@
1
+ import os
2
+
3
+
4
+ from .mix_class import Config_Mix
5
+
6
+
7
+ class Search_cls(Config_Mix):
8
+ def __init__(self):
9
+ super().__init__()
10
+
11
+
12
+ def __call__(
13
+ self,
14
+ seach_word,
15
+ auto=True,
16
+ download=False,
17
+ model_type="Checkpoint",
18
+ branch = "main",
19
+ single_file_only = False,
20
+ local_file_only = False,
21
+ return_path = True
22
+ ):
23
+ self.seach_word = seach_word
24
+ self.auto = auto
25
+ self.download = download
26
+ self.model_type = model_type
27
+ self.branch = branch
28
+ self.single_file_only = single_file_only
29
+ self.local_file_only = local_file_only
30
+ self.return_path = return_path
31
+ result = self.model_set(
32
+ model_select = self.seach_word,
33
+ auto = self.auto,
34
+ download = self.download,
35
+ model_type = self.model_type,
36
+ branch = self.branch,
37
+ local_file_only = self.local_file_only,
38
+ return_path = self.return_path
39
+ )
40
+ return result
41
+
42
+
43
+ def File_search(
44
+ self,
45
+ search_word,
46
+ auto = True
47
+ ):
48
+ """
49
+ only single file
50
+ """
51
+ search_path=""
52
+ paths = []
53
+ for root, dirs, files in os.walk("/"):
54
+ for file in files:
55
+ if any(file.endswith(ext) for ext in self.exts):
56
+ path = os.path.join(root, file)
57
+ if path not in self.exclude:
58
+ if not path.startswith("/root/.cache"):
59
+ paths.append(path)
60
+ num_path=len(paths)
61
+ if not num_path:
62
+ raise FileNotFoundError("\033[33mModel File not found\033[0m")
63
+ elif not auto:
64
+ print(f"{num_path} candidate model files found.")
65
+ for s, path in enumerate(paths, 1):
66
+ print(f"{s}: {path}")
67
+ num = int(input(f"Please enter a number(1〜{num_path}): "))
68
+ if 1 <= num <= len(paths):
69
+ search_path=(paths[num-1])
70
+ print(f"Selected model file: {search_path}\n")
71
+ else:
72
+ raise TypeError(f"\033[33mOnly natural numbers in the following range are valid : (1〜{len(paths)})\033[0m")
73
+ else:
74
+ search_path = self.max_temper(
75
+ search_word = search_word,
76
+ search_list = paths
77
+ )
78
+ return search_path
79
+
80
+
81
+ def model_set(self,
82
+ model_select,
83
+ auto = True,
84
+ download = False,
85
+ model_type = "Checkpoint",
86
+ branch = "main",
87
+ local_file_only = False,
88
+ return_path = True):
89
+ """
90
+ return:
91
+ if path_only is false
92
+ [model_path:str, {base_model_path: str,single_file: bool}]
93
+ """
94
+
95
+ if not model_type in ["Checkpoint", "TextualInversion", "LORA", "Hypernetwork", "AestheticGradient", "Controlnet", "Poses"]:
96
+ raise TypeError(f'Wrong argument. Valid values are "Checkpoint", "TextualInversion", "LORA", "Hypernetwork", "AestheticGradient", "Controlnet", "Poses". What was passed on {model_type}')
97
+
98
+ return_dict = {
99
+ "url_or_path":"",
100
+ "search_word":model_select,
101
+ "single_file":False,
102
+ "local":True if download or local_file_only else False,
103
+ "civitai_url":"",
104
+ }
105
+
106
+ model_path = model_select
107
+ file_path = ""
108
+ if model_select in self.model_dict:
109
+ model_path_to_check = self.model_dict[model_select]
110
+ if self.is_url_valid(f"https://huggingface.co/{model_path_to_check}"):
111
+ model_select = model_path_to_check
112
+
113
+ if local_file_only:
114
+ model_path = self.File_search(
115
+ search_word = model_select,
116
+ auto = auto
117
+ )
118
+ return_dict["single_file"] = True
119
+ return_dict["url_or_path"] = model_path
120
+
121
+ elif model_select.startswith("https://huggingface.co/"):
122
+ if not self.is_url_valid(model_select):
123
+ raise ValueError(self.Error_M1)
124
+ else:
125
+ if download:
126
+ model_path, single_file = self.run_hf_download(model_select)
127
+ return_dict["single_file"] = single_file
128
+ return_dict["url_or_path"] = model_path
129
+ else:
130
+ model_path = model_select
131
+ return_dict["single_file"] = True
132
+ return_dict["url_or_path"] = model_path
133
+
134
+ elif model_select.startswith("https://civitai.com/"):
135
+ #local file
136
+ model_path = self.public_civiai(model_select,
137
+ auto,
138
+ model_type)
139
+ return_dict["single_file"] = True
140
+
141
+ elif os.path.isfile(model_select):
142
+ model_path = model_select
143
+ return_dict["url_or_path"] = model_select
144
+ return_dict["single_file"] = True
145
+ return_dict["local"] = True
146
+
147
+ elif os.path.isdir(model_select):
148
+ if os.path.exists(os.path.join(model_select,self.Config_file)):
149
+ model_path = model_select
150
+ return_dict["url_or_path"] = model_select
151
+ return_dict["single_file"] = False
152
+ return_dict["local"] = True
153
+ else:
154
+ raise FileNotFoundError(f"model_index.json not found in {model_select}")
155
+
156
+ elif model_select.count("/") == 1:
157
+ if auto and self.diffusers_model_check(model_select):
158
+ if download:
159
+ model_path,single_file = self.run_hf_download(model_select)
160
+ return_dict["single_file"] = False
161
+ else:
162
+ model_path = model_select
163
+ return_dict["single_file"] = False
164
+ elif auto and (not self.hf_model_check(model_select)):
165
+ raise ValueError(f'The specified repository could not be found, please try turning off "auto" (model_select:{model_select})')
166
+ else:
167
+ file_path=self.file_name_set(model_select,auto,model_type)
168
+ if file_path == "_hf_no_model":
169
+ raise ValueError("Model not found")
170
+ elif file_path == "_DFmodel":
171
+ if download:
172
+ model_path,single_file = self.run_hf_download(model_select)
173
+ return_dict["single_file"] = False
174
+ else:
175
+ model_path = model_select
176
+ return_dict["single_file"] = False
177
+ else:
178
+ hf_model_path=f"https://huggingface.co/{model_select}/blob/{branch}/{file_path}"
179
+ if download:
180
+ model_path,single_file = self.run_hf_download(hf_model_path)
181
+ return_dict["single_file"] = single_file
182
+
183
+ else:
184
+ model_path = hf_model_path
185
+ return_dict["single_file"] = True
186
+
187
+ else:
188
+ model_name = self.model_name_search(model_select,auto)
189
+ #self.hf_repo_id = model_name
190
+ #hf->civit
191
+ if not model_name == "_hf_no_model":
192
+ file_path = self.file_name_set(model_name,auto,model_type)
193
+ if file_path == "_DFmodel":
194
+ if download:
195
+ model_path,single_file = self.run_hf_download(model_name)
196
+ return_dict["single_file"] = False
197
+ else:
198
+ model_path = model_name
199
+ return_dict["single_file"] = False
200
+
201
+ else:
202
+ hf_model_path = f"https://huggingface.co/{model_name}/blob/{branch}/{file_path}"
203
+ if download:
204
+ model_path,single_file = self.run_hf_download(hf_model_path)
205
+ return_dict["single_file"] = single_file
206
+ else:
207
+ model_path = hf_model_path
208
+ return_dict["single_file"] = True
209
+
210
+ else:
211
+ model_url, model_path = self.civitai_download(
212
+ model_select,
213
+ auto,
214
+ model_type,
215
+ download=download)
216
+
217
+ return_dict["single_file"] = True
218
+ return_dict["civitai_url"] = model_url
219
+ return_dict["local"] = True if download else False
220
+
221
+ #It is not called, but should not be deleted because it is updating the dictionary.
222
+ update_model_path = self.check_func_hist(key="model_path",value=model_path)
223
+ return_dict["url_or_path"] = model_path
224
+ if return_path:
225
+ return model_path
226
+ else:
227
+ return [model_path, return_dict]
228
+
@@ -0,0 +1,7 @@
1
+ from .mix_class import Config_Mix
2
+ from .Perform_path_search import Search_cls
3
+
4
+
5
+
6
+
7
+