multimodalart HF staff commited on
Commit
483e45c
1 Parent(s): b0ea15a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -21
app.py CHANGED
@@ -3,17 +3,68 @@ import numpy as np
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
 
 
 
 
 
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
- if torch.cuda.is_available():
10
- torch.cuda.max_memory_allocated(device=device)
11
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
- pipe.enable_xformers_memory_efficient_attention()
13
- pipe = pipe.to(device)
14
- else:
15
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
- pipe = pipe.to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 1024
@@ -32,7 +83,8 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
32
  num_inference_steps = num_inference_steps,
33
  width = width,
34
  height = height,
35
- generator = generator
 
36
  ).images[0]
37
 
38
  return image
@@ -50,17 +102,11 @@ css="""
50
  }
51
  """
52
 
53
- if torch.cuda.is_available():
54
- power_device = "GPU"
55
- else:
56
- power_device = "CPU"
57
-
58
  with gr.Blocks(css=css) as demo:
59
 
60
  with gr.Column(elem_id="col-container"):
61
  gr.Markdown(f"""
62
- # Text-to-Image Gradio Template
63
- Currently running on {power_device}.
64
  """)
65
 
66
  with gr.Row():
@@ -103,7 +149,7 @@ with gr.Blocks(css=css) as demo:
103
  minimum=256,
104
  maximum=MAX_IMAGE_SIZE,
105
  step=32,
106
- value=512,
107
  )
108
 
109
  height = gr.Slider(
@@ -111,7 +157,7 @@ with gr.Blocks(css=css) as demo:
111
  minimum=256,
112
  maximum=MAX_IMAGE_SIZE,
113
  step=32,
114
- value=512,
115
  )
116
 
117
  with gr.Row():
@@ -121,15 +167,15 @@ with gr.Blocks(css=css) as demo:
121
  minimum=0.0,
122
  maximum=10.0,
123
  step=0.1,
124
- value=0.0,
125
  )
126
 
127
  num_inference_steps = gr.Slider(
128
  label="Number of inference steps",
129
  minimum=1,
130
- maximum=12,
131
  step=1,
132
- value=2,
133
  )
134
 
135
  gr.Examples(
 
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
+ from huggingface_hub import hf_hub_download
7
+ from safetensors.torch import load_file
8
+ from cog_sdxl.dataset_and_utils import TokenEmbeddingsHandler
9
+ from cog_sdxl.no_init import no_init_or_tensor
10
+ from diffusers.models.attention_processor import LoRAAttnProcessor2_0
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
 
14
+ pipe = DiffusionPipeline.from_pretrained(
15
+ "stabilityai/stable-diffusion-xl-base-1.0",
16
+ torch_dtype=torch.float16,
17
+ use_safetensors=True,
18
+ variant="fp16",
19
+ ).to(device)
20
+
21
+ unet = pipe.unet
22
+
23
+ lora_path = hf_hub_download(repo_id="SvenN/sdxl-emoji", filename="lora.safetensors", repo_type="model")
24
+ embeddings_path = hf_hub_download(repo_id="SvenN/sdxl-emoji", filename="embeddings.pti", repo_type="model")
25
+
26
+ tensors = load_file(lora_path)
27
+ unet_lora_attn_procs = {}
28
+ name_rank_map = {}
29
+ for tk, tv in tensors.items():
30
+ # up is N, d
31
+ tensors[tk] = tv.half()
32
+ if tk.endswith("up.weight"):
33
+ proc_name = ".".join(tk.split(".")[:-3])
34
+ r = tv.shape[1]
35
+ name_rank_map[proc_name] = r
36
+
37
+ for name, attn_processor in unet.attn_processors.items():
38
+ cross_attention_dim = (
39
+ None
40
+ if name.endswith("attn1.processor")
41
+ else unet.config.cross_attention_dim
42
+ )
43
+ if name.startswith("mid_block"):
44
+ hidden_size = unet.config.block_out_channels[-1]
45
+ elif name.startswith("up_blocks"):
46
+ block_id = int(name[len("up_blocks.")])
47
+ hidden_size = list(reversed(unet.config.block_out_channels))[
48
+ block_id
49
+ ]
50
+ elif name.startswith("down_blocks"):
51
+ block_id = int(name[len("down_blocks.")])
52
+ hidden_size = unet.config.block_out_channels[block_id]
53
+ with no_init_or_tensor():
54
+ module = LoRAAttnProcessor2_0(
55
+ hidden_size=hidden_size,
56
+ cross_attention_dim=cross_attention_dim,
57
+ rank=name_rank_map[name],
58
+ ).half()
59
+ unet_lora_attn_procs[name] = module.to("cuda", non_blocking=True)
60
+
61
+ unet.set_attn_processor(unet_lora_attn_procs)
62
+ unet.load_state_dict(tensors, strict=False)
63
+
64
+ handler = TokenEmbeddingsHandler(
65
+ [pipe.text_encoder, pipe.text_encoder_2], [pipe.tokenizer, pipe.tokenizer_2]
66
+ )
67
+ handler.load_embeddings(embeddings_path)
68
 
69
  MAX_SEED = np.iinfo(np.int32).max
70
  MAX_IMAGE_SIZE = 1024
 
83
  num_inference_steps = num_inference_steps,
84
  width = width,
85
  height = height,
86
+ generator = generator,
87
+ cross_attention_kwargs={"scale": 0.6},
88
  ).images[0]
89
 
90
  return image
 
102
  }
103
  """
104
 
 
 
 
 
 
105
  with gr.Blocks(css=css) as demo:
106
 
107
  with gr.Column(elem_id="col-container"):
108
  gr.Markdown(f"""
109
+ # SDXL Emoji running on diffusers 0.25.0
 
110
  """)
111
 
112
  with gr.Row():
 
149
  minimum=256,
150
  maximum=MAX_IMAGE_SIZE,
151
  step=32,
152
+ value=1024,
153
  )
154
 
155
  height = gr.Slider(
 
157
  minimum=256,
158
  maximum=MAX_IMAGE_SIZE,
159
  step=32,
160
+ value=1024,
161
  )
162
 
163
  with gr.Row():
 
167
  minimum=0.0,
168
  maximum=10.0,
169
  step=0.1,
170
+ value=7.5,
171
  )
172
 
173
  num_inference_steps = gr.Slider(
174
  label="Number of inference steps",
175
  minimum=1,
176
+ maximum=50,
177
  step=1,
178
+ value=50,
179
  )
180
 
181
  gr.Examples(