| import re |
| import random |
| import gradio as gr |
| import modules.shared as shared |
| import modules.scripts as scripts |
| import modules.sd_samplers |
| from modules.processing import process_images, StableDiffusionProcessingTxt2Img |
|
|
| |
| |
|
|
| class Script(scripts.Script): |
| def title(self): |
| return "random prompt" |
|
|
| def ui(self, is_img2img): |
| dummy = gr.Textbox(label="随机标签脚本已启动",value="Batch count≥1,Batch size=1,建议在提示词最末尾添加随机标签: ,←short|long|messy→ hair, ") |
| sameseed = gr.Checkbox(label="生成相同的种子", value=False) |
| return [dummy,sameseed] |
|
|
| def run(self, p, dummy, sameseed): |
|
|
| original_prompt = p.prompt[0] if type(p.prompt) == list else p.prompt |
|
|
| all_prompts = [original_prompt] |
| |
|
|
| split_str=[''] |
| right_str=[] |
| gen_prompt='' |
| new_prompt='' |
| |
| for this_prompt in all_prompts: |
| for data in re.finditer(r'(←([^→]+)→)', this_prompt): |
| if data: |
| span = data.span(1) |
| new_prompt = this_prompt[:span[0]] |
| gen_prompt = this_prompt[span[0]:] |
| this_str=gen_prompt.replace("←","→") |
| split_str =this_str.split("→") |
| break |
| |
| |
| |
| |
| |
| split_str[0]=split_str[0].strip() |
| split_str[0]=split_str[0].strip(',') |
| split_str[0]=split_str[0]+' ' |
| for in_str in split_str: |
| if "|" not in in_str: |
| if "," in in_str: |
| FL_str=in_str.split(",") |
| right_str.append(FL_str[0]) |
| right_str.append(FL_str[1]) |
| else: |
| right_str.append(in_str) |
| |
| |
| |
| |
| |
| for ip in range(p.n_iter+1): |
| |
| my_prompt="" |
| |
| i=0 |
| for data in re.finditer(r'(←([^→]+)→)', gen_prompt): |
| if data: |
|
|
| items = data.group(2).split("|") |
| length=len(items)-1 |
|
|
| rand_item = items[random.randint(0,length)] |
| |
| rand_str=right_str[i]+rand_item+right_str[i+1] |
| |
| my_prompt=my_prompt+rand_str.strip()+"," |
| i=i+2 |
| |
| i_prompt=new_prompt+my_prompt |
| all_prompts.append(i_prompt) |
| |
| |
| |
| if gen_prompt != '': |
| all_prompts.remove(all_prompts[0]) |
| |
|
|
| p.prompt = all_prompts * p.n_iter |
| if sameseed == True: |
| p.seed =[item for item in range(int(p.seed), int(p.seed) + p.n_iter) for _ in range(len(all_prompts))] |
| |
| p.do_not_save_grid = True |
| p.prompt_for_display = original_prompt |
|
|
| return process_images(p) |
|
|