From 29a2933e23e68900bbae741a98439d0c9d6f26f6 Mon Sep 17 00:00:00 2001 From: EyeDeck Date: Sun, 11 Sep 2022 16:00:42 -0400 Subject: [PATCH] Add --hide-ui-dir-config command line flag Adds `--hide-ui-dir-config` flag to disable editing directory configs from the web UI. This can be set to prevent users from setting the directory to somewhere they shouldn't, for public (or semi-public) interfaces. Directories are still read from config.json, so the server admin can still set them in the web UI and then relaunch with the hide flag, or edit the config manually. Also: - fix OptionInfo `component_args` keyword argument not being read if `component` isn't also set - ensure that hidden settings aren't still read from the web UI (otherwise they could still be changed by tampering with the interface) --- modules/shared.py | 18 ++++++++++-------- modules/ui.py | 17 +++++++++++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/modules/shared.py b/modules/shared.py index 9eeb64e3..0557cfe3 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -42,6 +42,7 @@ parser.add_argument("--listen", action='store_true', help="launch gradio with 0. parser.add_argument("--port", type=int, help="launch gradio with given server port, you need root/admin rights for ports < 1024, defaults to 7860 if available", default=None) parser.add_argument("--show-negative-prompt", action='store_true', help="does not do anything", default=False) parser.add_argument("--ui-config-file", type=str, help="filename to use for ui configuration", default=os.path.join(script_path, 'ui-config.json')) +parser.add_argument("--hide-ui-dir-config", action='store_true', help="hide directory configuration from webui", default=False) cmd_opts = parser.parse_args() @@ -91,18 +92,19 @@ class Options: self.component_args = component_args data = None + hide_dirs = {"visible": False} if cmd_opts.hide_ui_dir_config else None data_labels = { - "outdir_samples": OptionInfo("", "Output directory for images; if empty, defaults to two directories below"), - "outdir_txt2img_samples": OptionInfo("outputs/txt2img-images", 'Output directory for txt2img images'), - "outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images'), - "outdir_extras_samples": OptionInfo("outputs/extras-images", 'Output directory for images from extras tab'), - "outdir_grids": OptionInfo("", "Output directory for grids; if empty, defaults to two directories below"), - "outdir_txt2img_grids": OptionInfo("outputs/txt2img-grids", 'Output directory for txt2img grids'), - "outdir_img2img_grids": OptionInfo("outputs/img2img-grids", 'Output directory for img2img grids'), + "outdir_samples": OptionInfo("", "Output directory for images; if empty, defaults to two directories below", component_args=hide_dirs), + "outdir_txt2img_samples": OptionInfo("outputs/txt2img-images", 'Output directory for txt2img images', component_args=hide_dirs), + "outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images', component_args=hide_dirs), + "outdir_extras_samples": OptionInfo("outputs/extras-images", 'Output directory for images from extras tab', component_args=hide_dirs), + "outdir_grids": OptionInfo("", "Output directory for grids; if empty, defaults to two directories below", component_args=hide_dirs), + "outdir_txt2img_grids": OptionInfo("outputs/txt2img-grids", 'Output directory for txt2img grids', component_args=hide_dirs), + "outdir_img2img_grids": OptionInfo("outputs/img2img-grids", 'Output directory for img2img grids', component_args=hide_dirs), "save_to_dirs": OptionInfo(False, "When writing images, create a directory with name derived from the prompt"), "grid_save_to_dirs": OptionInfo(False, "When writing grids, create a directory with name derived from the prompt"), "save_to_dirs_prompt_len": OptionInfo(10, "When using above, how many words from prompt to put into directory name", gr.Slider, {"minimum": 1, "maximum": 32, "step": 1}), - "outdir_save": OptionInfo("log/images", "Directory for saving images using the Save button"), + "outdir_save": OptionInfo("log/images", "Directory for saving images using the Save button", component_args=hide_dirs), "samples_save": OptionInfo(True, "Save indiviual samples"), "samples_format": OptionInfo('png', 'File format for individual samples'), "grid_save": OptionInfo(True, "Save image grids"), diff --git a/modules/ui.py b/modules/ui.py index b9af2c86..3b7eb9bb 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -661,19 +661,20 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): info = opts.data_labels[key] t = type(info.default) + args = info.component_args() if callable(info.component_args) else info.component_args + if info.component is not None: - args = info.component_args() if callable(info.component_args) else info.component_args - item = info.component(label=info.label, value=fun, **(args or {})) + comp = info.component elif t == str: - item = gr.Textbox(label=info.label, value=fun, lines=1) + comp = gr.Textbox elif t == int: - item = gr.Number(label=info.label, value=fun) + comp = gr.Number elif t == bool: - item = gr.Checkbox(label=info.label, value=fun) + comp = gr.Checkbox else: raise Exception(f'bad options item type: {str(t)} for key {key}') - return item + return comp(label=info.label, value=fun, **(args or {})) components = [] keys = list(opts.data_labels.keys()) @@ -684,6 +685,10 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): up = [] for key, value, comp in zip(opts.data_labels.keys(), args, components): + comp_args = opts.data_labels[key].component_args + if comp_args and isinstance(comp_args, dict) and comp_args.get('visible') is False: + continue + opts.data[key] = value up.append(comp.update(value=value))