Spaces:
Running on Zero
Running on Zero
Download vda/third_party/diffsynth/models/model_loader.py from joeWorld/video-diffusion-avatars: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/spaces/joeWorld/video-diffusion-avatars/resolve/main/vda/third_party/diffsynth/models/model_loader.py
- Command line
-
hf download hf://spaces/joeWorld/video-diffusion-avatars/vda/third_party/diffsynth/models/model_loader.py
-
curl -L -o model_loader.py https://huggingface.co/spaces/joeWorld/video-diffusion-avatars/resolve/main/vda/third_party/diffsynth/models/model_loader.py
3.8 kB
| # From DiffSynth-Studio (https://github.com/modelscope/DiffSynth-Studio), Apache-2.0, see | |
| # vda/third_party/diffsynth/LICENSE. Changed for video-diffusion-avatars: reduced to what the released | |
| # inference uses, plus the additions described below where present. These changes and additions are | |
| # licensed under CC BY-NC-SA 4.0 (LICENSE at the repository root); the upstream code stays Apache-2.0. | |
| from ..core.loader import load_model, hash_model_file | |
| from ..configs import MODEL_CONFIGS | |
| import importlib, json | |
| class ModelPool: | |
| def __init__(self): | |
| self.model = [] | |
| self.model_name = [] | |
| self.model_path = [] | |
| def import_model_class(self, model_class): | |
| split = model_class.rfind(".") | |
| model_resource, model_class = model_class[:split], model_class[split+1:] | |
| model_class = importlib.import_module(model_resource).__getattribute__(model_class) | |
| return model_class | |
| def load_model_file(self, config, path, torch_dtype, device): | |
| model_class = self.import_model_class(config["model_class"]) | |
| if "state_dict_converter" in config: | |
| state_dict_converter = self.import_model_class(config["state_dict_converter"]) | |
| else: | |
| state_dict_converter = None | |
| return load_model(model_class, path, config.get("extra_kwargs", {}), torch_dtype, device, state_dict_converter) | |
| def auto_load_model(self, path, torch_dtype, device): | |
| """Load every model class whose config matches the file's state-dict hash.""" | |
| print(f"Loading models from: {json.dumps(path, indent=4)}") | |
| model_hash = hash_model_file(path) | |
| loaded = False | |
| for config in MODEL_CONFIGS: | |
| if config["model_hash"] == model_hash: | |
| model = self.load_model_file(config, path, torch_dtype, device) | |
| self.model.append(model) | |
| model_name = config["model_name"] | |
| self.model_name.append(model_name) | |
| self.model_path.append(path) | |
| model_info = {"model_name": model_name, "model_class": config["model_class"], "extra_kwargs": config.get("extra_kwargs")} | |
| print(f"Loaded model: {json.dumps(model_info, indent=4)}") | |
| loaded = True | |
| if not loaded: | |
| raise ValueError(f"Cannot detect the model type. File: {path}. Model hash: {model_hash}") | |
| def fetch_model(self, model_name, index=None): | |
| fetched_models = [] | |
| fetched_model_paths = [] | |
| for model, model_path, model_name_ in zip(self.model, self.model_path, self.model_name): | |
| if model_name == model_name_: | |
| fetched_models.append(model) | |
| fetched_model_paths.append(model_path) | |
| if len(fetched_models) == 0: | |
| print(f"No {model_name} models available. This is not an error.") | |
| model = None | |
| elif len(fetched_models) == 1: | |
| print(f"Using {model_name} from {json.dumps(fetched_model_paths[0], indent=4)}.") | |
| model = fetched_models[0] | |
| else: | |
| if index is None: | |
| model = fetched_models[0] | |
| print(f"More than one {model_name} models are loaded: {fetched_model_paths}. Using {model_name} from {json.dumps(fetched_model_paths[0], indent=4)}.") | |
| elif isinstance(index, int): | |
| model = fetched_models[:index] | |
| print(f"More than one {model_name} models are loaded: {fetched_model_paths}. Using {model_name} from {json.dumps(fetched_model_paths[:index], indent=4)}.") | |
| else: | |
| model = fetched_models | |
| print(f"More than one {model_name} models are loaded: {fetched_model_paths}. Using {model_name} from {json.dumps(fetched_model_paths, indent=4)}.") | |
| return model | |