webui.py 4.4 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
A
arcticfaded 已提交
7
from fastapi import FastAPI
D
DepFA 已提交
8 9
from fastapi.middleware.gzip import GZipMiddleware

10 11
from modules.paths import script_path

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

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

D
d8ahazard 已提交
27
import modules.ui
D
d8ahazard 已提交
28
from modules import devices
D
d8ahazard 已提交
29
from modules import modelloader
D
d8ahazard 已提交
30 31
from modules.paths import script_path
from modules.shared import cmd_opts
32
import modules.hypernetworks.hypernetwork
33

34
queue_lock = threading.Lock()
A
AUTOMATIC 已提交
35

36

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

42
        return res
A
AUTOMATIC 已提交
43

44
    return f
45

A
AUTOMATIC 已提交
46

47
def wrap_gradio_gpu_call(func, extra_outputs=None):
48
    def f(*args, **kwargs):
49 50

        shared.state.begin()
A
AUTOMATIC 已提交
51

52 53
        with queue_lock:
            res = func(*args, **kwargs)
A
AUTOMATIC 已提交
54

55
        shared.state.end()
56

57
        return res
A
AUTOMATIC 已提交
58

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

61

62
def initialize():
Y
yfszzx 已提交
63
    if cmd_opts.ui_debug_mode:
64 65
        shared.sd_upscalers = upscaler.UpscalerLanczos().scalers
        modules.scripts.load_scripts()
Y
yfszzx 已提交
66
        return
67

68 69 70 71 72 73
    modelloader.cleanup_models()
    modules.sd_models.setup_model()
    codeformer.setup_model(cmd_opts.codeformer_models_path)
    gfpgan.setup_model(cmd_opts.gfpgan_models_path)
    shared.face_restorers.append(modules.face_restoration.FaceRestoration())
    modelloader.load_upscalers()
A
AUTOMATIC 已提交
74

75 76
    modules.scripts.load_scripts()

77
    modules.sd_models.load_model()
78 79
    shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: modules.sd_models.reload_model_weights(shared.sd_model)))
    shared.opts.onchange("sd_hypernetwork", wrap_queued_call(lambda: modules.hypernetworks.hypernetwork.load_hypernetwork(shared.opts.sd_hypernetwork)))
A
AUTOMATIC 已提交
80
    shared.opts.onchange("sd_hypernetwork_strength", modules.hypernetworks.hypernetwork.apply_strength)
81

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
def create_api(app):
91
    from modules.api.api import Api
A
arcticfaded 已提交
92
    api = Api(app, queue_lock)
93 94 95
    return api

def wait_on_server(demo=None):
D
DepFA 已提交
96
    while 1:
97 98 99 100 101 102 103 104 105
        time.sleep(0.5)
        if demo and getattr(demo, 'do_restart', False):
            time.sleep(0.5)
            demo.close()
            time.sleep(0.5)
            break

def api_only():
    initialize()
D
DepFA 已提交
106

107 108 109 110 111
    app = FastAPI()
    app.add_middleware(GZipMiddleware, minimum_size=1000)
    api = create_api(app)

    api.launch(server_name="0.0.0.0" if cmd_opts.listen else "127.0.0.1", port=cmd_opts.port if cmd_opts.port else 7861)
112

113

114 115
def webui():
    launch_api = cmd_opts.api
116
    initialize()
117

118
    while 1:
119
        demo = modules.ui.create_ui(wrap_gradio_gpu_call=wrap_gradio_gpu_call)
120

121
        app, local_url, share_url = demo.launch(
D
DepFA 已提交
122 123 124 125 126 127 128 129
            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
        )
130 131
        # after initial launch, disable --autolaunch for subsequent restarts
        cmd_opts.autolaunch = False
132

A
AUTOMATIC 已提交
133
        app.add_middleware(GZipMiddleware, minimum_size=1000)
D
DepFA 已提交
134

135
        if (launch_api):
136
            create_api(app)
D
DepFA 已提交
137

138
        wait_on_server(demo)
139

140 141
        sd_samplers.set_samplers()

D
DepFA 已提交
142
        print('Reloading Custom Scripts')
143
        modules.scripts.reload_scripts()
D
DepFA 已提交
144 145
        print('Reloading modules: modules.ui')
        importlib.reload(modules.ui)
146 147
        print('Refreshing Model List')
        modules.sd_models.list_models()
D
DepFA 已提交
148
        print('Restarting Gradio')
149

A
AUTOMATIC 已提交
150

A
arcticfaded 已提交
151 152

task = []
153
if __name__ == "__main__":
A
arcticfaded 已提交
154
    if cmd_opts.nowebui:
155
        api_only()
156
    else:
157
        webui()