Alvi-alvarez / sd-Img2img-batch-interrogator

Img2img batch interrogator for AUTOMATIC1111's Stable Diffusion web UI
MIT License
18 stars 3 forks source link

Filtering Update 2 sd_tag_batch.py #8

Closed SmirkingKitsune closed 3 months ago

SmirkingKitsune commented 3 months ago

Allows users to filter out content from the interrogation without having to put it in the prompt or negative prompt. This also allows users to reuse their custom filters from a previous run, allowing users to keep their filters through different sessions.

Custom Prompt Filter is disabled by default, to maintain vanilla workflow, though I don't think it would do anything even if it was enabled when the custom prompt is empty.

Previous sessions where the custom filtering prompt was used are saved as custom_filter.txt to the extensions\sd-Img2img-batch-interrogator directory. This file should only be saved if the custom prompt was used. Pressing the button should load the custom_filter.txt into the custom_filter textbox.


There are also some UI cleanup items. Such as moving the model select dropdown to the top of the UI, instead of being at the bottom. Changed "Deepbooru Tools" to "Filtering Tools" since it is being applied to CLIP too. I changed "interrogator weight" to "Interrogator Prompt Weight", and "Use Weighted Prompt" to "Use Interrogator Prompt Weight", to make it clearer to the user what these options do. I also changed my username in the comments.

I had initially attempted to hide the prompt_weight slider and the custom_filter textbox if the user had unchecked the use_weight or use_custom_filter checkboxes. Since those interactable components were not in use, users unfamiliar with the code could assume that they are doing something since they are still visible. However, I was unable to get it to work, so most of the cleanup items are remnants of my trying to implement a visibility update function. I determined that the custom filter feature was too fun to sit on, so I just ripped out the broken visibility code for the commit. The use_custom_filter checkbox being above the prompt_weight slider is one of the most notable remnants of this.

I will post what the UI function looked like before I removed the broken visibility code below, please note that the code below is currently non-functional, but the code in the commit is functional:

    def ui(self, is_img2img):
        # Function to load custom filter from file
        def load_custom_filter(custom_filter):
            with open("extensions\sd-Img2img-batch-interrogator\custom_filter.txt", "r") as file:
                custom_filter = file.read()
                return custom_filter

        # Function to update visibility of interactable components
        def update_visibility(interactable):
            interactable[0].visibility = interactable[1]
            return interactable[0]

        model_options = ["CLIP", "Deepbooru"]
        model_selection = gr.Dropdown(choices=model_options, label="Select Model(s)", multiselect=True, value="Deepbooru")

        in_front = gr.Checkbox(label="Prompt in front", value=True)
        use_weight = gr.Checkbox(label="Use Interrogator Prompt Weight", value=True)
        prompt_weight = gr.Slider(
            0.0, 1.0, value=0.5, step=0.1, label="Interrogator Prompt Weight"
        )

        with gr.Accordion("Filter tools:", open=False):
            no_duplicates = gr.Checkbox(label="Filter Duplicate Prompt Content from Interrogation", value=False)
            use_negatives = gr.Checkbox(label="Filter Negative Prompt Content from Interrogation", value=False)
            use_custom_filter = gr.Checkbox(label="Filter Custom Prompt Content from Interrogation", value=False)
            custom_filter = gr.Textbox(
                label="Custom Filter Prompt", 
                placeholder="Prompt content seperated by commas. Warning ignores attention syntax, parentheses '()' and colon suffix ':XX.XX' are discarded.", 
                show_copy_button=True
            )
            # Button to load custom filter from file
            load_custom_filter_button = gr.Button(value="Load Last Custom Filter")

        # Listeners
        load_custom_filter_button.click(load_custom_filter, inputs=custom_filter, outputs=custom_filter)
        use_weight.input(update_visibility, input=[prompt_weight, use_weight], output=prompt_weight)
        use_custom_filter.input(update_visibility, input=[custom_filter, use_custom_filter], output=custom_filter)

        return [in_front, prompt_weight, model_selection, use_weight, no_duplicates, use_negatives, use_custom_filter, custom_filter]

As far as I can tell the use_weight and use_custom_filter listeners cause the UI to freeze, preventing the run from getting expected variables.

Alvi-alvarez commented 3 months ago

I tried to hide elements but it doesn't work, on the other hand prompt_weight is wrong, because it should go from -3 to 3 or something like that but apparently it is saved in chache and it doesn't change.