Spaces:
Runtime error
Runtime error
from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny | |
import torch | |
import os | |
try: | |
import intel_extension_for_pytorch as ipex | |
except: | |
pass | |
from PIL import Image | |
import numpy as np | |
import gradio as gr | |
import psutil | |
import time | |
from sfast.compilers.stable_diffusion_pipeline_compiler import ( | |
compile, | |
CompilationConfig, | |
) | |
SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", None) | |
TORCH_COMPILE = os.environ.get("TORCH_COMPILE", None) | |
HF_TOKEN = os.environ.get("HF_TOKEN", None) | |
# check if MPS is available OSX only M1/M2/M3 chips | |
mps_available = hasattr(torch.backends, "mps") and torch.backends.mps.is_available() | |
xpu_available = hasattr(torch, "xpu") and torch.xpu.is_available() | |
device = torch.device( | |
"cuda" if torch.cuda.is_available() else "xpu" if xpu_available else "cpu" | |
) | |
torch_device = device | |
torch_dtype = torch.float16 | |
print(f"SAFETY_CHECKER: {SAFETY_CHECKER}") | |
print(f"TORCH_COMPILE: {TORCH_COMPILE}") | |
print(f"device: {device}") | |
if mps_available: | |
device = torch.device("mps") | |
torch_device = "cpu" | |
torch_dtype = torch.float32 | |
if SAFETY_CHECKER == "True": | |
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7") | |
else: | |
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7", safety_checker=None) | |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config) | |
pipe.unet.to(memory_format=torch.channels_last) | |
pipe.set_progress_bar_config(disable=True) | |
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") | |
pipe.fuse_lora() | |
pipe.to(device=torch_device, dtype=torch_dtype).to(device) | |
config = CompilationConfig.Default() | |
config.enable_xformers = True | |
config.enable_triton = True | |
config.enable_cuda_graph = True | |
pipe = compile(pipe, config=config) | |
def predict(prompt, guidance, steps, seed=1231231): | |
generator = torch.manual_seed(seed) | |
last_time = time.time() | |
results = pipe( | |
prompt=prompt, | |
generator=generator, | |
num_inference_steps=steps, | |
guidance_scale=guidance, | |
width=512, | |
height=512, | |
# original_inference_steps=params.lcm_steps, | |
output_type="pil", | |
) | |
print(f"Pipe took {time.time() - last_time} seconds") | |
nsfw_content_detected = ( | |
results.nsfw_content_detected[0] | |
if "nsfw_content_detected" in results | |
else False | |
) | |
if nsfw_content_detected: | |
gr.Warning("NSFW content detected.") | |
return Image.new("RGB", (512, 512)) | |
return results.images[0] | |
css = """ | |
#container{ | |
margin: 0 auto; | |
max-width: 40rem; | |
} | |
#intro{ | |
max-width: 100%; | |
text-align: center; | |
margin: 0 auto; | |
} | |
#container{ | |
margin: 0 auto; | |
max-width: 40rem; | |
} | |
#intro{ | |
max-width: 100%; | |
text-align: center; | |
margin: 0 auto; | |
} | |
div.svelte-vt1mxs { | |
display: flex; | |
position: relative; | |
flex-direction: column | |
} | |
div.svelte-vt1mxs>*,div.svelte-vt1mxs>.form > * { | |
width: var(--size-full) | |
} | |
.gap.svelte-vt1mxs { | |
gap: var(--layout-gap) | |
} | |
.hide.svelte-vt1mxs { | |
display: none | |
} | |
.compact.svelte-vt1mxs>*,.compact.svelte-vt1mxs .box { | |
border-radius: 0 | |
} | |
.compact.svelte-vt1mxs,.panel.svelte-vt1mxs { | |
border: solid var(--panel-border-width) var(--panel-border-color); | |
border-radius: var(--container-radius); | |
background: var(--panel-background-fill); | |
padding: var(--spacing-lg) | |
} | |
div#component-24 { | |
display: none; | |
} | |
div#component-8 {background: #00000024;border: 0;color: #ffffff;backdrop-filter: blur(20px);-webkit-backdrop-filter: blur(20px);border-width: 0 !important;} | |
span.md.svelte-9tftx4 { | |
display: none; | |
} | |
.empty.svelte-lk9eg8.large.unpadded_box { | |
background: none !important; | |
} | |
div#component-26 { | |
display: none; | |
} | |
div#component-7 { | |
background: none; | |
} | |
.wrap.default.full.svelte-119qaqt.hide { | |
background: none !important; | |
} | |
.styler.svelte-iyf88w { | |
background: none !important; | |
} | |
div#component-3 { | |
background: none !important; | |
border: 0; | |
} | |
input.scroll-hide.svelte-1f354aw { | |
overflow: hidden !important; | |
} | |
div#component-5 { | |
border-radius: 40px 0px 0px 40px; | |
background: black !important; | |
opacity: 0.9; | |
} | |
#component-6 { | |
border-radius: 0px 40px 40px 0px; | |
background: linear-gradient(358deg, #ff4d0080, #fff0); | |
color: #ffffffe3; | |
border: 2px #ffffffc2 dashed; | |
border-left: 0; | |
font-size: 30px; | |
letter-spacing:-1px; | |
position: relative; | |
z-index: 1; | |
backdrop-filter: blur(18px); | |
-webkit-backdrop-filter: blur(18px); | |
} | |
div#component-0 { | |
max-width: 100% !important; | |
} | |
.grid-wrap.svelte-1b19cri.fixed-height { | |
max-height: 100% !important; | |
overflow: auto; | |
} | |
footer.svelte-1ax1toq { | |
display: none !important; | |
} | |
input.scroll-hide.svelte-1f354aw { | |
font-size: 26px; | |
padding: 25px; | |
} | |
div#component-4 { | |
margin-top: 230px; | |
margin-bottom: 30px; | |
} | |
gradio-app { | |
background-color: transparent !important; | |
background: url(https://vivawaves.com/wavesweaveslogo.svg) top center no-repeat !important; | |
margin-top: 77px; | |
} | |
label.svelte-1f354aw { | |
} | |
.styler.svelte-iyf88w { | |
} | |
body { | |
background: url(https://vivawaves.com/vivatodaybg2.jpg); | |
background-size: cover; | |
} | |
img.svelte-1b19cri {} | |
.preview.svelte-1b19cri { | |
background: #0000004d !important; | |
border-radius: 20px; | |
padding: 20px; | |
overflow: hidden; | |
} | |
button.svelte-1030q2h { | |
border-radius: 100%; | |
} | |
div.svelte-1030q2h svg { | |
} | |
svg path { | |
} | |
img.svelte-1b19cri { | |
border-radius: 10px; | |
} | |
.form.svelte-sfqy0y { | |
background: #fff0; | |
border-width: 0px; | |
opacity: 0.8; | |
} | |
.gradio-container-3-44-2,.gradio-container-3-44-2 *,.gradio-container-3-44-2 :before,.gradio-container-3-44-2 :after { | |
box-sizing: border-box; | |
border-width: 0; | |
border-style: solid; | |
} | |
div#component-13 { | |
display: none; | |
} | |
footer.svelte-mpyp5e { | |
display: none !important; | |
} | |
div#intro { | |
display: none; | |
} | |
div.svelte-15lo0d8 { | |
display: flex; | |
flex-wrap: wrap; | |
gap: 0; | |
width: var(--size-full); | |
flex-direction: initial; | |
justify-content: center; | |
align-items: baseline; | |
} | |
input.svelte-1f354aw.svelte-1f354aw, textarea.svelte-1f354aw.svelte-1f354aw { | |
display: block; | |
position: relative; | |
outline: none !important; | |
box-shadow: var(--input-shadow); | |
background: var(--input-background-fill); | |
padding: var(--input-padding); | |
width: 100%; | |
color: var(--body-text-color); | |
font-weight: var(--input-text-weight); | |
font-size: large; | |
line-height: initial; | |
border: none; | |
text-size-adjust: auto; | |
font-size: 23px !important; | |
} | |
div#component-24 { | |
display: none; | |
} | |
div#component-8 {background: #00000024;border: 0;color: #ffffff;backdrop-filter: blur(20px);-webkit-backdrop-filter: blur(20px);border-width: 0 !important;} | |
span.md.svelte-9tftx4 { | |
display: none; | |
} | |
.empty.svelte-lk9eg8.large.unpadded_box { | |
background: none !important; | |
} | |
div#component-26 { | |
display: none; | |
} | |
div#component-7 { | |
background: none; | |
} | |
.wrap.default.full.svelte-119qaqt.hide { | |
background: none !important; | |
} | |
.styler.svelte-iyf88w { | |
background: none !important; | |
} | |
div#component-3 { | |
background: none !important; | |
border: 0; | |
} | |
input.scroll-hide.svelte-1f354aw { | |
overflow: hidden !important; | |
} | |
div#component-5 { | |
border-radius: 40px; | |
background: white !important; | |
opacity: 0.9; | |
} | |
#component-6 { | |
border-radius: 0px 40px 40px 0px; | |
background: linear-gradient(358deg, #ff4d0080, #fff0); | |
color: #ffffffe3; | |
border: 2px #ffffffc2 dashed; | |
border-left: 0; | |
font-size: 30px; | |
letter-spacing:-1px; | |
position: relative; | |
z-index: 1; | |
backdrop-filter: blur(18px); | |
-webkit-backdrop-filter: blur(18px); | |
display: none; | |
} | |
div#component-0 { | |
max-width: 100% !important; | |
} | |
.grid-wrap.svelte-1b19cri.fixed-height { | |
max-height: 100% !important; | |
overflow: auto; | |
} | |
footer.svelte-1ax1toq { | |
display: none !important; | |
} | |
input.scroll-hide.svelte-1f354aw { | |
font-size: 26px; | |
padding: 25px; | |
} | |
div#component-4 { | |
margin-top: 230px; | |
margin-bottom: 30px; | |
} | |
gradio-app { | |
background-color: transparent !important; | |
background: url(https://vivawaves.com/wavesweaveslogo.svg) top center no-repeat !important; | |
margin-top: 77px; | |
} | |
label.svelte-1f354aw { | |
} | |
.styler.svelte-iyf88w { | |
} | |
body { | |
background: url(https://vivawaves.com/vivatodaybg2.jpg); | |
background-size: cover; | |
} | |
img.svelte-1b19cri {} | |
.preview.svelte-1b19cri { | |
background: #0000004d !important; | |
border-radius: 20px; | |
padding: 20px; | |
overflow: hidden; | |
} | |
button.svelte-1030q2h { | |
border-radius: 100%; | |
} | |
div.svelte-1030q2h svg { | |
} | |
svg path { | |
} | |
img.svelte-1b19cri { | |
border-radius: 10px; | |
} | |
.form.svelte-sfqy0y { | |
background: #fff0; | |
border-width: 0px; | |
opacity: 0.8; | |
} | |
.gradio-container-3-44-2,.gradio-container-3-44-2 *,.gradio-container-3-44-2 :before,.gradio-container-3-44-2 :after { | |
box-sizing: border-box; | |
border-width: 0; | |
border-style: solid; | |
} | |
div#component-13 { | |
display: none; | |
} | |
footer.svelte-mpyp5e { | |
display: none !important; | |
} | |
div#intro { | |
display: none; | |
} | |
div.svelte-15lo0d8 { | |
display: flex; | |
flex-wrap: wrap; | |
gap: 0 !important; | |
width: var(--size-full); | |
flex-direction: initial; | |
justify-content: center; | |
align-items: baseline; | |
} | |
input.svelte-1f354aw.svelte-1f354aw, textarea.svelte-1f354aw.svelte-1f354aw { | |
display: block; | |
position: relative; | |
outline: none !important; | |
box-shadow: var(--input-shadow); | |
background: var(--input-background-fill); | |
padding: var(--input-padding); | |
width: 100%; | |
color: var(--body-text-color); | |
font-weight: var(--input-text-weight); | |
font-size: large; | |
line-height: initial; | |
border: none; | |
text-size-adjust: auto; | |
font-size: 23px !important; | |
border-radius: 30px; | |
background: white !important; | |
text-align: center; | |
} | |
div#component-8 { | |
margin-bottom: 70px; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
with gr.Column(elem_id="container"): | |
gr.Markdown( | |
"""# SD1.5 Latent Consistency LoRAs | |
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](#). | |
""", | |
elem_id="intro", | |
) | |
with gr.Row(): | |
with gr.Row(): | |
prompt = gr.Textbox( | |
placeholder="Insert your prompt here:", scale=5, container=False | |
) | |
generate_bt = gr.Button("Generate", scale=1) | |
image = gr.Image(type="filepath") | |
with gr.Accordion("Advanced options", open=False): | |
guidance = gr.Slider( | |
label="Guidance", minimum=0.0, maximum=5, value=0.3, step=0.001 | |
) | |
steps = gr.Slider(label="Steps", value=4, minimum=2, maximum=10, step=1) | |
seed = gr.Slider( | |
randomize=True, minimum=0, maximum=12013012031030, label="Seed", step=1 | |
) | |
with gr.Accordion("Run with diffusers"): | |
gr.Markdown( | |
"""## Running LCM-LoRAs it with `diffusers` | |
```bash | |
pip install diffusers==0.23.0 | |
``` | |
```py | |
from diffusers import DiffusionPipeline, LCMScheduler | |
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda") | |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config) | |
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA | |
results = pipe( | |
prompt="The spirit of a tamagotchi wandering in the city of Vienna", | |
num_inference_steps=4, | |
guidance_scale=0.0, | |
) | |
results.images[0] | |
``` | |
""" | |
) | |
inputs = [prompt, guidance, steps, seed] | |
generate_bt.click(fn=predict, inputs=inputs, outputs=image, show_progress=False) | |
prompt.input(fn=predict, inputs=inputs, outputs=image, show_progress=False) | |
guidance.change(fn=predict, inputs=inputs, outputs=image, show_progress=False) | |
steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False) | |
seed.change(fn=predict, inputs=inputs, outputs=image, show_progress=False) | |
demo.queue(api_open=False) | |
demo.launch(show_api=False) | |