Skip to content

Instantly share code, notes, and snippets.

@recoilme
Created July 22, 2024 20:55
Show Gist options
  • Save recoilme/ae7ab0fb7d7d73103df4e8956e28bf82 to your computer and use it in GitHub Desktop.
Save recoilme/ae7ab0fb7d7d73103df4e8956e28bf82 to your computer and use it in GitHub Desktop.
from diffusers import StableDiffusionXLPipeline
from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl
from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl_2p
from sd_embed.prompt_parser import parse_prompt_attention
from diffusers import DiffusionPipeline,EulerAncestralDiscreteScheduler
from transformers import CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer
from diffusers.utils import make_image_grid
import torch
MODEL_PATH = "models/colorfulxl"
pipe = StableDiffusionXLPipeline.from_pretrained(
MODEL_PATH,
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True
).to('cuda')
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(
pipe.scheduler.config,
)
#pipe.to('cuda')
prompt = "柴犬、カラフルアート"
prompt = "The image captures a vibrant scene from a bustling street. The perspective is from a pedestrian's viewpoint on the sidewalk, immersing the viewer in the city's daily life. The street is lined with a variety of buildings, their architecture hinting at the rich history of the city. Among these structures, a church with a tall bell tower stands out, its presence adding a sense of grandeur to the scene.People are seen walking on the sidewalk, going about their day, adding a dynamic element to the otherwise static urban landscape.The colors in the image are predominantly blue, yellow, and green, reflecting the lively atmosphere of the city. The sky above is a clear blue, suggesting a sunny day, which further enhances the overall vibrancy of the scene."
#prompt = "The image captures a serene scene on a river. Dominating the foreground is a wooden boat, its brown hue contrasting with the greenish-blue of the water. The boat is not alone in the frame. In the background, a red-roofed building can be seen, its white walls standing out against the greenery. The building is partially obscured by trees, adding an element of mystery to the scene. The sky above is overcast, casting a soft light over the entire scene. Despite this, there's a sense of tranquility that pervades the image, as if inviting the viewer to take a moment and appreciate the peacefulness of the scene. There's no text or discernible action in the image, just a snapshot of a moment, frozen in time."
#prompt = "A whimsical and creative image depicting a hybrid creature that is a mix of a waffle and a hippopotamus. This imaginative creature features the distinctive, bulky body of a hippo, but with a texture and appearance resembling a golden-brown, crispy waffle. The creature might have elements like waffle squares across its skin and a syrup-like sheen. It's set in a surreal environment that playfully combines a natural water habitat of a hippo with elements of a breakfast table setting, possibly including oversized utensils or plates in the background. The image should evoke a sense of playful absurdity and culinary fantasy."
#prompt = "Studio Ghibli ~ Hayao Miyazaki ~ Spirited Away ~ beautiful ultra detailed illustration of a cute witch of the sitting on a tree stump reading a book, her ornate robe reminiscent of the stars in the night sky. This contrast between the fantastical character and the more bold color scheme and elements gives the piece an intriguing narrative quality. painted realism, photorealistic, 8k, fantasy digital art, HDR, UHD."
prompt = "In a dark forest stands an old abandoned castle on the top of a hill, surrounded by dense trees with branches hanging down to the ground, which hide it from prying eyes with their gloomy cover of moss-lichen... This castle is inhabited by a spirit of revenge - the ghost of a woman named Lilith, who was brutally murdered here many centuries ago for her forbidden desires for power over people's lives!"
#prompt = "In the dark blue forest, where the aspen trees tremble, where the foliage flies from the sorcerer oaks, in the clearing, the hares mowed the grass at midnight And at the same time they chanted strange words"
#prompt = "a composition of dark flowers with sharply shaped leaves and plant stems around a central character - it could be a vampire girl or a witch with long, waist-length black hair with purple hues; she stands in front of an old abandoned castle without windows but surrounded by bright flowers of a red hue symbolizing blood or suffering; "
#prompt = "1girl, solo, animal ears, bow, teeth, jacket, tail, open mouth, brown hair, orange background, bowtie, orange nails, simple background, cat ears, orange eyes, blue bow, animal ear fluff, cat tail, looking at viewer, upper body, shirt, school uniform, hood, striped bow, striped, white shirt, black jacket, blue bowtie, fingernails, long sleeves, cat girl, bangs, fangs, collared shirt, striped bowtie, short hair, tongue, hoodie, sharp teeth, facial mark, claw pose,"
#prompt = "a beautiful 21yo woman with (a little white dragon in her arms), surrounded by white clouds, looking away, piercing eyes, fairyland at background, detailed face, detailed skin, dream, ultra wide angle, centered,"
negative_prompt = ""
#prompt = prompt.replace("...", ".")
#prompt = prompt.replace("!", ".")
#prompt = prompt.replace(".", "\n")
generator = torch.Generator(device="cuda").manual_seed(46)
pipe.enable_vae_slicing()
#pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
prompt_embeds, prompt_neg_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds = get_weighted_text_embeddings_sdxl_2p(
pipe = pipe
, prompt = prompt
, prompt_2 = ""
, neg_prompt = negative_prompt
, neg_prompt_2 = ""
)
image = pipe(
prompt_embeds = prompt_embeds
, negative_prompt_embeds = prompt_neg_embeds
, pooled_prompt_embeds = pooled_prompt_embeds
, negative_pooled_prompt_embeds = negative_pooled_prompt_embeds,
num_inference_steps=24,
guidance_scale=2,
generator=generator,
#num_images_per_prompt=2
).images[0]
(
prompt_embeds
, prompt_neg_embeds
, pooled_prompt_embeds
, negative_pooled_prompt_embeds
) = get_weighted_text_embeddings_sdxl(
pipe
, prompt = prompt
, neg_prompt = negative_prompt
)
image2 = pipe(
prompt_embeds=prompt_embeds,
pooled_prompt_embeds=pooled_prompt_embeds,
negative_prompt_embeds=prompt_neg_embeds,
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
num_inference_steps=24,
guidance_scale=2,
generator=generator,
#num_images_per_prompt=2
).images[0]
grid = make_image_grid([image, image2], rows=1, cols=2)
grid.save("sample.png")
display(grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment