File size: 2,745 Bytes
34097e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from modules import extra_networks, shared
import lycoris


default_args = [
    ('te', 1.0, float),
    ('unet', None, float),
    ('dyn', None, int)
]


def parse_args(params:list):
    arg_list = []
    kwarg_list = {}
    
    for i in params:
        if '=' in str(i):
            k, v = i.split('=', 1)
            kwarg_list[k] = v
        else:
            arg_list.append(i)
    
    args = []
    for name, default, type in default_args:
        if name in kwarg_list:
            x = kwarg_list[name]
        elif arg_list:
            x = arg_list.pop(0)
        else:
            x = default
        
        if x == 'default':
            x = default
        elif x is not None:
            x = type(x)
        
        args.append(x)
    
    return args


class ExtraNetworkLyCORIS(extra_networks.ExtraNetwork):
    def __init__(self):
        super().__init__('lyco')
        self.cache = ()

    def activate(self, p, params_list):
        additional = shared.opts.sd_lyco

        if additional != "" and additional in lycoris.available_lycos and len([x for x in params_list if x.items[0] == additional]) == 0:
            p.all_prompts = [
                x + 
                f"<lyco:{additional}:{shared.opts.extra_networks_default_multiplier}>" 
                for x in p.all_prompts
            ]
            params_list.append(
                extra_networks.ExtraNetworkParams(
                    items=[additional, shared.opts.extra_networks_default_multiplier])
                )

        names = []
        te_multipliers = []
        unet_multipliers = []
        dyn_dims = []
        for params in params_list:
            assert len(params.items) > 0

            names.append(params.items[0])
            te, unet, dyn_dim = parse_args(params.items[1:])
            if unet is None:
                unet = te
            te_multipliers.append(te)
            unet_multipliers.append(unet)
            dyn_dims.append(dyn_dim)
        
        all_lycos = tuple(
            (name, te, unet, dyn)
            for name, te, unet, dyn in zip(names, te_multipliers, unet_multipliers, dyn_dims)
        )
        
        if all_lycos != self.cache:
            for name, te, unet, dyn in all_lycos:
                print(
                    "========================================\n"
                    f"Apply LyCORIS model: {name}\n"
                    f"Text encoder weight: {te}\n"
                    f"Unet weight: {unet}\n"
                    f"DyLoRA Dim: {dyn}"
                )
            print("========================================")
            self.cache = all_lycos
        lycoris.load_lycos(names, te_multipliers, unet_multipliers, dyn_dims)

    def deactivate(self, p):
        pass