Spaces:
Running
on
Zero
Running
on
Zero
philipp-zettl
commited on
Commit
•
922fdb6
1
Parent(s):
13518e4
use compel for prompt embeddings
Browse files- app.py +13 -28
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
import torch
|
|
|
4 |
from diffusers import DiffusionPipeline
|
5 |
|
6 |
|
@@ -11,39 +12,23 @@ pipe = DiffusionPipeline.from_pretrained(
|
|
11 |
)
|
12 |
pipe.to('cuda')
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
negative_ids = pipe.tokenizer(
|
22 |
-
negative_prompt or "",
|
23 |
-
truncation=False,
|
24 |
-
padding="max_length",
|
25 |
-
max_length=input_ids.shape[-1],
|
26 |
-
return_tensors="pt"
|
27 |
-
).input_ids
|
28 |
-
negative_ids = negative_ids.to("cuda")
|
29 |
-
|
30 |
-
concat_embeds = []
|
31 |
-
neg_embeds = []
|
32 |
-
for i in range(0, input_ids.shape[-1], max_length):
|
33 |
-
concat_embeds.append(pipe.text_encoder(input_ids[:, i: i + max_length])[0])
|
34 |
-
neg_embeds.append(pipe.text_encoder(negative_ids[:, i: i + max_length])[0])
|
35 |
-
|
36 |
-
prompt_embeds = torch.cat(concat_embeds, dim=1)
|
37 |
-
negative_prompt_embeds = torch.cat(neg_embeds, dim=1)
|
38 |
-
return prompt_embeds, negative_prompt_embeds
|
39 |
|
40 |
|
41 |
@spaces.GPU
|
42 |
def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples):
|
43 |
-
|
|
|
44 |
return pipe(
|
45 |
-
prompt_embeds=
|
46 |
-
|
|
|
|
|
47 |
num_inference_steps=num_inference_steps,
|
48 |
guidance_scale=guidance_scale,
|
49 |
width=width,
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
import torch
|
4 |
+
from compel import Compel, ReturnedEmbeddingsType
|
5 |
from diffusers import DiffusionPipeline
|
6 |
|
7 |
|
|
|
12 |
)
|
13 |
pipe.to('cuda')
|
14 |
|
15 |
+
compel = Compel(
|
16 |
+
tokenizer=[pipe.tokenizer, pipe.tokenizer_2] ,
|
17 |
+
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
18 |
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
19 |
+
requires_pooled=[False, True]
|
20 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
@spaces.GPU
|
24 |
def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples):
|
25 |
+
embeds, pooled = compel(prompt)
|
26 |
+
neg_embeds, neg_pooled = compel(negative_prompt)
|
27 |
return pipe(
|
28 |
+
prompt_embeds=embeds,
|
29 |
+
pooled_prompt_embeds=pooled,
|
30 |
+
negative_prompt_embeds=neg_embeds,
|
31 |
+
negative_pooled_prompt_embeds=neg_pooled,
|
32 |
num_inference_steps=num_inference_steps,
|
33 |
guidance_scale=guidance_scale,
|
34 |
width=width,
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ diffusers
|
|
3 |
invisible_watermark
|
4 |
torch
|
5 |
transformers
|
6 |
-
xformers
|
|
|
|
3 |
invisible_watermark
|
4 |
torch
|
5 |
transformers
|
6 |
+
xformers
|
7 |
+
compel
|