赞
踩
prompt和negative_prompt都可以通过apply_wildcards函数来实现通配符替换,apply_wildcards会从txt中随机找一个出来。
prompt='sunshine, river, trees, __artist__'
task_prompt = apply_wildcards(prompt, task_rng)
task_negative_prompt = apply_wildcards(negative_prompt, task_rng)

def apply_wildcards(wildcard_text, rng, directory=wildcards_path): for _ in range(wildcards_max_bfs_depth): placeholders = re.findall(r'__([\w-]+)__', wildcard_text) if len(placeholders) == 0: return wildcard_text print(f'[Wildcards] processing: {wildcard_text}') for placeholder in placeholders: try: words = open(os.path.join(directory, f'{placeholder}.txt'), encoding='utf-8').read().splitlines() words = [x for x in words if x != ''] assert len(words) > 0 wildcard_text = wildcard_text.replace(f'__{placeholder}__', rng.choice(words), 1) except: print(f'[Wildcards] Warning: {placeholder}.txt missing or empty. ' f'Using "{placeholder}" as a normal word.') wildcard_text = wildcard_text.replace(f'__{placeholder}__', placeholder) print(f'[Wildcards] {wildcard_text}') print(f'[Wildcards] BFS stack overflow. Current text: {wildcard_text}') return wildcard_text
Fooocus可选多种风格,都是以更改prompt和negative_prompt来实现。
风格s传入apply_style函数中,得到prompt和negative_prompt:
注意:空列表+空列表仍旧是一个空列表,非空列表加空列表等于没加空列表。
positive_basic_workloads = []
negative_basic_workloads = []
style_selections = ['Fooocus Enhance', 'Fooocus Sharp']
task_prompt='sunshine, river, trees,'
for s in style_selections:
p, n = apply_style(s, positive=task_prompt) # 得到prompt和negative_prompt
positive_basic_workloads = positive_basic_workloads + p
negative_basic_workloads = negative_basic_workloads + n
apply_style函数则为:
def apply_style(style, positive):
p, n = styles[style]
return p.replace('{prompt}', positive).splitlines(), n.splitlines()
styles是一个全局字典,目前有213个key,则是有213种风格。每个key中是一个2个元素的元组,即是 prompt和negative_prompt

p.replace(‘{prompt}’, positive).splitlines()
用用户输入的prompt来替换风格字符串可能存在的{prompt},然后使用splitlines()方法,得到字符串的list返回(使用splitlines()或许有深层次考虑,但我没发现)。

这里就会发现,很容易有多个风格产生多个negativeprompt。

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。