webui.py 2.9 KB
Newer Older
1
import os
2
import signal
D
d8ahazard 已提交
3
import threading
D
d8ahazard 已提交
4

D
d8ahazard 已提交
5
import modules.codeformer_model as codeformer
A
AUTOMATIC 已提交
6
import modules.extras
D
d8ahazard 已提交
7 8
import modules.face_restoration
import modules.gfpgan_model as gfpgan
9
import modules.img2img
D
d8ahazard 已提交
10
import modules.lowvram
D
d8ahazard 已提交
11
import modules.paths
D
d8ahazard 已提交
12 13
import modules.scripts
import modules.sd_hijack
14
import modules.sd_models
D
d8ahazard 已提交
15 16 17
import modules.shared as shared
import modules.txt2img
import modules.ui
D
d8ahazard 已提交
18
from modules import devices
D
d8ahazard 已提交
19
from modules import modelloader
D
d8ahazard 已提交
20 21
from modules.paths import script_path
from modules.shared import cmd_opts
22

D
d8ahazard 已提交
23
modelloader.cleanup_models()
24
modules.sd_models.setup_model(cmd_opts.ckpt_dir)
D
d8ahazard 已提交
25 26
codeformer.setup_model(cmd_opts.codeformer_models_path)
gfpgan.setup_model(cmd_opts.gfpgan_models_path)
A
AUTOMATIC 已提交
27
shared.face_restorers.append(modules.face_restoration.FaceRestoration())
D
d8ahazard 已提交
28
modelloader.load_upscalers()
29
queue_lock = threading.Lock()
A
AUTOMATIC 已提交
30

31

32 33 34 35
def wrap_queued_call(func):
    def f(*args, **kwargs):
        with queue_lock:
            res = func(*args, **kwargs)
A
first  
AUTOMATIC 已提交
36

37
        return res
A
AUTOMATIC 已提交
38

39
    return f
40

A
AUTOMATIC 已提交
41

42 43
def wrap_gradio_gpu_call(func):
    def f(*args, **kwargs):
44 45
        devices.torch_gc()

A
AUTOMATIC 已提交
46
        shared.state.sampling_step = 0
A
AUTOMATIC 已提交
47
        shared.state.job_count = -1
A
AUTOMATIC 已提交
48
        shared.state.job_no = 0
E
Eyrie 已提交
49
        shared.state.job_timestamp = shared.state.get_job_timestamp()
A
AUTOMATIC 已提交
50 51
        shared.state.current_latent = None
        shared.state.current_image = None
52
        shared.state.current_image_sampling_step = 0
53
        shared.state.interrupted = False
A
AUTOMATIC 已提交
54

55 56
        with queue_lock:
            res = func(*args, **kwargs)
A
AUTOMATIC 已提交
57

58
        shared.state.job = ""
A
AUTOMATIC 已提交
59
        shared.state.job_count = 0
A
AUTOMATIC 已提交
60

61 62
        devices.torch_gc()

63
        return res
A
AUTOMATIC 已提交
64

65
    return modules.ui.wrap_gradio_call(f)
A
AUTOMATIC 已提交
66

67

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

70 71
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)))
72

73 74

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

A
AUTOMATIC 已提交
80
    signal.signal(signal.SIGINT, sigint_handler)
81

A
AUTOMATIC 已提交
82 83 84
    demo = modules.ui.create_ui(
        txt2img=wrap_gradio_gpu_call(modules.txt2img.txt2img),
        img2img=wrap_gradio_gpu_call(modules.img2img.img2img),
85
        run_extras=wrap_gradio_gpu_call(modules.extras.run_extras),
86 87
        run_pnginfo=modules.extras.run_pnginfo,
        run_modelmerger=modules.extras.run_modelmerger
A
AUTOMATIC 已提交
88
    )
89

90 91 92 93 94
    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,
A
AUTOMATIC 已提交
95
        auth=[tuple(cred.split(':')) for cred in cmd_opts.gradio_auth.strip('"').split(',')] if cmd_opts.gradio_auth else None,
96
        inbrowser=cmd_opts.autolaunch,
97
    )
98

A
AUTOMATIC 已提交
99

100 101
if __name__ == "__main__":
    webui()