webui.py 3.6 KB
Newer Older
1
import os
2
import threading
3

4
from modules import devices
5
from modules.paths import script_path
6
import signal
D
d8ahazard 已提交
7
import threading
D
d8ahazard 已提交
8
import modules.paths
D
d8ahazard 已提交
9
import modules.codeformer_model as codeformer
A
AUTOMATIC 已提交
10
import modules.esrgan_model as esrgan
D
d8ahazard 已提交
11
import modules.bsrgan_model as bsrgan
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 17 18 19 20
import modules.ldsr_model as ldsr
import modules.lowvram
import modules.realesrgan_model as realesrgan
import modules.scripts
import modules.sd_hijack
21
import modules.sd_models
D
d8ahazard 已提交
22 23 24 25
import modules.shared as shared
import modules.swinir_model as swinir
import modules.txt2img
import modules.ui
D
d8ahazard 已提交
26
from modules import modelloader
D
d8ahazard 已提交
27 28
from modules.paths import script_path
from modules.shared import cmd_opts
29

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

38

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

44
        return res
A
AUTOMATIC 已提交
45

46
    return f
47

A
AUTOMATIC 已提交
48

49 50
def wrap_gradio_gpu_call(func):
    def f(*args, **kwargs):
51 52
        devices.torch_gc()

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

62 63
        with queue_lock:
            res = func(*args, **kwargs)
A
AUTOMATIC 已提交
64

65
        shared.state.job = ""
A
AUTOMATIC 已提交
66
        shared.state.job_count = 0
A
AUTOMATIC 已提交
67

68 69
        devices.torch_gc()

70
        return res
A
AUTOMATIC 已提交
71

72
    return modules.ui.wrap_gradio_call(f)
A
AUTOMATIC 已提交
73

74

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

77 78
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)))
79

80 81

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

A
AUTOMATIC 已提交
87
    signal.signal(signal.SIGINT, sigint_handler)
88

D
DepFA 已提交
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
    while 1:

      demo = modules.ui.create_ui(
          txt2img=wrap_gradio_gpu_call(modules.txt2img.txt2img),
          img2img=wrap_gradio_gpu_call(modules.img2img.img2img),
          run_extras=wrap_gradio_gpu_call(modules.extras.run_extras),
          run_pnginfo=modules.extras.run_pnginfo,
          run_modelmerger=modules.extras.run_modelmerger
      )


      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)
        if getattr(demo,'do_restart',False):
          time.sleep(0.5)
          demo.close()
          time.sleep(0.5)
          break

      print('Reloading Scripts')
      modules.scripts.reload_scripts(os.path.join(script_path, "scripts"))
      print('Restarting Gradio')
121

A
AUTOMATIC 已提交
122

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