update web_config
This commit is contained in:
@@ -1,31 +1,43 @@
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime
|
||||
from json import JSONDecodeError
|
||||
|
||||
import gradio as gr
|
||||
from loguru import logger
|
||||
|
||||
from iopaint.const import *
|
||||
|
||||
_config_file = None
|
||||
|
||||
_config_file: Path = None
|
||||
|
||||
|
||||
class WebConfig(ApiConfig):
|
||||
model_dir: str = DEFAULT_MODEL_DIR
|
||||
|
||||
|
||||
def load_config(p: Path) -> WebConfig:
|
||||
if p.exists():
|
||||
with open(p, "r", encoding="utf-8") as f:
|
||||
try:
|
||||
return WebConfig(**{**default_configs, **json.load(f)})
|
||||
except JSONDecodeError:
|
||||
print(f"Load config file failed, using default configs")
|
||||
return WebConfig(**default_configs)
|
||||
else:
|
||||
return WebConfig(**default_configs)
|
||||
|
||||
|
||||
def save_config(
|
||||
host,
|
||||
port,
|
||||
model,
|
||||
sd_local_model_path,
|
||||
enable_controlnet,
|
||||
controlnet_method,
|
||||
device,
|
||||
gui,
|
||||
no_gui_auto_close,
|
||||
no_half,
|
||||
cpu_offload,
|
||||
disable_nsfw,
|
||||
sd_cpu_textencoder,
|
||||
local_files_only,
|
||||
model_dir,
|
||||
no_half,
|
||||
low_mem,
|
||||
cpu_offload,
|
||||
disable_nsfw_checker,
|
||||
local_files_only,
|
||||
cpu_textencoder,
|
||||
device,
|
||||
input,
|
||||
output_dir,
|
||||
quality,
|
||||
@@ -41,33 +53,29 @@ def save_config(
|
||||
gfpgan_device,
|
||||
enable_restoreformer,
|
||||
restoreformer_device,
|
||||
enable_gif,
|
||||
):
|
||||
config = InpaintRequest(**locals())
|
||||
config = WebConfig(**locals())
|
||||
if str(config.input) == ".":
|
||||
config.input = None
|
||||
if str(config.output_dir) == ".":
|
||||
config.output_dir = None
|
||||
|
||||
print(config)
|
||||
if config.input and not os.path.exists(config.input):
|
||||
return "[Error] Input file or directory does not exist"
|
||||
|
||||
current_time = datetime.now().strftime("%H:%M:%S")
|
||||
msg = f"[{current_time}] Successful save config to: {os.path.abspath(_config_file)}"
|
||||
msg = f"[{current_time}] Successful save config to: {str(_config_file.absolute())}"
|
||||
logger.info(msg)
|
||||
try:
|
||||
with open(_config_file, "w", encoding="utf-8") as f:
|
||||
json.dump(config.dict(), f, indent=4, ensure_ascii=False)
|
||||
f.write(config.model_dump_json(indent=4))
|
||||
except Exception as e:
|
||||
return f"Save failed: {str(e)}"
|
||||
return f"Save configure file failed: {str(e)}"
|
||||
return msg
|
||||
|
||||
|
||||
def close_server(*args):
|
||||
# TODO: make close both browser and server works
|
||||
import os, signal
|
||||
|
||||
pid = os.getpid()
|
||||
os.kill(pid, signal.SIGUSR1)
|
||||
|
||||
|
||||
def main(config_file: str):
|
||||
def main(config_file: Path):
|
||||
global _config_file
|
||||
_config_file = config_file
|
||||
|
||||
@@ -75,7 +83,9 @@ def main(config_file: str):
|
||||
|
||||
with gr.Blocks() as demo:
|
||||
with gr.Row():
|
||||
with gr.Column(scale=1):
|
||||
with gr.Column():
|
||||
gr.Textbox(config_file, label="Config file", interactive=False)
|
||||
with gr.Column():
|
||||
save_btn = gr.Button(value="Save configurations")
|
||||
message = gr.HTML()
|
||||
|
||||
@@ -86,10 +96,12 @@ def main(config_file: str):
|
||||
port = gr.Number(init_config.port, label="Port", precision=0)
|
||||
|
||||
model = gr.Radio(
|
||||
AVAILABLE_MODELS, label="Model", value=init_config.model
|
||||
AVAILABLE_MODELS + DIFFUSION_MODELS,
|
||||
label="Models (https://www.iopaint.com/models)",
|
||||
value=init_config.model,
|
||||
)
|
||||
device = gr.Radio(
|
||||
AVAILABLE_DEVICES, label="Device", value=init_config.device
|
||||
Device.values(), label="Device", value=init_config.device
|
||||
)
|
||||
quality = gr.Slider(
|
||||
value=95,
|
||||
@@ -99,8 +111,20 @@ def main(config_file: str):
|
||||
step=1,
|
||||
)
|
||||
|
||||
with gr.Column():
|
||||
gui = gr.Checkbox(init_config.gui, label=f"{GUI_HELP}")
|
||||
no_half = gr.Checkbox(init_config.no_half, label=f"{NO_HALF_HELP}")
|
||||
cpu_offload = gr.Checkbox(
|
||||
init_config.cpu_offload, label=f"{CPU_OFFLOAD_HELP}"
|
||||
)
|
||||
low_mem = gr.Checkbox(init_config.low_mem, label=f"{LOW_MEM_HELP}")
|
||||
cpu_textencoder = gr.Checkbox(
|
||||
init_config.cpu_textencoder, label=f"{CPU_TEXTENCODER_HELP}"
|
||||
)
|
||||
disable_nsfw_checker = gr.Checkbox(
|
||||
init_config.disable_nsfw_checker, label=f"{DISABLE_NSFW_HELP}"
|
||||
)
|
||||
local_files_only = gr.Checkbox(
|
||||
init_config.local_files_only, label=f"{LOCAL_FILES_ONLY_HELP}"
|
||||
)
|
||||
|
||||
with gr.Column():
|
||||
model_dir = gr.Textbox(
|
||||
@@ -116,19 +140,20 @@ def main(config_file: str):
|
||||
)
|
||||
|
||||
with gr.Tab("Plugins"):
|
||||
enable_interactive_seg = gr.Checkbox(
|
||||
init_config.enable_interactive_seg, label=INTERACTIVE_SEG_HELP
|
||||
)
|
||||
interactive_seg_model = gr.Radio(
|
||||
AVAILABLE_INTERACTIVE_SEG_MODELS,
|
||||
label=f"Segment Anything models. {INTERACTIVE_SEG_MODEL_HELP}",
|
||||
value=init_config.interactive_seg_model,
|
||||
)
|
||||
interactive_seg_device = gr.Radio(
|
||||
AVAILABLE_INTERACTIVE_SEG_DEVICES,
|
||||
label="Segment Anything Device",
|
||||
value=init_config.interactive_seg_device,
|
||||
)
|
||||
with gr.Row():
|
||||
enable_interactive_seg = gr.Checkbox(
|
||||
init_config.enable_interactive_seg, label=INTERACTIVE_SEG_HELP
|
||||
)
|
||||
interactive_seg_model = gr.Radio(
|
||||
InteractiveSegModel.values(),
|
||||
label=f"Segment Anything models. {INTERACTIVE_SEG_MODEL_HELP}",
|
||||
value=init_config.interactive_seg_model,
|
||||
)
|
||||
interactive_seg_device = gr.Radio(
|
||||
Device.values(),
|
||||
label="Segment Anything Device",
|
||||
value=init_config.interactive_seg_device,
|
||||
)
|
||||
with gr.Row():
|
||||
enable_remove_bg = gr.Checkbox(
|
||||
init_config.enable_remove_bg, label=REMOVE_BG_HELP
|
||||
@@ -143,12 +168,12 @@ def main(config_file: str):
|
||||
init_config.enable_realesrgan, label=REALESRGAN_HELP
|
||||
)
|
||||
realesrgan_device = gr.Radio(
|
||||
REALESRGAN_AVAILABLE_DEVICES,
|
||||
Device.values(),
|
||||
label="RealESRGAN Device",
|
||||
value=init_config.realesrgan_device,
|
||||
)
|
||||
realesrgan_model = gr.Radio(
|
||||
RealESRGANModelNameList,
|
||||
RealESRGANModel.values(),
|
||||
label="RealESRGAN model",
|
||||
value=init_config.realesrgan_model,
|
||||
)
|
||||
@@ -157,7 +182,7 @@ def main(config_file: str):
|
||||
init_config.enable_gfpgan, label=GFPGAN_HELP
|
||||
)
|
||||
gfpgan_device = gr.Radio(
|
||||
GFPGAN_AVAILABLE_DEVICES,
|
||||
Device.values(),
|
||||
label="GFPGAN Device",
|
||||
value=init_config.gfpgan_device,
|
||||
)
|
||||
@@ -166,37 +191,10 @@ def main(config_file: str):
|
||||
init_config.enable_restoreformer, label=RESTOREFORMER_HELP
|
||||
)
|
||||
restoreformer_device = gr.Radio(
|
||||
RESTOREFORMER_AVAILABLE_DEVICES,
|
||||
Device.values(),
|
||||
label="RestoreFormer Device",
|
||||
value=init_config.restoreformer_device,
|
||||
)
|
||||
enable_gif = gr.Checkbox(init_config.enable_gif, label=GIF_HELP)
|
||||
|
||||
with gr.Tab("Diffusion Model"):
|
||||
sd_local_model_path = gr.Textbox(
|
||||
init_config.sd_local_model_path, label=f"{SD_LOCAL_MODEL_HELP}"
|
||||
)
|
||||
enable_controlnet = gr.Checkbox(
|
||||
init_config.enable_controlnet, label=f"{SD_CONTROLNET_HELP}"
|
||||
)
|
||||
controlnet_method = gr.Radio(
|
||||
SD_CONTROLNET_CHOICES,
|
||||
label="ControlNet method",
|
||||
value=init_config.controlnet_method,
|
||||
)
|
||||
no_half = gr.Checkbox(init_config.no_half, label=f"{NO_HALF_HELP}")
|
||||
cpu_offload = gr.Checkbox(
|
||||
init_config.cpu_offload, label=f"{CPU_OFFLOAD_HELP}"
|
||||
)
|
||||
sd_cpu_textencoder = gr.Checkbox(
|
||||
init_config.sd_cpu_textencoder, label=f"{CPU_TEXTENCODER_HELP}"
|
||||
)
|
||||
disable_nsfw = gr.Checkbox(
|
||||
init_config.disable_nsfw, label=f"{DISABLE_NSFW_HELP}"
|
||||
)
|
||||
local_files_only = gr.Checkbox(
|
||||
init_config.local_files_only, label=f"{LOCAL_FILES_ONLY_HELP}"
|
||||
)
|
||||
|
||||
save_btn.click(
|
||||
save_config,
|
||||
@@ -204,18 +202,14 @@ def main(config_file: str):
|
||||
host,
|
||||
port,
|
||||
model,
|
||||
sd_local_model_path,
|
||||
enable_controlnet,
|
||||
controlnet_method,
|
||||
device,
|
||||
gui,
|
||||
no_gui_auto_close,
|
||||
no_half,
|
||||
cpu_offload,
|
||||
disable_nsfw,
|
||||
sd_cpu_textencoder,
|
||||
local_files_only,
|
||||
model_dir,
|
||||
no_half,
|
||||
low_mem,
|
||||
cpu_offload,
|
||||
disable_nsfw_checker,
|
||||
local_files_only,
|
||||
cpu_textencoder,
|
||||
device,
|
||||
input,
|
||||
output_dir,
|
||||
quality,
|
||||
@@ -231,7 +225,6 @@ def main(config_file: str):
|
||||
gfpgan_device,
|
||||
enable_restoreformer,
|
||||
restoreformer_device,
|
||||
enable_gif,
|
||||
],
|
||||
message,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user