Spaces:
Sleeping
Sleeping
first
Browse files- app.py +31 -38
- requirements.txt +1 -2
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
|
2 |
-
from compel import Compel, ReturnedEmbeddingsType
|
3 |
import torch
|
4 |
import os
|
5 |
|
@@ -12,7 +11,7 @@ from PIL import Image
|
|
12 |
import numpy as np
|
13 |
import gradio as gr
|
14 |
import psutil
|
15 |
-
|
16 |
|
17 |
SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", None)
|
18 |
TORCH_COMPILE = os.environ.get("TORCH_COMPILE", None)
|
@@ -35,16 +34,15 @@ if mps_available:
|
|
35 |
torch_device = "cpu"
|
36 |
torch_dtype = torch.float32
|
37 |
|
38 |
-
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
39 |
-
|
40 |
if SAFETY_CHECKER == "True":
|
41 |
-
pipe = DiffusionPipeline.from_pretrained(
|
42 |
else:
|
43 |
-
pipe = DiffusionPipeline.from_pretrained(
|
44 |
|
45 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
46 |
pipe.to(device=torch_device, dtype=torch_dtype).to(device)
|
47 |
pipe.unet.to(memory_format=torch.channels_last)
|
|
|
48 |
|
49 |
# check if computer has less than 64GB of RAM using sys or os
|
50 |
if psutil.virtual_memory().total < 64 * 1024**3:
|
@@ -53,47 +51,35 @@ if psutil.virtual_memory().total < 64 * 1024**3:
|
|
53 |
if TORCH_COMPILE:
|
54 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
55 |
pipe.vae = torch.compile(pipe.vae, mode="reduce-overhead", fullgraph=True)
|
56 |
-
|
57 |
pipe(prompt="warmup", num_inference_steps=1, guidance_scale=8.0)
|
58 |
|
59 |
# Load LCM LoRA
|
60 |
-
pipe.load_lora_weights(
|
61 |
-
|
62 |
-
use_auth_token=HF_TOKEN,
|
63 |
-
)
|
64 |
-
|
65 |
-
compel_proc = Compel(
|
66 |
-
tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
|
67 |
-
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
68 |
-
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
69 |
-
requires_pooled=[False, True],
|
70 |
-
)
|
71 |
|
72 |
|
73 |
-
def predict(
|
74 |
-
prompt, guidance, steps, seed=1231231, progress=gr.Progress(track_tqdm=True)
|
75 |
-
):
|
76 |
generator = torch.manual_seed(seed)
|
77 |
-
|
78 |
-
|
79 |
results = pipe(
|
80 |
-
|
81 |
-
pooled_prompt_embeds=pooled_prompt_embeds,
|
82 |
generator=generator,
|
83 |
num_inference_steps=steps,
|
84 |
guidance_scale=guidance,
|
85 |
-
width=
|
86 |
-
height=
|
87 |
# original_inference_steps=params.lcm_steps,
|
88 |
output_type="pil",
|
89 |
)
|
|
|
90 |
nsfw_content_detected = (
|
91 |
results.nsfw_content_detected[0]
|
92 |
if "nsfw_content_detected" in results
|
93 |
else False
|
94 |
)
|
95 |
if nsfw_content_detected:
|
96 |
-
|
|
|
97 |
return results.images[0]
|
98 |
|
99 |
|
@@ -111,8 +97,8 @@ css = """
|
|
111 |
with gr.Blocks(css=css) as demo:
|
112 |
with gr.Column(elem_id="container"):
|
113 |
gr.Markdown(
|
114 |
-
"""#
|
115 |
-
|
116 |
""",
|
117 |
elem_id="intro",
|
118 |
)
|
@@ -122,7 +108,7 @@ with gr.Blocks(css=css) as demo:
|
|
122 |
placeholder="Insert your prompt here:", scale=5, container=False
|
123 |
)
|
124 |
generate_bt = gr.Button("Generate", scale=1)
|
125 |
-
|
126 |
image = gr.Image(type="filepath")
|
127 |
with gr.Accordion("Advanced options", open=False):
|
128 |
guidance = gr.Slider(
|
@@ -133,16 +119,18 @@ with gr.Blocks(css=css) as demo:
|
|
133 |
randomize=True, minimum=0, maximum=12013012031030, label="Seed", step=1
|
134 |
)
|
135 |
with gr.Accordion("Run with diffusers"):
|
136 |
-
gr.Markdown(
|
|
|
137 |
```bash
|
138 |
pip install diffusers==0.23.0
|
139 |
```
|
140 |
|
141 |
```py
|
142 |
-
from diffusers import DiffusionPipeline, LCMScheduler
|
143 |
-
|
|
|
144 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
145 |
-
pipe.load_lora_weights("latent-consistency/lcm-lora-
|
146 |
|
147 |
results = pipe(
|
148 |
prompt="The spirit of a tamagotchi wandering in the city of Vienna",
|
@@ -151,10 +139,15 @@ with gr.Blocks(css=css) as demo:
|
|
151 |
)
|
152 |
results.images[0]
|
153 |
```
|
154 |
-
|
155 |
-
|
|
|
156 |
inputs = [prompt, guidance, steps, seed]
|
157 |
-
generate_bt.click(fn=predict, inputs=inputs, outputs=image)
|
|
|
|
|
|
|
|
|
158 |
|
159 |
demo.queue()
|
160 |
demo.launch()
|
|
|
1 |
from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
|
|
|
2 |
import torch
|
3 |
import os
|
4 |
|
|
|
11 |
import numpy as np
|
12 |
import gradio as gr
|
13 |
import psutil
|
14 |
+
import time
|
15 |
|
16 |
SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", None)
|
17 |
TORCH_COMPILE = os.environ.get("TORCH_COMPILE", None)
|
|
|
34 |
torch_device = "cpu"
|
35 |
torch_dtype = torch.float32
|
36 |
|
|
|
|
|
37 |
if SAFETY_CHECKER == "True":
|
38 |
+
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7")
|
39 |
else:
|
40 |
+
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7", safety_checker=None)
|
41 |
|
42 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
43 |
pipe.to(device=torch_device, dtype=torch_dtype).to(device)
|
44 |
pipe.unet.to(memory_format=torch.channels_last)
|
45 |
+
pipe.set_progress_bar_config(disable=True)
|
46 |
|
47 |
# check if computer has less than 64GB of RAM using sys or os
|
48 |
if psutil.virtual_memory().total < 64 * 1024**3:
|
|
|
51 |
if TORCH_COMPILE:
|
52 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
53 |
pipe.vae = torch.compile(pipe.vae, mode="reduce-overhead", fullgraph=True)
|
|
|
54 |
pipe(prompt="warmup", num_inference_steps=1, guidance_scale=8.0)
|
55 |
|
56 |
# Load LCM LoRA
|
57 |
+
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
|
58 |
+
pipe.fuse_lora()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
|
61 |
+
def predict(prompt, guidance, steps, seed=1231231):
|
|
|
|
|
62 |
generator = torch.manual_seed(seed)
|
63 |
+
last_time = time.time()
|
|
|
64 |
results = pipe(
|
65 |
+
prompt=prompt,
|
|
|
66 |
generator=generator,
|
67 |
num_inference_steps=steps,
|
68 |
guidance_scale=guidance,
|
69 |
+
width=512,
|
70 |
+
height=512,
|
71 |
# original_inference_steps=params.lcm_steps,
|
72 |
output_type="pil",
|
73 |
)
|
74 |
+
print(f"Pipe took {time.time() - last_time} seconds")
|
75 |
nsfw_content_detected = (
|
76 |
results.nsfw_content_detected[0]
|
77 |
if "nsfw_content_detected" in results
|
78 |
else False
|
79 |
)
|
80 |
if nsfw_content_detected:
|
81 |
+
gr.Warning("NSFW content detected.")
|
82 |
+
return Image.new("RGB", (512, 512))
|
83 |
return results.images[0]
|
84 |
|
85 |
|
|
|
97 |
with gr.Blocks(css=css) as demo:
|
98 |
with gr.Column(elem_id="container"):
|
99 |
gr.Markdown(
|
100 |
+
"""# SD1.5 Latent Consistency LoRAs
|
101 |
+
SD1.5 is loaded with a LCM-LoRA, giving it the super power of doing inference in as little as 4 steps. [Learn more on our blog](#) or [technical report](#).
|
102 |
""",
|
103 |
elem_id="intro",
|
104 |
)
|
|
|
108 |
placeholder="Insert your prompt here:", scale=5, container=False
|
109 |
)
|
110 |
generate_bt = gr.Button("Generate", scale=1)
|
111 |
+
|
112 |
image = gr.Image(type="filepath")
|
113 |
with gr.Accordion("Advanced options", open=False):
|
114 |
guidance = gr.Slider(
|
|
|
119 |
randomize=True, minimum=0, maximum=12013012031030, label="Seed", step=1
|
120 |
)
|
121 |
with gr.Accordion("Run with diffusers"):
|
122 |
+
gr.Markdown(
|
123 |
+
"""## Running LCM-LoRAs it with `diffusers`
|
124 |
```bash
|
125 |
pip install diffusers==0.23.0
|
126 |
```
|
127 |
|
128 |
```py
|
129 |
+
from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
|
130 |
+
|
131 |
+
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda")
|
132 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
133 |
+
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA
|
134 |
|
135 |
results = pipe(
|
136 |
prompt="The spirit of a tamagotchi wandering in the city of Vienna",
|
|
|
139 |
)
|
140 |
results.images[0]
|
141 |
```
|
142 |
+
"""
|
143 |
+
)
|
144 |
+
|
145 |
inputs = [prompt, guidance, steps, seed]
|
146 |
+
generate_bt.click(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
147 |
+
prompt.input(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
148 |
+
guidance.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
149 |
+
steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
150 |
+
seed.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
151 |
|
152 |
demo.queue()
|
153 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
|
2 |
-
git+https://github.com/huggingface/diffusers.git@6110d7c95f630479cf01340cc8a8141c1e359f09
|
3 |
transformers==4.34.1
|
4 |
gradio==4.1.2
|
5 |
--extra-index-url https://download.pytorch.org/whl/cu121
|
|
|
1 |
+
diffusers==0.23.0
|
|
|
2 |
transformers==4.34.1
|
3 |
gradio==4.1.2
|
4 |
--extra-index-url https://download.pytorch.org/whl/cu121
|