File size: 1,355 Bytes
96e0b44
 
 
 
 
 
 
 
 
 
188f9f7
 
96e0b44
 
 
 
 
 
 
 
 
 
188f9f7
 
96e0b44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import gradio as gr
import numpy as np

from diffusers import DiffusionPipeline
import torch

# load both base & refiner
base = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True, cache_dir="./local_model_cache"
)
# base.enable_model_cpu_offload()
base.to("cpu")

refiner = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-refiner-1.0",
    text_encoder_2=base.text_encoder_2,
    vae=base.vae,
    torch_dtype=torch.float16,
    use_safetensors=True,
    variant="fp16",
    cache_dir="./local_model_cache"
)
# refiner.enable_model_cpu_offload()
refiner.to("cpu")

# Define how many steps and what % of steps to be run on each experts (80/20) here
n_steps = 40
high_noise_frac = 0.8

def create_image(prompt):
    # run both experts
    # torch.cuda.empty_cache()
    image = base(
        prompt=prompt,
        num_inference_steps=n_steps,
        denoising_end=high_noise_frac,
        output_type="latent",
    ).images
    image = refiner(
        prompt=prompt,
        num_inference_steps=n_steps,
        denoising_start=high_noise_frac,
        image=image,
    ).images[0]
    # torch.cuda.empty_cache()
    return image


demo = gr.Interface(
    fn=create_image,
    inputs=["text"],
    outputs=["image"],
)

demo.launch()