import torch from torch import autocast from compel import Compel import base64 from PIL import Image from io import BytesIO import numpy as np from diffusers import DiffusionPipeline, AutoencoderKL, StableDiffusionImg2ImgPipeline,AutoPipelineForImage2Image,AutoPipelineForInpainting,StableDiffusionPipeline model_id = "IDK-ab0ut/Yiffymix_v52-XL" # has new pooled embeds model1_id=model_id model2_id=model_id device = "cuda" pipe1 = None pipe2 = AutoPipelineForInpainting.from_pretrained(model2_id, torch_dtype=torch.float16, use_safetensors=True) pipe2 = pipe2.to(device) compel_proc2 = Compel(tokenizer=[pipe2.tokenizer,pipe2.tokenizer_2], text_encoder=[pipe2.text_encoder, pipe2.text_encoder_2], truncate_long_prompts=False, requires_pooled=[False, True]) def display(img): img.show() def test(prompt,i=1,start=1024,depth=30,guidance_scale=7.5, negative_prompt=None, pipe=pipe1, width=512, height=512, skip=2): if isinstance(prompt,str): posprompt=prompt negprompt=negative_prompt else: posprompt=prompt[0] negprompt=prompt[1] print("+ %s\n- %s\n"%(posprompt,negprompt)) prompt_embeds, prompt_embeds_pooled = compel_proc(posprompt) negative_prompt_embeds, negative_prompt_embeds_pooled = compel_proc(negprompt) [prompt_embeds,negative_prompt_embeds] = compel_proc.pad_conditioning_tensors_to_same_length([prompt_embeds, negative_prompt_embeds]) results=[] for a in range(start,start+i): generator = torch.Generator(device).manual_seed(a) with autocast(device): image = pipe(prompt_embeds=prompt_embeds, pooled_prompt_embeds=prompt_embeds_pooled, guidance_scale=guidance_scale,num_inference_steps=depth, generator=generator, negative_prompt_embeds=negative_prompt_embeds, negative_pooled_prompt_embeds=negative_prompt_embeds_pooled, clip_skip=skip, width=width,height=height).images[0] print(a) display(image) results.append(image) return results def generate(prompt,i,depth=60,guidance_scale=7.5,negative_prompt=None,width=512,height=512,pipe=pipe1, skip=2): return test(prompt,1,start=i,depth=depth,guidance_scale=guidance_scale,negative_prompt=negative_prompt,width=width,height=height,pipe=pipe,skip=skip) def improvetest(prompt, inputimg=None, maskimage=None, i=1,strength=0.3,start=1024,depth=30,guidance_scale=7.5, negative_prompt=None, pipe=pipe2, skip=2, width=512, height=512): if isinstance(prompt,str): posprompt=prompt negprompt=negative_prompt else: posprompt=prompt[0] negprompt=prompt[1] print("+ %s\n- %s\n"%(posprompt,negprompt)) prompt_embeds, prompt_embeds_pooled = compel_proc2(posprompt) negative_prompt_embeds, negative_prompt_embeds_pooled = compel_proc2(negprompt) [prompt_embeds,negative_prompt_embeds] = compel_proc2.pad_conditioning_tensors_to_same_length([prompt_embeds, negative_prompt_embeds]) results=[] for a in range(start,start+i): generator = torch.Generator(device).manual_seed(a) with autocast(device): with torch.inference_mode(): image = pipe(image=inputimg, mask_image=maskimage, strength=strength, prompt_embeds=prompt_embeds, pooled_prompt_embeds=prompt_embeds_pooled, guidance_scale=guidance_scale,num_inference_steps=depth, generator=generator, negative_prompt_embeds=negative_prompt_embeds, negative_pooled_prompt_embeds=negative_prompt_embeds_pooled, clip_skip=skip, width=width, height=height).images[0] print(a) display(image) results.append(image) return results def improve(prompt,inputimg,i,strength=0.3,depth=60,guidance_scale=7.5,negative_prompt=None,pipe=pipe2): return improvetest(prompt,inputimg, 1,strength=strength,start=i,depth=depth,guidance_scale=guidance_scale,negative_prompt=negative_prompt,pipe=pipe) def resize(img): w,h=img.size s=max(w,h) f=512./s return img.resize((int(w*f),int(h*f))) def loadimage(data): r=base64.b64decode((data.split("base64,",1))[-1]) img = Image.open(BytesIO(r)) npimg=np.array(img) npimg=npimg[:,:,[0,1,2]] return Image.fromarray(npimg) return resize(Image.fromarray(npimg)) white="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAIAAADZF8uwAAAAA3NCSVQICAjb4U/gAAAAGUlEQVQYV2P8//8/AyHAREgBSH5UEb2DAAA/qQMVBPlgXQAAAABJRU5ErkJggg==" imgbb=loadimage(white) p0=""" Photorealistic: The butcher, a middle aged bull leaned back and waited for the next client. A young gazelle stepped in: Sylvia turned 14 today and it was time for her to get processed. The butcher told her to lay down on the large table. Sylvia stretched, exposing her naked belly. Are you excited? the butcher asked her, petting her belly. """ n0="""ugly""" p=(p0,n0) res0=improvetest(p,imgbb,imgbb,1,strength=1.0,guidance_scale=3,depth=35,start=1002) x=res0[0] p0=""" Photorealistic: A young gazelle has come to visit the butcher bull to be processed. She was told to lay down on the table. Sylvia the gazelle nodded and patiently held still. The knife was very sharp. The man cut the gazelle deep and open, sliced her belly-skin, and took out her inner organs. """ n0="""ugly""" p=(p0,n0) res0=improvetest(p,x,imgbb,1,strength=0.65,guidance_scale=3.5,depth=30,start=1018) y=res0[0] p0=""" Photorealistic: A young gazelle has come to visit the butcher bull to be processed. She was told to lay down on the table. After Sylvia was butchered her belly was filled with delicious vegetables and stuffed to the brim. She squirmed, her belly bulging and twitching, excited for what would come next. her nipples got pointy. photorealistic fur gazelle stuffed with vegetables """ n0="""ugly""" p=(p0,n0) res0=improvetest(p,y,imgbb,1,strength=0.65,guidance_scale=3.5,depth=30,start=1006) z=res0[0] p0=""" Photorealistic: A young gazelle has come to visit the kitchen to be processed. She was told to lay down on the table. After Sylvia had been roasted in the oven, she had turned into a very delicious gazelle roast. Stretching on her back on a serving platter, her roasted meat juicy, her roasted skin crispy, and completely cooked through, with her belly stuffed with delicious roasted vegetables, her tiny breasts crispy. Her roasted body was roasted to perfection, completely cooked through and through. """ n0="""ugly""" p=(p0,n0) res0=improvetest(p,x,imgbb,1,strength=0.65,guidance_scale=3.5,depth=25,start=1006) v=res0[0]