webui.py 3.5 KB
Newer Older
1
import os
2
import threading
D
DepFA 已提交
3
import time
D
DepFA 已提交
4
import importlib
5
import signal
D
d8ahazard 已提交
6
import threading
D
d8ahazard 已提交
7

8 9 10
from modules.paths import script_path

from modules import devices, sd_samplers
D
d8ahazard 已提交
11
import modules.codeformer_model as codeformer
A
AUTOMATIC 已提交
12
import modules.extras
D
d8ahazard 已提交
13 14
import modules.face_restoration
import modules.gfpgan_model as gfpgan
15
import modules.img2img
D
d8ahazard 已提交
16

D
d8ahazard 已提交
17
import modules.lowvram
D
d8ahazard 已提交
18
import modules.paths
D
d8ahazard 已提交
19 20
import modules.scripts
import modules.sd_hijack
21
import modules.sd_models
D
d8ahazard 已提交
22 23
import modules.shared as shared
import modules.txt2img
D
d8ahazard 已提交
24

D
d8ahazard 已提交
25
import modules.ui
D
d8ahazard 已提交
26
from modules import devices
D
d8ahazard 已提交
27
from modules import modelloader
D
d8ahazard 已提交
28 29
from modules.paths import script_path
from modules.shared import cmd_opts
30

D
d8ahazard 已提交
31
modelloader.cleanup_models()
32
modules.sd_models.setup_model()
D
d8ahazard 已提交
33 34
codeformer.setup_model(cmd_opts.codeformer_models_path)
gfpgan.setup_model(cmd_opts.gfpgan_models_path)
A
AUTOMATIC 已提交
35
shared.face_restorers.append(modules.face_restoration.FaceRestoration())
D
d8ahazard 已提交
36
modelloader.load_upscalers()
37
queue_lock = threading.Lock()
A
AUTOMATIC 已提交
38

39

40 41 42 43
def wrap_queued_call(func):
    def f(*args, **kwargs):
        with queue_lock:
            res = func(*args, **kwargs)
A
first  
AUTOMATIC 已提交
44

45
        return res
A
AUTOMATIC 已提交
46

47
    return f
48

A
AUTOMATIC 已提交
49

50
def wrap_gradio_gpu_call(func, extra_outputs=None):
51
    def f(*args, **kwargs):
52 53
        devices.torch_gc()

A
AUTOMATIC 已提交
54
        shared.state.sampling_step = 0
A
AUTOMATIC 已提交
55
        shared.state.job_count = -1
A
AUTOMATIC 已提交
56
        shared.state.job_no = 0
E
Eyrie 已提交
57
        shared.state.job_timestamp = shared.state.get_job_timestamp()
A
AUTOMATIC 已提交
58 59
        shared.state.current_latent = None
        shared.state.current_image = None
60
        shared.state.current_image_sampling_step = 0
61
        shared.state.skipped = False
62
        shared.state.interrupted = False
63
        shared.state.textinfo = None
A
AUTOMATIC 已提交
64

65 66
        with queue_lock:
            res = func(*args, **kwargs)
A
AUTOMATIC 已提交
67

68
        shared.state.job = ""
A
AUTOMATIC 已提交
69
        shared.state.job_count = 0
A
AUTOMATIC 已提交
70

71 72
        devices.torch_gc()

73
        return res
A
AUTOMATIC 已提交
74

75
    return modules.ui.wrap_gradio_call(f, extra_outputs=extra_outputs)
A
AUTOMATIC 已提交
76

77

78
modules.scripts.load_scripts(os.path.join(script_path, "scripts"))
A
AUTOMATIC 已提交
79

80 81
shared.sd_model = modules.sd_models.load_model()
shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: modules.sd_models.reload_model_weights(shared.sd_model)))
82

83 84

def webui():
A
AUTOMATIC 已提交
85 86
    # make the program just exit at ctrl+c without waiting for anything
    def sigint_handler(sig, frame):
A
AUTOMATIC 已提交
87
        print(f'Interrupted with signal {sig} in {frame}')
A
AUTOMATIC 已提交
88
        os._exit(0)
A
first  
AUTOMATIC 已提交
89

A
AUTOMATIC 已提交
90
    signal.signal(signal.SIGINT, sigint_handler)
91

D
DepFA 已提交
92 93
    while 1:

94 95
        demo = modules.ui.create_ui(wrap_gradio_gpu_call=wrap_gradio_gpu_call)
        
D
DepFA 已提交
96 97 98 99 100 101 102 103 104 105 106 107
        demo.launch(
            share=cmd_opts.share,
            server_name="0.0.0.0" if cmd_opts.listen else None,
            server_port=cmd_opts.port,
            debug=cmd_opts.gradio_debug,
            auth=[tuple(cred.split(':')) for cred in cmd_opts.gradio_auth.strip('"').split(',')] if cmd_opts.gradio_auth else None,
            inbrowser=cmd_opts.autolaunch,
            prevent_thread_lock=True
        )

        while 1:
            time.sleep(0.5)
A
hello  
AUTOMATIC 已提交
108 109 110 111 112
            if getattr(demo, 'do_restart', False):
                time.sleep(0.5)
                demo.close()
                time.sleep(0.5)
                break
D
DepFA 已提交
113

114 115
        sd_samplers.set_samplers()

D
DepFA 已提交
116 117 118 119 120
        print('Reloading Custom Scripts')
        modules.scripts.reload_scripts(os.path.join(script_path, "scripts"))
        print('Reloading modules: modules.ui')
        importlib.reload(modules.ui)
        print('Restarting Gradio')
121

A
AUTOMATIC 已提交
122 123


124 125
if __name__ == "__main__":
    webui()