improved comments & added warning if value couldn't be parsed correctly

This commit is contained in:
xra
2022-08-22 23:32:01 +09:00
parent 2736d7e15e
commit a3632f5b4f

View File

@ -457,52 +457,47 @@ The vast majority of these arguments default to reasonable values.
image = image[None].transpose(0, 3, 1, 2) image = image[None].transpose(0, 3, 1, 2)
image = torch.from_numpy(image) image = torch.from_numpy(image)
return 2.*image - 1. return 2.*image - 1.
"""
example: "an apple: a banana:0 a watermelon:0.5"
grabs all text up to the first occurance of ':'
then removes the text, repeating until no characters left.
if ':' has no weight defined, defaults to 1.0
the above example turns into 3 sub-prompts: """
"an apple" 1.0 grabs all text up to the first occurrence of ':'
"a banana" 0.0 uses the grabbed text as a sub-prompt, and takes the value following ':' as weight
"a watermelon" 0.5 if ':' has no value defined, defaults to 1.0
The weights are added and normalized repeats until no text remaining
The resulting image will be: apple 66% (1.0 / 1.5), banana 0%, watermelon 33% (0.5 / 1.5)
""" """
def split_weighted_subprompts(text): def split_weighted_subprompts(text):
# very simple, uses : to separate sub-prompts
# assumes number following : and space after number
# if no number found, defaults to 1.0
remaining = len(text) remaining = len(text)
prompts = [] prompts = []
weights = [] weights = []
while remaining > 0: while remaining > 0:
# find :
if ":" in text: if ":" in text:
idx = text.index(":") # first occurrance from start idx = text.index(":") # first occurrence from start
# snip sub prompt # grab up to index as sub-prompt
prompt = text[:idx] prompt = text[:idx]
remaining -= idx remaining -= idx
# remove from main text # remove from main text
text = text[idx+1:] text = text[idx+1:]
# get number # find value for weight
if " " in text: if " " in text:
idx = text.index(" ") # first occurance idx = text.index(" ") # first occurence
else: # no space, read to end else: # no space, read to end
idx = len(text) idx = len(text)
if idx != 0: if idx != 0:
weight = float(text[:idx]) try:
else: # no number to grab weight = float(text[:idx])
except: # couldn't treat as float
print(f"Warning: '{text[:idx]}' is not a value, are you missing a space?")
weight = 1.0
else: # no value found
weight = 1.0 weight = 1.0
# remove # remove from main text
remaining -= idx remaining -= idx
text = text[idx+1:] text = text[idx+1:]
# append the sub-prompt and its weight
prompts.append(prompt) prompts.append(prompt)
weights.append(weight) weights.append(weight)
else: else: # no : found
if len(text) > 0: if len(text) > 0: # there is still text though
# take what remains as weight 1 # take remainder as weight 1
prompts.append(text) prompts.append(text)
weights.append(1.0) weights.append(1.0)
remaining = 0 remaining = 0