diff --git a/modules/async_worker.py b/modules/async_worker.py index b2939d2be..f9e277dbf 100644 --- a/modules/async_worker.py +++ b/modules/async_worker.py @@ -9,7 +9,7 @@ class AsyncTask: def __init__(self, args): - from modules.flags import Performance, MetadataScheme, ip_list, controlnet_image_count, disabled + from modules.flags import Performance, MetadataScheme, ip_list, disabled from modules.util import get_enabled_loras from modules.config import default_max_lora_number import args_manager @@ -101,7 +101,7 @@ def __init__(self, args): args.pop()) if not args_manager.args.disable_metadata else MetadataScheme.FOOOCUS self.cn_tasks = {x: [] for x in ip_list} - for _ in range(controlnet_image_count): + for _ in range(modules.config.default_controlnet_image_count): cn_img = args.pop() cn_stop = args.pop() cn_weight = args.pop() diff --git a/modules/config.py b/modules/config.py index 5af9c58ad..7062c0056 100644 --- a/modules/config.py +++ b/modules/config.py @@ -403,12 +403,36 @@ def init_temp_path(path: str | None, default_path: str) -> str: validator=lambda x: x in Performance.values(), expected_type=str ) +default_image_prompt_checkbox = get_config_item_or_set_default( + key='default_image_prompt_checkbox', + default_value=False, + validator=lambda x: isinstance(x, bool), + expected_type=bool +) +default_enhance_checkbox = get_config_item_or_set_default( + key='default_enhance_checkbox', + default_value=False, + validator=lambda x: isinstance(x, bool), + expected_type=bool +) default_advanced_checkbox = get_config_item_or_set_default( key='default_advanced_checkbox', default_value=False, validator=lambda x: isinstance(x, bool), expected_type=bool ) +default_developer_debug_mode_checkbox = get_config_item_or_set_default( + key='default_developer_debug_mode_checkbox', + default_value=False, + validator=lambda x: isinstance(x, bool), + expected_type=bool +) +default_image_prompt_advanced_checkbox = get_config_item_or_set_default( + key='default_image_prompt_advanced_checkbox', + default_value=False, + validator=lambda x: isinstance(x, bool), + expected_type=bool +) default_max_image_number = get_config_item_or_set_default( key='default_max_image_number', default_value=32, @@ -469,6 +493,64 @@ def init_temp_path(path: str | None, default_path: str) -> str: validator=lambda x: x in modules.flags.inpaint_engine_versions, expected_type=str ) +default_selected_image_input_tab_id = get_config_item_or_set_default( + key='default_selected_image_input_tab_id', + default_value=modules.flags.default_input_image_tab, + validator=lambda x: x in modules.flags.input_image_tab_ids, + expected_type=str +) +default_uov_method = get_config_item_or_set_default( + key='default_uov_method', + default_value=modules.flags.disabled, + validator=lambda x: x in modules.flags.uov_list, + expected_type=str +) +default_controlnet_image_count = get_config_item_or_set_default( + key='default_controlnet_image_count', + default_value=4, + validator=lambda x: isinstance(x, int) and x > 0, + expected_type=int +) +default_ip_images = {} +default_ip_stop_ats = {} +default_ip_weights = {} +default_ip_types = {} + +for image_count in range(default_controlnet_image_count): + default_ip_images[image_count] = get_config_item_or_set_default( + key=f'default_ip_image_{image_count}', + default_value=None, + validator=lambda x: x is None or isinstance(x, str) and os.path.exists(x), + expected_type=str + ) + default_ip_types[image_count] = get_config_item_or_set_default( + key=f'default_ip_type_{image_count}', + default_value=modules.flags.default_ip, + validator=lambda x: x in modules.flags.ip_list, + expected_type=str + ) + + default_end, default_weight = modules.flags.default_parameters[default_ip_types[image_count]] + + default_ip_stop_ats[image_count] = get_config_item_or_set_default( + key=f'default_ip_stop_at_{image_count}', + default_value=default_end, + validator=lambda x: isinstance(x, float) and 0 <= x <= 1, + expected_type=float + ) + default_ip_weights[image_count] = get_config_item_or_set_default( + key=f'default_ip_weight_{image_count}', + default_value=default_weight, + validator=lambda x: isinstance(x, float) and 0 <= x <= 2, + expected_type=float + ) + +default_inpaint_advanced_masking_checkbox = get_config_item_or_set_default( + key='default_inpaint_advanced_masking_checkbox', + default_value=False, + validator=lambda x: isinstance(x, bool), + expected_type=bool +) default_inpaint_method = get_config_item_or_set_default( key='default_inpaint_method', default_value=modules.flags.inpaint_option_default, @@ -526,12 +608,6 @@ def init_temp_path(path: str | None, default_path: str) -> str: validator=lambda x: isinstance(x, int) and 1 <= x <= 5, expected_type=int ) -default_enhance_checkbox = get_config_item_or_set_default( - key='default_enhance_checkbox', - default_value=False, - validator=lambda x: isinstance(x, bool), - expected_type=bool -) default_enhance_uov_method = get_config_item_or_set_default( key='default_enhance_uov_method', default_value=modules.flags.disabled, @@ -590,6 +666,13 @@ def init_temp_path(path: str | None, default_path: str) -> str: example_inpaint_prompts = [[x] for x in example_inpaint_prompts] example_enhance_detection_prompts = [[x] for x in example_enhance_detection_prompts] +default_invert_mask_checkbox = get_config_item_or_set_default( + key='default_invert_mask_checkbox', + default_value=False, + validator=lambda x: isinstance(x, bool), + expected_type=bool +) + default_inpaint_mask_model = get_config_item_or_set_default( key='default_inpaint_mask_model', default_value='isnet-general-use', diff --git a/modules/flags.py b/modules/flags.py index 4b6cd7b59..4357cdf11 100644 --- a/modules/flags.py +++ b/modules/flags.py @@ -67,6 +67,9 @@ refiner_swap_method = 'joint' +default_input_image_tab = 'uov_tab' +input_image_tab_ids = ['uov_tab', 'ip_tab', 'inpaint_tab', 'describe_tab', 'enhance_tab', 'metadata_tab'] + cn_ip = "ImagePrompt" cn_ip_face = "FaceSwap" cn_canny = "PyraCanny" @@ -91,8 +94,8 @@ inpaint_option_modify = 'Modify Content (add objects, change background, etc.)' inpaint_options = [inpaint_option_default, inpaint_option_detail, inpaint_option_modify] -desc_type_photo = 'Photograph' -desc_type_anime = 'Art/Anime' +describe_type_photo = 'Photograph' +describe_type_anime = 'Art/Anime' sdxl_aspect_ratios = [ '704*1408', '704*1344', '768*1344', '768*1280', '832*1216', '832*1152', @@ -113,8 +116,6 @@ class MetadataScheme(Enum): (f'{MetadataScheme.A1111.value} (plain text)', MetadataScheme.A1111.value), ] -controlnet_image_count = 4 - class OutputFormat(Enum): PNG = 'png' diff --git a/webui.py b/webui.py index 3413a5dd3..5a2100e5c 100644 --- a/webui.py +++ b/webui.py @@ -200,19 +200,19 @@ def skip_clicked(currentTask): stop_button.click(stop_clicked, inputs=currentTask, outputs=currentTask, queue=False, show_progress=False, _js='cancelGenerateForever') skip_button.click(skip_clicked, inputs=currentTask, outputs=currentTask, queue=False, show_progress=False) with gr.Row(elem_classes='advanced_check_row'): - input_image_checkbox = gr.Checkbox(label='Input Image', value=False, container=False, elem_classes='min_check') + input_image_checkbox = gr.Checkbox(label='Input Image', value=modules.config.default_image_prompt_checkbox, container=False, elem_classes='min_check') enhance_checkbox = gr.Checkbox(label='Enhance', value=modules.config.default_enhance_checkbox, container=False, elem_classes='min_check') advanced_checkbox = gr.Checkbox(label='Advanced', value=modules.config.default_advanced_checkbox, container=False, elem_classes='min_check') - with gr.Row(visible=False) as image_input_panel: - with gr.Tabs(): - with gr.TabItem(label='Upscale or Variation') as uov_tab: + with gr.Row(visible=modules.config.default_image_prompt_checkbox) as image_input_panel: + with gr.Tabs(selected=modules.config.default_selected_image_input_tab_id): + with gr.Tab(label='Upscale or Variation', id='uov_tab') as uov_tab: with gr.Row(): with gr.Column(): uov_input_image = grh.Image(label='Image', source='upload', type='numpy', show_label=False) with gr.Column(): - uov_method = gr.Radio(label='Upscale or Variation:', choices=flags.uov_list, value=flags.disabled) + uov_method = gr.Radio(label='Upscale or Variation:', choices=flags.uov_list, value=modules.config.default_uov_method) gr.HTML('\U0001F4D4 Documentation') - with gr.TabItem(label='Image Prompt') as ip_tab: + with gr.Tab(label='Image Prompt', id='ip_tab') as ip_tab: with gr.Row(): ip_images = [] ip_types = [] @@ -220,30 +220,28 @@ def skip_clicked(currentTask): ip_weights = [] ip_ctrls = [] ip_ad_cols = [] - for _ in range(flags.controlnet_image_count): + for image_count in range(modules.config.default_controlnet_image_count): with gr.Column(): - ip_image = grh.Image(label='Image', source='upload', type='numpy', show_label=False, height=300) + ip_image = grh.Image(label='Image', source='upload', type='numpy', show_label=False, height=300, value=modules.config.default_ip_images[image_count]) ip_images.append(ip_image) ip_ctrls.append(ip_image) - with gr.Column(visible=False) as ad_col: + with gr.Column(visible=modules.config.default_image_prompt_advanced_checkbox) as ad_col: with gr.Row(): - default_end, default_weight = flags.default_parameters[flags.default_ip] - - ip_stop = gr.Slider(label='Stop At', minimum=0.0, maximum=1.0, step=0.001, value=default_end) + ip_stop = gr.Slider(label='Stop At', minimum=0.0, maximum=1.0, step=0.001, value=modules.config.default_ip_stop_ats[image_count]) ip_stops.append(ip_stop) ip_ctrls.append(ip_stop) - ip_weight = gr.Slider(label='Weight', minimum=0.0, maximum=2.0, step=0.001, value=default_weight) + ip_weight = gr.Slider(label='Weight', minimum=0.0, maximum=2.0, step=0.001, value=modules.config.default_ip_weights[image_count]) ip_weights.append(ip_weight) ip_ctrls.append(ip_weight) - ip_type = gr.Radio(label='Type', choices=flags.ip_list, value=flags.default_ip, container=False) + ip_type = gr.Radio(label='Type', choices=flags.ip_list, value=modules.config.default_ip_types[image_count], container=False) ip_types.append(ip_type) ip_ctrls.append(ip_type) ip_type.change(lambda x: flags.default_parameters[x], inputs=[ip_type], outputs=[ip_stop, ip_weight], queue=False, show_progress=False) ip_ad_cols.append(ad_col) - ip_advanced = gr.Checkbox(label='Advanced', value=False, container=False) + ip_advanced = gr.Checkbox(label='Advanced', value=modules.config.default_image_prompt_advanced_checkbox, container=False) gr.HTML('* \"Image Prompt\" is powered by Fooocus Image Mixture Engine (v1.0.1). \U0001F4D4 Documentation') def ip_advance_checked(x): @@ -256,11 +254,11 @@ def ip_advance_checked(x): outputs=ip_ad_cols + ip_types + ip_stops + ip_weights, queue=False, show_progress=False) - with gr.TabItem(label='Inpaint or Outpaint') as inpaint_tab: + with gr.Tab(label='Inpaint or Outpaint', id='inpaint_tab') as inpaint_tab: with gr.Row(): with gr.Column(): inpaint_input_image = grh.Image(label='Image', source='upload', type='numpy', tool='sketch', height=500, brush_color="#FFFFFF", elem_id='inpaint_canvas', show_label=False) - inpaint_advanced_masking_checkbox = gr.Checkbox(label='Enable Advanced Masking Features', value=False) + inpaint_advanced_masking_checkbox = gr.Checkbox(label='Enable Advanced Masking Features', value=modules.config.default_inpaint_advanced_masking_checkbox) inpaint_mode = gr.Dropdown(choices=modules.flags.inpaint_options, value=modules.config.default_inpaint_method, label='Method') inpaint_additional_prompt = gr.Textbox(placeholder="Describe what you want to inpaint.", elem_id='inpaint_additional_prompt', label='Inpaint Additional Prompt', visible=False) outpaint_selections = gr.CheckboxGroup(choices=['Left', 'Right', 'Top', 'Bottom'], value=[], label='Outpaint Direction') @@ -271,9 +269,9 @@ def ip_advance_checked(x): gr.HTML('* Powered by Fooocus Inpaint Engine \U0001F4D4 Documentation') example_inpaint_prompts.click(lambda x: x[0], inputs=example_inpaint_prompts, outputs=inpaint_additional_prompt, show_progress=False, queue=False) - with gr.Column(visible=False) as inpaint_mask_generation_col: + with gr.Column(visible=modules.config.default_inpaint_advanced_masking_checkbox) as inpaint_mask_generation_col: inpaint_mask_image = grh.Image(label='Mask Upload', source='upload', type='numpy', tool='sketch', height=500, brush_color="#FFFFFF", mask_opacity=1, elem_id='inpaint_mask_canvas') - invert_mask_checkbox = gr.Checkbox(label='Invert Mask When Generating', value=False) + invert_mask_checkbox = gr.Checkbox(label='Invert Mask When Generating', value=modules.config.default_invert_mask_checkbox) inpaint_mask_model = gr.Dropdown(label='Mask generation model', choices=flags.inpaint_mask_models, value=modules.config.default_inpaint_mask_model) @@ -333,33 +331,33 @@ def generate_mask(image, mask_model, cloth_category, dino_prompt_text, sam_model example_inpaint_mask_dino_prompt_text], queue=False, show_progress=False) - with gr.TabItem(label='Describe') as desc_tab: + with gr.Tab(label='Describe', id='describe_tab') as describe_tab: with gr.Row(): with gr.Column(): - desc_input_image = grh.Image(label='Image', source='upload', type='numpy', show_label=False) + describe_input_image = grh.Image(label='Image', source='upload', type='numpy', show_label=False) with gr.Column(): - desc_method = gr.Radio( + describe_method = gr.Radio( label='Content Type', - choices=[flags.desc_type_photo, flags.desc_type_anime], - value=flags.desc_type_photo) - desc_btn = gr.Button(value='Describe this Image into Prompt') - desc_image_size = gr.Textbox(label='Image Size and Recommended Size', elem_id='desc_image_size', visible=False) + choices=[flags.describe_type_photo, flags.describe_type_anime], + value=flags.describe_type_photo) + describe_btn = gr.Button(value='Describe this Image into Prompt') + describe_image_size = gr.Textbox(label='Image Size and Recommended Size', elem_id='describe_image_size', visible=False) gr.HTML('\U0001F4D4 Documentation') def trigger_show_image_properties(image): value = modules.util.get_image_size_info(image, modules.flags.sdxl_aspect_ratios) return gr.update(value=value, visible=True) - desc_input_image.upload(trigger_show_image_properties, inputs=desc_input_image, - outputs=desc_image_size, show_progress=False, queue=False) + describe_input_image.upload(trigger_show_image_properties, inputs=describe_input_image, + outputs=describe_image_size, show_progress=False, queue=False) - with gr.TabItem(label='Enhance') as enhance_tab: + with gr.Tab(label='Enhance', id='enhance_tab') as enhance_tab: with gr.Row(): with gr.Column(): enhance_input_image = grh.Image(label='Use with Enhance, skips image generation', source='upload', type='numpy') gr.HTML('\U0001F4D4 Documentation') - with gr.TabItem(label='Metadata') as metadata_tab: + with gr.Tab(label='Metadata', id='metadata_tab') as metadata_tab: with gr.Column(): metadata_input_image = grh.Image(label='For images created by Fooocus', source='upload', type='pil') metadata_json = gr.JSON(label='Metadata') @@ -382,7 +380,7 @@ def trigger_metadata_preview(file): with gr.Row(visible=modules.config.default_enhance_checkbox) as enhance_input_panel: with gr.Tabs(): - with gr.TabItem(label='Upscale or Variation'): + with gr.Tab(label='Upscale or Variation'): with gr.Row(): with gr.Column(): enhance_uov_method = gr.Radio(label='Upscale or Variation:', choices=flags.uov_list, @@ -407,7 +405,7 @@ def trigger_metadata_preview(file): enhance_inpaint_engine_ctrls = [] enhance_inpaint_update_ctrls = [] for index in range(modules.config.default_enhance_tabs): - with gr.TabItem(label=f'#{index + 1}') as enhance_tab_item: + with gr.Tab(label=f'#{index + 1}') as enhance_tab_item: enhance_enabled = gr.Checkbox(label='Enable', value=False, elem_classes='min_check', container=False) @@ -549,7 +547,7 @@ def trigger_metadata_preview(file): uov_tab.select(lambda: 'uov', outputs=current_tab, queue=False, _js=down_js, show_progress=False) inpaint_tab.select(lambda: 'inpaint', outputs=current_tab, queue=False, _js=down_js, show_progress=False) ip_tab.select(lambda: 'ip', outputs=current_tab, queue=False, _js=down_js, show_progress=False) - desc_tab.select(lambda: 'desc', outputs=current_tab, queue=False, _js=down_js, show_progress=False) + describe_tab.select(lambda: 'desc', outputs=current_tab, queue=False, _js=down_js, show_progress=False) enhance_tab.select(lambda: 'enhance', outputs=current_tab, queue=False, _js=down_js, show_progress=False) metadata_tab.select(lambda: 'metadata', outputs=current_tab, queue=False, _js=down_js, show_progress=False) enhance_checkbox.change(lambda x: gr.update(visible=x), inputs=enhance_checkbox, @@ -693,9 +691,9 @@ def update_history_link(): value=modules.config.default_sample_sharpness, info='Higher value means image and texture are sharper.') gr.HTML('\U0001F4D4 Documentation') - dev_mode = gr.Checkbox(label='Developer Debug Mode', value=False, container=False) + dev_mode = gr.Checkbox(label='Developer Debug Mode', value=modules.config.default_developer_debug_mode_checkbox, container=False) - with gr.Column(visible=False) as dev_tools: + with gr.Column(visible=modules.config.default_developer_debug_mode_checkbox) as dev_tools: with gr.Tab(label='Debug Tools'): adm_scaler_positive = gr.Slider(label='Positive ADM Guidance Scaler', minimum=0.1, maximum=3.0, step=0.001, value=1.5, info='The scaler multiplied to positive ADM (use 1.0 to disable). ') @@ -1062,16 +1060,16 @@ def trigger_metadata_import(file, state_is_generating): break def trigger_describe(mode, img): - if mode == flags.desc_type_photo: + if mode == flags.describe_type_photo: from extras.interrogate import default_interrogator as default_interrogator_photo return default_interrogator_photo(img), ["Fooocus V2", "Fooocus Enhance", "Fooocus Sharp"] - if mode == flags.desc_type_anime: + if mode == flags.describe_type_anime: from extras.wd14tagger import default_interrogator as default_interrogator_anime return default_interrogator_anime(img), ["Fooocus V2", "Fooocus Masterpiece"] return mode, ["Fooocus V2"] - desc_btn.click(trigger_describe, inputs=[desc_method, desc_input_image], - outputs=[prompt, style_selections], show_progress=True, queue=True) + describe_btn.click(trigger_describe, inputs=[describe_method, describe_input_image], + outputs=[prompt, style_selections], show_progress=True, queue=True) if args_manager.args.enable_auto_describe_image: def trigger_auto_describe(mode, img, prompt): @@ -1080,11 +1078,11 @@ def trigger_auto_describe(mode, img, prompt): return trigger_describe(mode, img) return gr.update(), gr.update() - uov_input_image.upload(trigger_auto_describe, inputs=[desc_method, uov_input_image, prompt], + uov_input_image.upload(trigger_auto_describe, inputs=[describe_method, uov_input_image, prompt], outputs=[prompt, style_selections], show_progress=True, queue=True) enhance_input_image.upload(lambda: gr.update(value=True), outputs=enhance_checkbox, queue=False, show_progress=False) \ - .then(trigger_auto_describe, inputs=[desc_method, enhance_input_image, prompt], outputs=[prompt, style_selections], show_progress=True, queue=True) + .then(trigger_auto_describe, inputs=[describe_method, enhance_input_image, prompt], outputs=[prompt, style_selections], show_progress=True, queue=True) def dump_default_english_config(): from modules.localization import dump_english_config