File size: 1,833 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
import glob
import importlib
import os
import sys

import gradio as gr

import scripts.shared as shared
from scripts.shared import ROOT_DIR
from scripts.utilities import path_to_module


def title(txt):
    gr.HTML(
        f'<h1 style="margin: 0.5rem 0; font-weight: bold; font-size: 1.5rem;">{txt}</h1>',
    )


def create_ui(css):
    PATHS = [
        os.path.join(ROOT_DIR, "kohya_ss", "library"),
        ROOT_DIR,
    ]
    sys.path.extend(PATHS)
    with gr.Blocks(css=css, analytics_enabled=False) as ui:
        with gr.Tabs(elem_id="kohya_sd_webui__root"):
            tabs_dir = os.path.join(ROOT_DIR, "scripts", "tabs")
            for category in os.listdir(tabs_dir):
                dir = os.path.join(tabs_dir, category)
                tabs = glob.glob(os.path.join(dir, "*.py"))
                sys.path.append(dir)
                if len(tabs) < 1:
                    continue
                with gr.TabItem(category):
                    for lib in tabs:
                        try:
                            module_path = path_to_module(lib)
                            module_name = module_path.replace(".", "_")

                            module = importlib.import_module(module_path)
                            shared.current_tab = module_name
                            shared.loaded_tabs.append(module_name)

                            with gr.TabItem(module.title()):
                                module.create_ui()
                        except Exception as e:
                            print(f"Failed to load {module_path}")
                            print(e)
                sys.path.remove(dir)
            with gr.TabItem("terminal"):
                gr.HTML('<div id="kohya_sd_webui__terminal_outputs"></div>')
    sys.path = [x for x in sys.path if x not in PATHS]
    return ui