mirror of
https://github.com/vladmandic/automatic
synced 2026-09-19 01:04:32 +02:00
Enable A1111 and Full parsing conversion
This commit is contained in:
@@ -1,9 +1,25 @@
|
||||
import torch
|
||||
import modules.shared as shared
|
||||
import modules.prompt_parser as prompt_parser
|
||||
from compel import Compel, ReturnedEmbeddingsType
|
||||
import diffusers
|
||||
import typing
|
||||
|
||||
def convert_to_compel(prompt: str):
|
||||
if prompt is None:
|
||||
return None
|
||||
all_schedules = prompt_parser.get_learned_conditioning_prompt_schedules(prompt, 100)[0] #100 should be steps, but doesn't actually matter because we can't schedule yet
|
||||
output_list = prompt_parser.parse_prompt_attention(all_schedules[0][1])
|
||||
converted_prompt = []
|
||||
for subprompt, weight in output_list:
|
||||
if subprompt != " ":
|
||||
if weight == 1:
|
||||
converted_prompt.append(subprompt)
|
||||
else:
|
||||
converted_prompt.append(f"({subprompt}){weight}")
|
||||
converted_prompt = " ".join(converted_prompt)
|
||||
return converted_prompt
|
||||
|
||||
def compel_encode_prompt(pipeline: typing.Any, *args, **kwargs):
|
||||
compel_encode_fn = COMPEL_ENCODE_FN_DICT.get(type(pipeline), None)
|
||||
if compel_encode_fn is None:
|
||||
@@ -11,6 +27,12 @@ def compel_encode_prompt(pipeline: typing.Any, *args, **kwargs):
|
||||
return compel_encode_fn(pipeline, *args, **kwargs)
|
||||
|
||||
def compel_encode_prompt_sdxl(pipeline: diffusers.StableDiffusionXLPipeline, prompt: str, negative_prompt: str, prompt_2: typing.Optional[str]=None, negative_prompt_2: typing.Optional[str]=None, refiner=False):
|
||||
if shared.opts.data['prompt_attention'] != 'Compel parser':
|
||||
prompt = convert_to_compel(prompt)
|
||||
negative_prompt = convert_to_compel(negative_prompt)
|
||||
prompt_2 = convert_to_compel(prompt_2)
|
||||
negative_prompt_2 = convert_to_compel(negative_prompt_2)
|
||||
|
||||
compel_te1 = Compel(
|
||||
tokenizer=pipeline.tokenizer,
|
||||
text_encoder=pipeline.text_encoder,
|
||||
@@ -24,7 +46,7 @@ def compel_encode_prompt_sdxl(pipeline: diffusers.StableDiffusionXLPipeline, pro
|
||||
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
||||
requires_pooled=True,
|
||||
)
|
||||
if refiner is None:
|
||||
if refiner is False:
|
||||
positive_te1 = compel_te1(prompt)
|
||||
positive_te2, pooled = compel_te2(prompt_2)
|
||||
positive = torch.cat((positive_te1, positive_te2), dim=-1)
|
||||
@@ -36,7 +58,7 @@ def compel_encode_prompt_sdxl(pipeline: diffusers.StableDiffusionXLPipeline, pro
|
||||
positive, pooled = compel_te2(prompt)
|
||||
negative, negative_pooled = compel_te2(negative_prompt)
|
||||
|
||||
|
||||
shared.log.debug(compel_te1.parse_prompt_string(prompt))
|
||||
[prompt_embed, negative_embed] = compel_te2.pad_conditioning_tensors_to_same_length([positive, negative])
|
||||
return prompt_embed, pooled, negative_embed, negative_pooled
|
||||
|
||||
|
||||
Reference in New Issue
Block a user