Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- utils/i2i.py +100 -0
- utils/t2i.py +68 -0
utils/i2i.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import numpy as np
|
5 |
+
from lib_layerdiffuse.pipeline_flux_img2img import FluxImg2ImgPipeline
|
6 |
+
from lib_layerdiffuse.vae import TransparentVAE, pad_rgb
|
7 |
+
from torchvision import transforms
|
8 |
+
from PIL import Image
|
9 |
+
import spaces
|
10 |
+
|
11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
13 |
+
|
14 |
+
def seed_everything(seed: int) -> torch.Generator:
|
15 |
+
torch.manual_seed(seed)
|
16 |
+
torch.cuda.manual_seed_all(seed)
|
17 |
+
np.random.seed(seed)
|
18 |
+
generator = torch.Generator(device=device)
|
19 |
+
generator.manual_seed(seed)
|
20 |
+
return generator
|
21 |
+
|
22 |
+
# Initialize the pipeline
|
23 |
+
i2i_pipe = FluxImg2ImgPipeline.from_pretrained(
|
24 |
+
"black-forest-labs/FLUX.1-dev",
|
25 |
+
torch_dtype=torch.bfloat16,
|
26 |
+
use_auth_token=HF_TOKEN
|
27 |
+
).to(device)
|
28 |
+
|
29 |
+
# Load the LoRA weights
|
30 |
+
i2i_pipe.load_lora_weights("RedAIGC/Flux-version-LayerDiffuse", weight_name="layerlora.safetensors")
|
31 |
+
|
32 |
+
# Initialize the transparent VAE
|
33 |
+
trans_vae = TransparentVAE(i2i_pipe.vae, i2i_pipe.vae.dtype)
|
34 |
+
trans_vae.load_state_dict(torch.load("./models/TransparentVAE.pth"), strict=False)
|
35 |
+
trans_vae.to(device)
|
36 |
+
|
37 |
+
@spaces.GPU(duration=120)
|
38 |
+
def i2i_gen(
|
39 |
+
input_image,
|
40 |
+
prompt: str,
|
41 |
+
seed: int = 1111,
|
42 |
+
guidance_scale: float = 7.0,
|
43 |
+
num_inference_steps: int = 50,
|
44 |
+
strength: float = 0.8,
|
45 |
+
):
|
46 |
+
if input_image is None:
|
47 |
+
return None
|
48 |
+
|
49 |
+
# Process the input image
|
50 |
+
original_image = (transforms.ToTensor()(input_image)).unsqueeze(0)
|
51 |
+
|
52 |
+
# Get dimensions from the input image
|
53 |
+
height, width = original_image.shape[2], original_image.shape[3]
|
54 |
+
|
55 |
+
# Prepare the image for processing
|
56 |
+
padding_feed = [x for x in original_image.movedim(1, -1).float().cpu().numpy()]
|
57 |
+
list_of_np_rgb_padded = [pad_rgb(x) for x in padding_feed]
|
58 |
+
rgb_padded_bchw_01 = torch.from_numpy(np.stack(list_of_np_rgb_padded, axis=0)).float().movedim(-1, 1).to(device)
|
59 |
+
|
60 |
+
original_image_feed = original_image.clone()
|
61 |
+
original_image_feed[:, :3, :, :] = original_image_feed[:, :3, :, :] * 2.0 - 1.0
|
62 |
+
original_image_rgb = original_image_feed[:, :3, :, :] * original_image_feed[:, 3:, :, :]
|
63 |
+
|
64 |
+
original_image_feed = original_image_feed.to(device)
|
65 |
+
original_image_rgb = original_image_rgb.to(device)
|
66 |
+
|
67 |
+
# Generate the initial latent
|
68 |
+
initial_latent = trans_vae.encode(original_image_feed, original_image_rgb, rgb_padded_bchw_01, use_offset=True)
|
69 |
+
|
70 |
+
# Generate the image
|
71 |
+
latents = i2i_pipe(
|
72 |
+
latents=initial_latent,
|
73 |
+
image=original_image,
|
74 |
+
prompt=prompt,
|
75 |
+
height=height,
|
76 |
+
width=width,
|
77 |
+
num_inference_steps=num_inference_steps,
|
78 |
+
output_type="latent",
|
79 |
+
generator=seed_everything(seed),
|
80 |
+
guidance_scale=guidance_scale,
|
81 |
+
strength=strength,
|
82 |
+
).images
|
83 |
+
|
84 |
+
# Process the latents
|
85 |
+
latents = i2i_pipe._unpack_latents(latents, height, width, i2i_pipe.vae_scale_factor)
|
86 |
+
latents = (latents / i2i_pipe.vae.config.scaling_factor) + i2i_pipe.vae.config.shift_factor
|
87 |
+
|
88 |
+
# Decode the latents
|
89 |
+
with torch.no_grad():
|
90 |
+
original_x, x = trans_vae.decode(latents)
|
91 |
+
|
92 |
+
# Convert to image
|
93 |
+
x = x.clamp(0, 1)
|
94 |
+
x = x.permute(0, 2, 3, 1)
|
95 |
+
img = Image.fromarray((x*255).float().cpu().numpy().astype(np.uint8)[0])
|
96 |
+
|
97 |
+
# Clean up
|
98 |
+
torch.cuda.empty_cache()
|
99 |
+
|
100 |
+
return img
|
utils/t2i.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import argparse
|
4 |
+
import os
|
5 |
+
import datetime
|
6 |
+
from diffusers import FluxPipeline
|
7 |
+
from lib_layerdiffuse.pipeline_flux_img2img import FluxImg2ImgPipeline
|
8 |
+
from lib_layerdiffuse.vae import TransparentVAE, pad_rgb
|
9 |
+
import numpy as np
|
10 |
+
from torchvision import transforms
|
11 |
+
from safetensors.torch import load_file
|
12 |
+
from PIL import Image, ImageDraw, ImageFont
|
13 |
+
import spaces
|
14 |
+
|
15 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
16 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
17 |
+
|
18 |
+
def seed_everything(seed: int) -> torch.Generator:
|
19 |
+
torch.manual_seed(seed)
|
20 |
+
torch.cuda.manual_seed_all(seed)
|
21 |
+
np.random.seed(seed)
|
22 |
+
generator = torch.Generator()
|
23 |
+
generator.manual_seed(seed)
|
24 |
+
return generator
|
25 |
+
|
26 |
+
t2i_pipe = FluxPipeline.from_pretrained(
|
27 |
+
"black-forest-labs/FLUX.1-dev",
|
28 |
+
torch_dtype=torch.bfloat16,
|
29 |
+
use_auth_token=HF_TOKEN
|
30 |
+
).to(device)
|
31 |
+
|
32 |
+
trans_vae = TransparentVAE(t2i_pipe.vae, t2i_pipe.vae.dtype)
|
33 |
+
trans_vae.load_state_dict(torch.load("./models/TransparentVAE.pth"), strict=False)
|
34 |
+
trans_vae.to(device)
|
35 |
+
|
36 |
+
@spaces.GPU(duration=120)
|
37 |
+
def t2i_gen(
|
38 |
+
prompt: str,
|
39 |
+
# negative_prompt: str = None,
|
40 |
+
seed: int = 1111,
|
41 |
+
width: int = 1024,
|
42 |
+
height: int = 1024,
|
43 |
+
guidance_scale: float = 3.5,
|
44 |
+
num_inference_steps: int = 50,
|
45 |
+
):
|
46 |
+
t2i_pipe.load_lora_weights("RedAIGC/Flux-version-LayerDiffuse", weight_name="layerlora.safetensors")
|
47 |
+
latents = t2i_pipe(
|
48 |
+
prompt=prompt,
|
49 |
+
height=height,
|
50 |
+
width=width,
|
51 |
+
num_inference_steps=num_inference_steps,
|
52 |
+
output_type="latent",
|
53 |
+
generator=seed_everything(seed),
|
54 |
+
guidance_scale=guidance_scale,
|
55 |
+
).images
|
56 |
+
|
57 |
+
latents = t2i_pipe._unpack_latents(latents, height, width, t2i_pipe.vae_scale_factor)
|
58 |
+
latents = (latents / t2i_pipe.vae.config.scaling_factor) + t2i_pipe.vae.config.shift_factor
|
59 |
+
|
60 |
+
with torch.no_grad():
|
61 |
+
original_x, x = trans_vae.decode(latents)
|
62 |
+
|
63 |
+
x = x.clamp(0, 1)
|
64 |
+
x = x.permute(0, 2, 3, 1)
|
65 |
+
img = Image.fromarray((x*255).float().cpu().numpy().astype(np.uint8)[0])
|
66 |
+
torch.cuda.empty_cache()
|
67 |
+
|
68 |
+
return img
|