diff --git a/replacer/mask_creator.py b/replacer/mask_creator.py index 0d2694f..17d8e32 100644 --- a/replacer/mask_creator.py +++ b/replacer/mask_creator.py @@ -3,7 +3,7 @@ from modules import devices from replacer.extensions import replacer_extensions from replacer.generation_args import GenerationArgs -from replacer.options import needAutoUnloadModels, EXT_NAME, useCpuForDetection, useFastDilation +from replacer.options import EXT_NAME, useCpuForDetection, useFastDilation from replacer.tools import areImagesTheSame, limitImageByOneDemention, fastMaskDilate sam_predict = None update_mask = None @@ -12,15 +12,15 @@ def initSamDependencies(): global sam_predict, update_mask, clear_cache if not sam_predict or not update_mask or not clear_cache: - import scripts.sam - sam_predict = scripts.sam.sam_predict + from lib_segment_anything import sam + sam_predict = sam.sam_predict if useFastDilation(): update_mask = fastMaskDilate else: - update_mask = scripts.sam.update_mask - clear_cache = scripts.sam.clear_cache + update_mask = sam.update_mask + clear_cache = sam.clear_cache if useCpuForDetection(): - scripts.sam.sam_device = 'cpu' + sam.sam_device = 'cpu' print('Use CPU for SAM') diff --git a/replacer/tools.py b/replacer/tools.py index 8463469..e117a99 100644 --- a/replacer/tools.py +++ b/replacer/tools.py @@ -130,8 +130,8 @@ def extraMaskExpand(mask: Image.Image, expand: int): if useFastDilation(): update_mask = fastMaskDilate else: - from scripts.sam import update_mask as update_mask_ - update_mask = update_mask_ + from lib_segment_anything import sam + update_mask = sam.update_mask_ expandedMask = update_mask(mask, 0, expand, mask.convert('RGBA'))[1] cachedExtraMaskExpand = CachedExtraMaskExpand(mask, expand, expandedMask) print('extraMaskExpand cached') @@ -208,8 +208,8 @@ def interrupted(): def clearCache(): global g_clear_cache if g_clear_cache is None: - from scripts.sam import clear_cache - g_clear_cache = clear_cache + from lib_segment_anything import sam + g_clear_cache = sam.clear_cache g_clear_cache() diff --git a/replacer/ui/make_advanced_options.py b/replacer/ui/make_advanced_options.py index d7d287a..0b45b61 100644 --- a/replacer/ui/make_advanced_options.py +++ b/replacer/ui/make_advanced_options.py @@ -126,13 +126,13 @@ def makeAdvancedOptions(comp: AttrDict, isDedicatedPage: bool): ) with gr.Row(): - from scripts.sam import sam_model_list, refresh_sam_models + from lib_segment_anything import sam from scripts.dino import dino_model_list - + sam_model_list = sam.get_sam_model_list() comp.sam_model_name = gr.Dropdown(label="SAM Model", choices=sam_model_list, value=sam_model_list[0] if len(sam_model_list) > 0 else None) comp.sam_refresh_models = ToolButton(value=refresh_symbol) - comp.sam_refresh_models.click(refresh_sam_models, comp.sam_model_name, comp.sam_model_name) + comp.sam_refresh_models.click(sam.refresh_sam_models, comp.sam_model_name, comp.sam_model_name) comp.dino_model_name = gr.Dropdown(label="GroundingDINO Model", choices=dino_model_list, value=dino_model_list[0]) diff --git a/replacer/ui/tools_ui.py b/replacer/ui/tools_ui.py index 90d9557..aa4d45d 100644 --- a/replacer/ui/tools_ui.py +++ b/replacer/ui/tools_ui.py @@ -48,8 +48,8 @@ def get_current_image(image, isAvoid, needLimit, maxResolutionOnDetection): def unloadModels(): mem_stats = {k: -(v//-(1024*1024)) for k, v in shared.mem_mon.stop().items()} memBefore = mem_stats['reserved'] - from scripts.sam import clear_cache - clear_cache() + from lib_segment_anything import sam + sam.clear_cache() mem_stats = {k: -(v//-(1024*1024)) for k, v in shared.mem_mon.stop().items()} memAfter = mem_stats['reserved'] diff --git a/scripts/replacer_api.py b/scripts/replacer_api.py index d962366..12d6bcb 100644 --- a/scripts/replacer_api.py +++ b/scripts/replacer_api.py @@ -15,7 +15,8 @@ def replacer_api(_, app: FastAPI): - from scripts.sam import sam_model_list + from lib_segment_anything import sam + sam_model_list = sam.get_sam_model_list() from scripts.dino import dino_model_list try: from lama_cleaner_masked_content.inpaint import lamaInpaint diff --git a/scripts/replacer_main_ui.py b/scripts/replacer_main_ui.py index 5cf99bd..f5744f0 100644 --- a/scripts/replacer_main_ui.py +++ b/scripts/replacer_main_ui.py @@ -15,7 +15,7 @@ def on_ui_tabs(): replacer_tab_ui.reinitMainUIAfterUICreated() tab = replacer_tab_ui.replacerMainUI.getReplacerTabUI() - return [(tab, EXT_NAME, EXT_NAME)] + return [(tab, EXT_NAME, EXT_NAME_LOWER)] script_callbacks.on_ui_tabs(on_ui_tabs) @@ -43,7 +43,7 @@ def mountDedicatedPage(demo, app): loadsave = copy.copy(demo.ui_loadsave) loadsave.finalized_ui = False - loadsave.add_block(replacerUi, EXT_NAME) + loadsave.add_block(replacerUi, EXT_NAME_LOWER) loadsave.dump_defaults() replacerUi.ui_loadsave = loadsave gr.mount_gradio_app(app, replacerUi, path=path) diff --git a/scripts/replacer_script.py b/scripts/replacer_script.py index a26ae33..47f6889 100644 --- a/scripts/replacer_script.py +++ b/scripts/replacer_script.py @@ -34,106 +34,110 @@ def show(self, is_img2img): return scripts.AlwaysVisible def ui(self, is_img2img): - tabName = 'img2img' if is_img2img else 'txt2img' - with ( - InputAccordion(False, label=EXT_NAME) if InputAccordion - else gr.Accordion(EXT_NAME, open=False) - as enable - ): - if not InputAccordion: + try: + tabName = 'img2img' if is_img2img else 'txt2img' + with ( + InputAccordion(False, label=EXT_NAME) if InputAccordion + else gr.Accordion(EXT_NAME, open=False) + as enable + ): + if not InputAccordion: + with gr.Row(): + enable = gr.Checkbox(False, label="Enable") with gr.Row(): - enable = gr.Checkbox(False, label="Enable") - with gr.Row(): - gr.Markdown(f'This script takes all {EXT_NAME} settings from its tab') - with gr.Row(): - save_originals = gr.Checkbox(True, label="Save originals", elem_id=f'replacer_{tabName}_save_originals') - follow_txt2img_hires_fix = gr.Checkbox(True, label="Follow txt2img hires fix", elem_id=f'replacer_{tabName}_follow_txt2img_hires_fix',visible=not is_img2img) - with gr.Row(): - force_override_sd_model = gr.Checkbox(label='Force override stable diffusion model', - value=True, elem_id=f"replacer_{tabName}_force_override_sd_model", - info='Be sure you use inpainting model here') - force_sd_model_checkpoint = ui_settings.create_setting_component('sd_model_checkpoint') - - comp = replacer_tab_ui.replacerMainUI.components - - main_tab_inputs = [ - comp.detectionPrompt, - comp.avoidancePrompt, - comp.positvePrompt, - comp.negativePrompt, - comp.upscaler_for_img2img, - comp.seed, - comp.sampler, - comp.scheduler, - comp.steps, - comp.box_threshold, - comp.mask_expand, - comp.mask_blur, - comp.max_resolution_on_detection, - comp.sam_model_name, - comp.dino_model_name, - comp.cfg_scale, - comp.denoise, - comp.inpaint_padding, - comp.inpainting_fill, - comp.width, - comp.height, - comp.batch_count, - comp.batch_size, - comp.inpainting_mask_invert, - comp.extra_includes, - comp.fix_steps, - comp.override_sd_model, - comp.sd_model_checkpoint, - comp.mask_num, - comp.avoid_mask_mode, - comp.avoidance_mask, - comp.only_custom_mask, - comp.custom_mask_mode, - comp.custom_mask, - comp.use_inpaint_diff, - comp.inpaint_diff_mask_view, - comp.lama_cleaner_upscaler, - comp.clip_skip, - comp.pass_into_hires_fix_automatically, - comp.save_before_hires_fix, - comp.do_not_use_mask, + gr.Markdown(f'This script takes all {EXT_NAME} settings from its tab') + with gr.Row(): + save_originals = gr.Checkbox(True, label="Save originals", elem_id=f'replacer_{tabName}_save_originals') + follow_txt2img_hires_fix = gr.Checkbox(True, label="Follow txt2img hires fix", elem_id=f'replacer_{tabName}_follow_txt2img_hires_fix',visible=not is_img2img) + with gr.Row(): + force_override_sd_model = gr.Checkbox(label='Force override stable diffusion model', + value=True, elem_id=f"replacer_{tabName}_force_override_sd_model", + info='Be sure you use inpainting model here') + force_sd_model_checkpoint = ui_settings.create_setting_component('sd_model_checkpoint') + + comp = replacer_tab_ui.replacerMainUI.components + + main_tab_inputs = [ + comp.detectionPrompt, + comp.avoidancePrompt, + comp.positvePrompt, + comp.negativePrompt, + comp.upscaler_for_img2img, + comp.seed, + comp.sampler, + comp.scheduler, + comp.steps, + comp.box_threshold, + comp.mask_expand, + comp.mask_blur, + comp.max_resolution_on_detection, + comp.sam_model_name, + comp.dino_model_name, + comp.cfg_scale, + comp.denoise, + comp.inpaint_padding, + comp.inpainting_fill, + comp.width, + comp.height, + comp.batch_count, + comp.batch_size, + comp.inpainting_mask_invert, + comp.extra_includes, + comp.fix_steps, + comp.override_sd_model, + comp.sd_model_checkpoint, + comp.mask_num, + comp.avoid_mask_mode, + comp.avoidance_mask, + comp.only_custom_mask, + comp.custom_mask_mode, + comp.custom_mask, + comp.use_inpaint_diff, + comp.inpaint_diff_mask_view, + comp.lama_cleaner_upscaler, + comp.clip_skip, + comp.pass_into_hires_fix_automatically, + comp.save_before_hires_fix, + comp.do_not_use_mask, - comp.hf_upscaler, - comp.hf_steps, - comp.hf_sampler, - comp.hf_scheduler, - comp.hf_denoise, - comp.hf_cfg_scale, - comp.hfPositivePromptSuffix, - comp.hf_size_limit, - comp.hf_above_limit_upscaler, - comp.hf_unload_detection_models, - comp.hf_disable_cn, - comp.hf_extra_mask_expand, - comp.hf_positvePrompt, - comp.hf_negativePrompt, - comp.hf_sd_model_checkpoint, - comp.hf_extra_inpaint_padding, - comp.hf_extra_mask_blur, - comp.hf_randomize_seed, - comp.hf_soft_inpaint, - ] + comp.cn_inputs \ - + comp.soft_inpaint_inputs + comp.hf_upscaler, + comp.hf_steps, + comp.hf_sampler, + comp.hf_scheduler, + comp.hf_denoise, + comp.hf_cfg_scale, + comp.hfPositivePromptSuffix, + comp.hf_size_limit, + comp.hf_above_limit_upscaler, + comp.hf_unload_detection_models, + comp.hf_disable_cn, + comp.hf_extra_mask_expand, + comp.hf_positvePrompt, + comp.hf_negativePrompt, + comp.hf_sd_model_checkpoint, + comp.hf_extra_inpaint_padding, + comp.hf_extra_mask_blur, + comp.hf_randomize_seed, + comp.hf_soft_inpaint, + ] + comp.cn_inputs \ + + comp.soft_inpaint_inputs - for i in range(len(main_tab_inputs)): - main_tab_inputs[i] = copy.copy(main_tab_inputs[i]) - main_tab_inputs[i].do_not_save_to_config = True + for i in range(len(main_tab_inputs)): + main_tab_inputs[i] = copy.copy(main_tab_inputs[i]) + main_tab_inputs[i].do_not_save_to_config = True - inputs = [ - enable, - save_originals, - force_override_sd_model, - force_sd_model_checkpoint, - follow_txt2img_hires_fix, - ] + inputs = [ + enable, + save_originals, + force_override_sd_model, + force_sd_model_checkpoint, + follow_txt2img_hires_fix, + ] - return inputs + main_tab_inputs + return inputs + main_tab_inputs + + except Exception as e: + print(f'[{EXT_NAME}] cannot create script: {e}') def before_process(self, p: StableDiffusionProcessingTxt2Img, enable,