Spaces:
Build error
Build error
use huggingface-cli to download
Browse files- app.py +59 -50
- requirements.txt +4 -3
app.py
CHANGED
|
@@ -23,6 +23,51 @@ login(token=os.getenv('HF_TOKEN'))
|
|
| 23 |
device = torch.device('cuda')
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
@spaces.GPU
|
| 27 |
def generate_image(
|
| 28 |
model_name,
|
|
@@ -41,71 +86,35 @@ def generate_image(
|
|
| 41 |
torch.cuda.empty_cache()
|
| 42 |
dtype = torch.float16
|
| 43 |
set_seed(seed)
|
|
|
|
|
|
|
| 44 |
if model_name == 'sd35':
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
)
|
| 50 |
-
model_nf4 = SD3Transformer2DModel.from_pretrained(
|
| 51 |
-
"stabilityai/stable-diffusion-3.5-large",
|
| 52 |
-
subfolder="transformer",
|
| 53 |
-
quantization_config=nf4_config,
|
| 54 |
-
torch_dtype=torch.bfloat16
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 58 |
-
"stabilityai/stable-diffusion-3.5-large",
|
| 59 |
-
transformer=model_nf4,
|
| 60 |
-
torch_dtype=torch.bfloat16,
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 64 |
-
inverse_scheduler = FlowMatchEulerInverseScheduler.from_pretrained("stabilityai/stable-diffusion-3.5-large",
|
| 65 |
-
subfolder='scheduler')
|
| 66 |
-
pipe.inv_scheduler = inverse_scheduler
|
| 67 |
-
|
| 68 |
-
elif model_name == "sdxl":
|
| 69 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 70 |
-
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 71 |
-
torch_dtype=torch.float16,
|
| 72 |
-
variant="fp16",
|
| 73 |
-
use_safetensors=True
|
| 74 |
-
).to("cuda")
|
| 75 |
-
|
| 76 |
-
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
| 77 |
-
inverse_scheduler = DDIMInverseScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",
|
| 78 |
-
subfolder='scheduler')
|
| 79 |
-
pipe.inv_scheduler = inverse_scheduler
|
| 80 |
|
| 81 |
pipe.to(device)
|
| 82 |
pipe.enable_model_cpu_offload()
|
| 83 |
-
|
|
|
|
| 84 |
# TODO: load noise model
|
| 85 |
if method == 'core' or method == 'z-core':
|
| 86 |
from diffusion_pipeline.refine_model import PromptSD35Net, PromptSDXLNet
|
| 87 |
from diffusion_pipeline.lora import replace_linear_with_lora, lora_true
|
| 88 |
|
| 89 |
if model_name == 'sd35':
|
| 90 |
-
refine_model = PromptSD35Net()
|
| 91 |
replace_linear_with_lora(refine_model, rank=64, alpha=1.0, number_of_lora=28)
|
| 92 |
lora_true(refine_model, lora_idx=0)
|
| 93 |
-
|
| 94 |
-
os.makedirs('./weights', exist_ok=True)
|
| 95 |
-
if not os.path.exists('./weights/sd35_noise_model.pth'):
|
| 96 |
-
os.system('wget https://huggingface.co/sst12345/CoRe2/resolve/main/weights/sd35_noise_model.pth')
|
| 97 |
-
os.system('mv sd35_noise_model.pth ./weights/')
|
| 98 |
-
checkpoint = torch.load('./weights/sd35_noise_model.pth', map_location='cpu')
|
| 99 |
refine_model.load_state_dict(checkpoint)
|
| 100 |
elif model_name == 'sdxl':
|
| 101 |
refine_model = PromptSDXLNet()
|
| 102 |
replace_linear_with_lora(refine_model, rank=48, alpha=1.0, number_of_lora=50)
|
| 103 |
lora_true(refine_model, lora_idx=0)
|
| 104 |
-
|
| 105 |
-
if not os.path.exists('./weights/sdxl_noise_model.pth'):
|
| 106 |
-
os.system('wget https://huggingface.co/sst12345/CoRe2/resolve/main/weights/sdxl_noise_model.pth')
|
| 107 |
-
os.system('mv sdxl_noise_model.pth ./weights/')
|
| 108 |
-
checkpoint = torch.load('./weights/sdxl_noise_model.pth', map_location='cpu')
|
| 109 |
refine_model.load_state_dict(checkpoint)
|
| 110 |
|
| 111 |
print("Load Lora Success")
|
|
@@ -235,7 +244,7 @@ if __name__ == '__main__':
|
|
| 235 |
gr.Slider(minimum=1024, maximum=2048, value=1024, label="Size") # 设置默认大小为 1024
|
| 236 |
],
|
| 237 |
outputs=gr.Image(type="filepath"), # 修改了type参数
|
| 238 |
-
title="Image Generation with CoRe^2"
|
| 239 |
)
|
| 240 |
-
iface.launch()
|
| 241 |
|
|
|
|
| 23 |
device = torch.device('cuda')
|
| 24 |
|
| 25 |
|
| 26 |
+
|
| 27 |
+
# Load models outside the function to avoid reloading every time
|
| 28 |
+
def load_models():
|
| 29 |
+
# Load sd35 model
|
| 30 |
+
nf4_config = BitsAndBytesConfig(
|
| 31 |
+
load_in_4bit=True,
|
| 32 |
+
bnb_4bit_quant_type="nf4",
|
| 33 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 34 |
+
)
|
| 35 |
+
model_nf4 = SD3Transformer2DModel.from_pretrained(
|
| 36 |
+
"stabilityai/stable-diffusion-3.5-large",
|
| 37 |
+
subfolder="transformer",
|
| 38 |
+
quantization_config=nf4_config,
|
| 39 |
+
torch_dtype=torch.bfloat16
|
| 40 |
+
)
|
| 41 |
+
pipe_sd35 = StableDiffusion3Pipeline.from_pretrained(
|
| 42 |
+
"stabilityai/stable-diffusion-3.5-large",
|
| 43 |
+
transformer=model_nf4,
|
| 44 |
+
torch_dtype=torch.bfloat16,
|
| 45 |
+
)
|
| 46 |
+
pipe_sd35.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe_sd35.scheduler.config)
|
| 47 |
+
inverse_scheduler_sd35 = FlowMatchEulerInverseScheduler.from_pretrained(
|
| 48 |
+
"stabilityai/stable-diffusion-3.5-large",
|
| 49 |
+
subfolder='scheduler'
|
| 50 |
+
)
|
| 51 |
+
pipe_sd35.inv_scheduler = inverse_scheduler_sd35
|
| 52 |
+
|
| 53 |
+
# Load sdxl model
|
| 54 |
+
pipe_sdxl = StableDiffusionXLPipeline.from_pretrained(
|
| 55 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 56 |
+
torch_dtype=torch.float16,
|
| 57 |
+
variant="fp16",
|
| 58 |
+
use_safetensors=True
|
| 59 |
+
).to("cuda")
|
| 60 |
+
pipe_sdxl.scheduler = DDIMScheduler.from_config(pipe_sdxl.scheduler.config)
|
| 61 |
+
inverse_scheduler_sdxl = DDIMInverseScheduler.from_pretrained(
|
| 62 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 63 |
+
subfolder='scheduler'
|
| 64 |
+
)
|
| 65 |
+
pipe_sdxl.inv_scheduler = inverse_scheduler_sdxl
|
| 66 |
+
|
| 67 |
+
return pipe_sd35, pipe_sdxl
|
| 68 |
+
|
| 69 |
+
pipe_sd35, pipe_sdxl = load_models()
|
| 70 |
+
|
| 71 |
@spaces.GPU
|
| 72 |
def generate_image(
|
| 73 |
model_name,
|
|
|
|
| 86 |
torch.cuda.empty_cache()
|
| 87 |
dtype = torch.float16
|
| 88 |
set_seed(seed)
|
| 89 |
+
|
| 90 |
+
# Select the appropriate pipeline
|
| 91 |
if model_name == 'sd35':
|
| 92 |
+
pipe = pipe_sd35
|
| 93 |
+
elif model_name == 'sdxl':
|
| 94 |
+
pipe = pipe_sdxl
|
| 95 |
+
else:
|
| 96 |
+
raise ValueError("Invalid model name")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
pipe.to(device)
|
| 99 |
pipe.enable_model_cpu_offload()
|
| 100 |
+
# os.makedirs('./weights', exist_ok=True)
|
| 101 |
+
os.system('huggingface-cli download sst12345/CoRe2 weights/sd35_noise_model.pth weights/sdxl_noise_model.pth --local-dir ./weights')
|
| 102 |
# TODO: load noise model
|
| 103 |
if method == 'core' or method == 'z-core':
|
| 104 |
from diffusion_pipeline.refine_model import PromptSD35Net, PromptSDXLNet
|
| 105 |
from diffusion_pipeline.lora import replace_linear_with_lora, lora_true
|
| 106 |
|
| 107 |
if model_name == 'sd35':
|
| 108 |
+
refine_model = PromptSD35Net().to(device)
|
| 109 |
replace_linear_with_lora(refine_model, rank=64, alpha=1.0, number_of_lora=28)
|
| 110 |
lora_true(refine_model, lora_idx=0)
|
| 111 |
+
checkpoint = torch.load('./weights/weights/sd35_noise_model.pth', map_location='cpu')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
refine_model.load_state_dict(checkpoint)
|
| 113 |
elif model_name == 'sdxl':
|
| 114 |
refine_model = PromptSDXLNet()
|
| 115 |
replace_linear_with_lora(refine_model, rank=48, alpha=1.0, number_of_lora=50)
|
| 116 |
lora_true(refine_model, lora_idx=0)
|
| 117 |
+
checkpoint = torch.load('./weights/weights/sdxl_noise_model.pth', map_location='cpu')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
refine_model.load_state_dict(checkpoint)
|
| 119 |
|
| 120 |
print("Load Lora Success")
|
|
|
|
| 244 |
gr.Slider(minimum=1024, maximum=2048, value=1024, label="Size") # 设置默认大小为 1024
|
| 245 |
],
|
| 246 |
outputs=gr.Image(type="filepath"), # 修改了type参数
|
| 247 |
+
title="Image Generation with CoRe^2",
|
| 248 |
)
|
| 249 |
+
iface.launch(share=True)
|
| 250 |
|
requirements.txt
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
-
diffusers
|
| 2 |
-
transformers
|
| 3 |
einops
|
| 4 |
wandb
|
| 5 |
-
accelerate
|
| 6 |
pandas
|
| 7 |
imageio
|
| 8 |
gradio
|
| 9 |
sentencepiece
|
|
|
|
| 10 |
bitsandbytes
|
| 11 |
imageio-ffmpeg
|
| 12 |
omegaconf
|
|
|
|
| 1 |
+
diffusers==0.31.0
|
| 2 |
+
transformers==4.46.1
|
| 3 |
einops
|
| 4 |
wandb
|
| 5 |
+
accelerate==1.0.1
|
| 6 |
pandas
|
| 7 |
imageio
|
| 8 |
gradio
|
| 9 |
sentencepiece
|
| 10 |
+
huggingface_hub
|
| 11 |
bitsandbytes
|
| 12 |
imageio-ffmpeg
|
| 13 |
omegaconf
|