File size: 1,011 Bytes
84f584e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import yaml

from huggingface_hub import hf_hub_download

from src.models.sdxl.adapter.ip_adapter import (
    SDXLModelWithIPAdapter,
    SDXLModelWithIPAdapterConfig,
)


def _load_config(config_path: str, model_path: str, adapter_path: str):
    with open(config_path, "r") as f:
        config = yaml.safe_load(f)

    config = SDXLModelWithIPAdapterConfig(**config)
    config.checkpoint_path = model_path
    config.adapter.checkpoint_weight = adapter_path

    return config


def load_ip_adapter_model(
    model_path: str,
    config_path: str,
    adapter_path: str,
):
    config = _load_config(config_path, model_path, adapter_path)
    model = SDXLModelWithIPAdapter.from_checkpoint(config)

    return model


def get_file_path(repo_id: str, filename: str, revision: str = "main") -> str:
    if os.path.exists(filename):
        return filename

    file_path = hf_hub_download(
        repo_id=repo_id,
        filename=filename,
        revision=revision,
    )

    return file_path