Commit
•
e0de939
1
Parent(s):
1794075
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from diffusers import AutoencoderKL
|
4 |
from diffusers.utils import load_image
|
5 |
from controlnet_flux import FluxControlNetModel
|
6 |
from transformer_flux import FluxTransformer2DModel
|
7 |
from pipeline_flux_controlnet_inpaint import FluxControlNetInpaintingPipeline
|
|
|
8 |
from PIL import Image, ImageDraw
|
9 |
import numpy as np
|
10 |
import spaces
|
11 |
from huggingface_hub import hf_hub_download
|
12 |
|
|
|
|
|
|
|
|
|
|
|
13 |
# Load models
|
14 |
-
controlnet = FluxControlNetModel.from_pretrained("alimama-creative/FLUX.1-dev-Controlnet-Inpainting-Alpha", torch_dtype=torch.bfloat16)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
pipe = FluxControlNetInpaintingPipeline.from_pretrained(
|
21 |
"black-forest-labs/FLUX.1-dev",
|
22 |
-
|
23 |
-
transformer=transformer,
|
24 |
-
vae=vae,
|
25 |
torch_dtype=torch.bfloat16
|
26 |
)
|
|
|
|
|
27 |
repo_name = "ByteDance/Hyper-SD"
|
28 |
ckpt_name = "Hyper-FLUX.1-dev-8steps-lora.safetensors"
|
29 |
pipe.load_lora_weights(hf_hub_download(repo_name, ckpt_name))
|
30 |
pipe.fuse_lora(lora_scale=0.125)
|
31 |
-
pipe.transformer.to(torch.bfloat16)
|
32 |
-
pipe.controlnet.to(torch.bfloat16)
|
33 |
pipe.to("cuda")
|
34 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
35 |
if alignment in ("Left", "Right") and source_width >= target_width:
|
@@ -150,7 +156,7 @@ def inpaint(image, width, height, overlap_percentage, num_inference_steps, resiz
|
|
150 |
|
151 |
generator = torch.Generator(device="cuda").manual_seed(42)
|
152 |
|
153 |
-
|
154 |
prompt=final_prompt,
|
155 |
height=height,
|
156 |
width=width,
|
@@ -162,12 +168,8 @@ def inpaint(image, width, height, overlap_percentage, num_inference_steps, resiz
|
|
162 |
guidance_scale=3.5,
|
163 |
negative_prompt="",
|
164 |
true_guidance_scale=3.5,
|
165 |
-
|
166 |
-
|
167 |
-
print(latent_image)
|
168 |
-
pipe.to("cpu")
|
169 |
-
vae.to("cuda")
|
170 |
-
result = vae.decode(latent_image).sample
|
171 |
result = result.convert("RGBA")
|
172 |
cnet_image.paste(result, (0, 0), mask)
|
173 |
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from diffusers import AutoencoderKL, FluxTransformer2DModel
|
4 |
from diffusers.utils import load_image
|
5 |
from controlnet_flux import FluxControlNetModel
|
6 |
from transformer_flux import FluxTransformer2DModel
|
7 |
from pipeline_flux_controlnet_inpaint import FluxControlNetInpaintingPipeline
|
8 |
+
from transformers import T5EncoderModel, CLIPTextModel
|
9 |
from PIL import Image, ImageDraw
|
10 |
import numpy as np
|
11 |
import spaces
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
|
14 |
+
# Load fp8
|
15 |
+
#transformer = FluxTransformer2DModel.from_single_file("https://huggingface.co/Kijai/flux-fp8/blob/main/flux1-dev-fp8.safetensors", torch_dtype=torch.bfloat16)
|
16 |
+
#quantize(transformer, weights=qfloat8)
|
17 |
+
#freeze(transformer)
|
18 |
+
|
19 |
# Load models
|
20 |
+
#controlnet = FluxControlNetModel.from_pretrained("alimama-creative/FLUX.1-dev-Controlnet-Inpainting-Alpha", torch_dtype=torch.bfloat16)
|
21 |
+
#quantize(controlnet, weights=qfloat8)
|
22 |
+
#freeze(controlnet)
|
23 |
+
|
24 |
+
text_encoder_2 = T5EncoderModel.from_pretrained(bfl_repo, subfolder="text_encoder_2", torch_dtype=dtype)
|
25 |
+
quantize(text_encoder_2, weights=qfloat8)
|
26 |
+
freeze(text_encoder_2)
|
27 |
+
|
28 |
pipe = FluxControlNetInpaintingPipeline.from_pretrained(
|
29 |
"black-forest-labs/FLUX.1-dev",
|
30 |
+
text_encoder_2=None,
|
|
|
|
|
31 |
torch_dtype=torch.bfloat16
|
32 |
)
|
33 |
+
pipe.text_encoder_2 = text_encoder_2
|
34 |
+
|
35 |
repo_name = "ByteDance/Hyper-SD"
|
36 |
ckpt_name = "Hyper-FLUX.1-dev-8steps-lora.safetensors"
|
37 |
pipe.load_lora_weights(hf_hub_download(repo_name, ckpt_name))
|
38 |
pipe.fuse_lora(lora_scale=0.125)
|
|
|
|
|
39 |
pipe.to("cuda")
|
40 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
41 |
if alignment in ("Left", "Right") and source_width >= target_width:
|
|
|
156 |
|
157 |
generator = torch.Generator(device="cuda").manual_seed(42)
|
158 |
|
159 |
+
result = pipe(
|
160 |
prompt=final_prompt,
|
161 |
height=height,
|
162 |
width=width,
|
|
|
168 |
guidance_scale=3.5,
|
169 |
negative_prompt="",
|
170 |
true_guidance_scale=3.5,
|
171 |
+
).images[0]
|
172 |
+
|
|
|
|
|
|
|
|
|
173 |
result = result.convert("RGBA")
|
174 |
cnet_image.paste(result, (0, 0), mask)
|
175 |
|