Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
model_id_default = "CompVis/stable-diffusion-v1-4" # Replace to the model you would like to use
|
|
@@ -18,25 +19,89 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
| 18 |
MAX_IMAGE_SIZE = 1024
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 22 |
def infer(
|
| 23 |
prompt,
|
| 24 |
negative_prompt,
|
| 25 |
-
width,
|
| 26 |
-
height,
|
| 27 |
model_id=model_id_default,
|
| 28 |
seed=42,
|
| 29 |
guidance_scale=7.0,
|
|
|
|
| 30 |
num_inference_steps=20,
|
| 31 |
progress=gr.Progress(track_tqdm=True),
|
| 32 |
):
|
| 33 |
-
generator = torch.Generator().manual_seed(seed)
|
| 34 |
-
pipe =
|
|
|
|
| 35 |
pipe = pipe.to(device)
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
image = pipe(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
guidance_scale=guidance_scale,
|
| 41 |
num_inference_steps=num_inference_steps,
|
| 42 |
width=width,
|
|
@@ -94,6 +159,14 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
| 94 |
value=7.0, # Replace with defaults that work for your model
|
| 95 |
)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
num_inference_steps = gr.Slider(
|
| 98 |
label="Number of inference steps",
|
| 99 |
minimum=1,
|
|
@@ -141,4 +214,4 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
| 141 |
)
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
+
import os
|
|
|
|
|
|
|
| 5 |
import torch
|
| 6 |
+
from diffusers import StableDiffusionPipeline
|
| 7 |
+
from peft import PeftModel, LoraConfig
|
| 8 |
+
from diffusers import DiffusionPipeline
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
model_id_default = "CompVis/stable-diffusion-v1-4" # Replace to the model you would like to use
|
|
|
|
| 19 |
MAX_IMAGE_SIZE = 1024
|
| 20 |
|
| 21 |
|
| 22 |
+
def get_lora_sd_pipeline(
|
| 23 |
+
ckpt_dir='./output',
|
| 24 |
+
base_model_name_or_path=model_id_default,
|
| 25 |
+
dtype=torch_dtype,
|
| 26 |
+
device=device,
|
| 27 |
+
adapter_name="default"
|
| 28 |
+
):
|
| 29 |
+
unet_sub_dir = os.path.join(ckpt_dir, "unet")
|
| 30 |
+
text_encoder_sub_dir = os.path.join(ckpt_dir, "text_encoder")
|
| 31 |
+
if os.path.exists(text_encoder_sub_dir) and base_model_name_or_path is None:
|
| 32 |
+
config = LoraConfig.from_pretrained(text_encoder_sub_dir)
|
| 33 |
+
base_model_name_or_path = config.base_model_name_or_path
|
| 34 |
+
|
| 35 |
+
if base_model_name_or_path is None:
|
| 36 |
+
raise ValueError("Please specify the base model name or path")
|
| 37 |
+
|
| 38 |
+
pipe = StableDiffusionPipeline.from_pretrained(base_model_name_or_path, torch_dtype=dtype).to(device)
|
| 39 |
+
pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir, adapter_name=adapter_name)
|
| 40 |
+
|
| 41 |
+
if os.path.exists(text_encoder_sub_dir):
|
| 42 |
+
pipe.text_encoder = PeftModel.from_pretrained(
|
| 43 |
+
pipe.text_encoder, text_encoder_sub_dir, adapter_name=adapter_name
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if dtype in (torch.float16, torch.bfloat16):
|
| 47 |
+
pipe.unet.half()
|
| 48 |
+
pipe.text_encoder.half()
|
| 49 |
+
|
| 50 |
+
pipe.to(device)
|
| 51 |
+
return pipe
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def encode_prompt(prompt, tokenizer, text_encoder):
|
| 55 |
+
text_inputs = tokenizer(
|
| 56 |
+
prompt,
|
| 57 |
+
padding="max_length",
|
| 58 |
+
max_length=tokenizer.model_max_length,
|
| 59 |
+
return_tensors="pt",
|
| 60 |
+
)
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
if len(text_inputs.input_ids[0]) < tokenizer.model_max_length:
|
| 63 |
+
prompt_embeds = text_encoder(text_inputs.input_ids.to(text_encoder.device))[0]
|
| 64 |
+
else:
|
| 65 |
+
embeds = []
|
| 66 |
+
start = 0
|
| 67 |
+
while start < tokenizer.model_max_length:
|
| 68 |
+
end = start + tokenizer.model_max_length
|
| 69 |
+
part_of_text_inputs = text_inputs.input_ids[0][start:end]
|
| 70 |
+
if len(part_of_text_inputs) < tokenizer.model_max_length:
|
| 71 |
+
part_of_text_inputs = torch.cat([part_of_text_inputs, torch.tensor([tokenizer.pad_token_id] * (tokenizer.model_max_length - len(part_of_text_inputs)))])
|
| 72 |
+
embeds.append(text_encoder(part_of_text_inputs.to(text_encoder.device).unsqueeze(0))[0])
|
| 73 |
+
start += int((8/11)*tokenizer.model_max_length)
|
| 74 |
+
prompt_embeds = torch.mean(torch.stack(embeds, dim=0), dim=0)
|
| 75 |
+
return prompt_embeds
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
pipe = get_lora_sd_pipeline(adapter_name="sticker_of_funny_cat_Pusheen")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 82 |
def infer(
|
| 83 |
prompt,
|
| 84 |
negative_prompt,
|
| 85 |
+
width=512,
|
| 86 |
+
height=512,
|
| 87 |
model_id=model_id_default,
|
| 88 |
seed=42,
|
| 89 |
guidance_scale=7.0,
|
| 90 |
+
lora_scale=0.5,
|
| 91 |
num_inference_steps=20,
|
| 92 |
progress=gr.Progress(track_tqdm=True),
|
| 93 |
):
|
| 94 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 95 |
+
pipe = get_lora_sd_pipeline(base_model_name_or_path=model_id,
|
| 96 |
+
adapter_name="sticker_of_funny_cat_Pusheen")
|
| 97 |
pipe = pipe.to(device)
|
| 98 |
+
prompt_embeds = encode_prompt(prompt, pipe.tokenizer, pipe.text_encoder)
|
| 99 |
+
negative_prompt_embeds = encode_prompt(negative_prompt, pipe.tokenizer, pipe.text_encoder)
|
| 100 |
+
pipe.fuse_lora(lora_scale=lora_scale)
|
| 101 |
|
| 102 |
image = pipe(
|
| 103 |
+
prompt_embeds=prompt_embeds,
|
| 104 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
| 105 |
guidance_scale=guidance_scale,
|
| 106 |
num_inference_steps=num_inference_steps,
|
| 107 |
width=width,
|
|
|
|
| 159 |
value=7.0, # Replace with defaults that work for your model
|
| 160 |
)
|
| 161 |
|
| 162 |
+
lora_scale = gr.Slider(
|
| 163 |
+
label="LoRA scale",
|
| 164 |
+
minimum=0.0,
|
| 165 |
+
maximum=1.0,
|
| 166 |
+
step=0.1,
|
| 167 |
+
value=0.5,
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
num_inference_steps = gr.Slider(
|
| 171 |
label="Number of inference steps",
|
| 172 |
minimum=1,
|
|
|
|
| 214 |
)
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
demo.launch()
|