File size: 7,794 Bytes
0163a2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import ast
import importlib
import os
import subprocess
import sys

import gradio as gr

import scripts.shared as shared
from scripts.shared import ROOT_DIR

python = sys.executable


def path_to_module(filepath):
    return (
        os.path.relpath(filepath, ROOT_DIR).replace(os.path.sep, ".").replace(".py", "")
    )


def which(program):
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, _ = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None


def literal_eval(v, module=None):
    if v == "str":
        return str
    elif v == "int":
        return int
    elif v == "float":
        return float
    elif v == list:
        return list
    else:
        if module:
            try:
                m = importlib.import_module(module)
                if hasattr(m, v):
                    return getattr(m, v)
            except:
                ()

        return ast.literal_eval(v)


def compile_arg_parser(txt, module_path=None):
    in_parser = False
    parsers = {}
    args = []
    arg = ""
    in_list = False
    in_str = None

    def compile(arg):
        arg = arg.strip()
        matches = arg.split("=")

        if len(matches) > 1:
            k = "".join(matches[:1])
            v = literal_eval("".join(matches[1:]), module_path)
            return (k, v)
        else:
            return literal_eval(arg, module_path)

    for line in txt.split("\n"):
        line = line.split("#")[0]

        if "parser.add_argument(" in line:
            in_parser = True
            line = line.replace("parser.add_argument(", "")

        if not in_parser:
            continue

        for c in line:

            if in_str is None and c == ")":
                if arg.strip():
                    args.append(compile(arg))
                in_parser = False
                [dest, *others] = args
                parsers[dest] = {"dest": dest.replace("--", ""), **dict(others)}
                arg = ""
                args = []
                break

            if c == "[":
                in_list = True
            elif c == "]":
                in_list = False
            if c == '"' or c == "'":
                if in_str is not None and in_str == c:
                    in_str = None
                elif in_str is None:
                    in_str = c

            if c == "," and not in_list and in_str is None:
                args.append(compile(arg))
                arg = ""
                continue

            arg += c

    if arg.strip():
        args.append(compile(arg))
    return parsers


def load_args_template(*filename):
    repo_dir = os.path.join(ROOT_DIR, "kohya_ss")
    filepath = os.path.join(repo_dir, *filename)
    with open(filepath, mode="r", encoding="utf-8_sig") as f:
        lines = f.readlines()
        add = False
        txt = ""
        for line in lines:
            if add == True:
                txt += line
            if "def setup_parser()" in line:
                add = True
                continue
    return compile_arg_parser(txt, path_to_module(filepath)), filepath


def check_key(d, k):
    return k in d and d[k] is not None


def get_arg_type(d):
    if check_key(d, "choices"):
        return list
    if check_key(d, "type"):
        return d["type"]
    if check_key(d, "action") and (
        d["action"] == "store_true" or d["action"] == "store_false"
    ):
        return bool
    if check_key(d, "const") and type(d["const"]) == bool:
        return bool
    return str


def options_to_gradio(options, out, overrides={}):
    for _, item in options.items():
        item = item.__dict__ if hasattr(item, "__dict__") else item
        key = item["dest"]
        if key == "help":
            continue
        override = overrides[key] if key in overrides else {}
        component = None

        help = item["help"] if "help" in item else ""
        id = f"kohya_sd_webui__{shared.current_tab.replace('.', '_')}_{key}"
        type = override["type"] if "type" in override else get_arg_type(item)
        if type == list:
            choices = [
                c if c is not None else "None"
                for c in (
                    override["choices"] if "choices" in override else item["choices"]
                )
            ]
            component = gr.Radio(
                choices=choices,
                value=item["default"] if check_key(item, "default") else choices[0],
                label=key,
                elem_id=id,
                interactive=True,
            )
        elif type == bool:
            component = gr.Checkbox(
                value=item["default"] if check_key(item, "default") else False,
                label=key,
                elem_id=id,
                interactive=True,
            )
        else:
            component = gr.Textbox(
                value=item["default"] if check_key(item, "default") else "",
                label=key,
                elem_id=id,
                interactive=True,
            ).style()

        shared.help_title_map[id] = help
        out[key] = component


def args_to_gradio(args, out, overrides={}):
    options_to_gradio(args.__dict__["_option_string_actions"], out, overrides)


def gradio_to_args(arguments, options, args, strarg=False):
    def find_arg(key):
        for k, arg in arguments.items():
            arg = arg.__dict__ if hasattr(arg, "__dict__") else arg
            if arg["dest"] == key:
                return k, arg
        return None, None

    def get_value(key):
        item = args[options[key]]
        raw_key, arg = find_arg(key)
        arg_type = get_arg_type(arg)
        multiple = "nargs" in arg and arg["nargs"] == "*"

        def set_type(x):
            if x is None or x == "None":
                return None
            elif arg_type is None:
                return x
            elif arg_type == list:
                return x
            return arg_type(x)

        if multiple and item is None or item == "":
            return raw_key, None

        return raw_key, (
            [set_type(x) for x in item.split(" ")] if multiple else set_type(item)
        )

    if strarg:
        main = []
        optional = {}

        for k in options:
            key, v = get_value(k)
            if key.startswith("--"):
                key = k.replace("--", "")
                optional[key] = v
            else:
                main.append(v)

        main = [x for x in main if x is not None]

        return main, optional
    else:
        result = {}
        for k in options:
            _, v = get_value(k)
            result[k] = v
        return result


def make_args(d):
    arguments = []
    for k, v in d.items():
        if type(v) == bool:
            arguments.append(f"--{k}" if v else "")
        elif type(v) == list and len(v) > 0:
            arguments.extend([f"--{k}", *v])
        elif type(v) == str and v:
            arguments.extend([f"--{k}", f"{v}"])
        elif v:
            arguments.extend([f"--{k}", f"{v}"])
    return arguments


def run_python(script, templates, options, args):
    main, optional = gradio_to_args(templates, options, args, strarg=True)
    args = [x for x in [*main, *make_args(optional)] if x]
    proc_args = [python, "-u", script, *args]
    print("Start process: ", " ".join(proc_args))

    ps = subprocess.Popen(
        proc_args,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=os.path.join(ROOT_DIR, "kohya_ss"),
    )
    return ps