CoolFace
Apppublic

cymic/Waifu_Diffusion_Webui

sourceHugging Faceupdated 4y agoView on Hugging Face
1likes
prompts_from_file.py56 linesDownload Raw Back to scripts
1import math2import os3import sys4import traceback5 6import modules.scripts as scripts7import gradio as gr8 9from modules.processing import Processed, process_images10from PIL import Image11from modules.shared import opts, cmd_opts, state12 13 14class Script(scripts.Script):15    def title(self):16        return "Prompts from file or textbox"17 18    def ui(self, is_img2img):19        # This checkbox would look nicer as two tabs, but there are two problems:20        # 1) There is a bug in Gradio 3.3 that prevents visibility from working on Tabs21        # 2) Even with Gradio 3.3.1, returning a control (like Tabs) that can't be used as input22        #    causes a AttributeError: 'Tabs' object has no attribute 'preprocess' assert,23        #    due to the way Script assumes all controls returned can be used as inputs.24        # Therefore, there's no good way to use grouping components right now,25        # so we will use a checkbox! :)26        checkbox_txt = gr.Checkbox(label="Show Textbox", value=False)27        file = gr.File(label="File with inputs", type='bytes')28        prompt_txt = gr.TextArea(label="Prompts")29        checkbox_txt.change(fn=lambda x: [gr.File.update(visible = not x), gr.TextArea.update(visible = x)], inputs=[checkbox_txt], outputs=[file, prompt_txt])30        return [checkbox_txt, file, prompt_txt]31 32    def run(self, p, checkbox_txt, data: bytes, prompt_txt: str):33        if (checkbox_txt):34            lines = [x.strip() for x in prompt_txt.splitlines()]35        else:36            lines = [x.strip() for x in data.decode('utf8', errors='ignore').split("\n")]37        lines = [x for x in lines if len(x) > 0]38 39        img_count = len(lines) * p.n_iter40        batch_count = math.ceil(img_count / p.batch_size)41        loop_count = math.ceil(batch_count / p.n_iter)42        print(f"Will process {img_count} images in {batch_count} batches.")43 44        p.do_not_save_grid = True45 46        state.job_count = batch_count47 48        images = []49        for loop_no in range(loop_count):50            state.job = f"{loop_no + 1} out of {loop_count}"51            p.prompt = lines[loop_no*p.batch_size:(loop_no+1)*p.batch_size] * p.n_iter52            proc = process_images(p)53            images += proc.images54 55        return Processed(p, images, p.seed, "")56