Spaces:
Running
on
Zero
Running
on
Zero
Update app_v4.py
Browse files
app_v4.py
CHANGED
|
@@ -6,10 +6,9 @@ import spaces
|
|
| 6 |
import os
|
| 7 |
import datetime
|
| 8 |
import io
|
| 9 |
-
import numpy as np
|
| 10 |
import moondream as md
|
| 11 |
from transformers import T5EncoderModel
|
| 12 |
-
from diffusers import FluxControlNetPipeline
|
| 13 |
from diffusers.utils import load_image
|
| 14 |
from PIL import Image
|
| 15 |
from threading import Thread
|
|
@@ -71,29 +70,14 @@ text_encoder_2_unquant = T5EncoderModel.from_pretrained(
|
|
| 71 |
torch_dtype=torch.bfloat16,
|
| 72 |
token=huggingface_token
|
| 73 |
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
)
|
| 77 |
-
pipe_upscaler = FluxControlNetPipeline.from_pretrained(
|
| 78 |
"LPX55/FLUX.1M-8step_upscaler-cnet",
|
| 79 |
torch_dtype=torch.bfloat16,
|
| 80 |
text_encoder_2=text_encoder_2_unquant,
|
| 81 |
token=huggingface_token
|
| 82 |
)
|
| 83 |
-
pipe_upscaler.to("cuda")
|
| 84 |
-
|
| 85 |
-
controlnet = FluxControlNetModel.from_pretrained("alimama-creative/FLUX.1-dev-Controlnet-Inpainting-Beta", torch_dtype=torch.bfloat16)
|
| 86 |
-
|
| 87 |
-
pipe = FluxControlNetInpaintPipeline.from_pretrained(
|
| 88 |
-
"LPX55/FLUX.1-merged_lightning_v2",
|
| 89 |
-
controlnet=controlnet,
|
| 90 |
-
transformer=transformer,
|
| 91 |
-
torch_dtype=torch.bfloat16,
|
| 92 |
-
token=huggingface_token
|
| 93 |
-
)
|
| 94 |
pipe.to("cuda")
|
| 95 |
-
pipe.transformer.to(torch.bfloat16)
|
| 96 |
-
pipe.controlnet.to(torch.bfloat16)
|
| 97 |
|
| 98 |
try:
|
| 99 |
dump_environment_info()
|
|
@@ -164,51 +148,28 @@ def generate_focus(control_image, focus_list):
|
|
| 164 |
except Exception as e:
|
| 165 |
print(f"Error generating focus: {e}")
|
| 166 |
return "highly detailed photo, raw photography.", "Original Image Dimensions: N/A"
|
| 167 |
-
|
| 168 |
|
| 169 |
@spaces.GPU(duration=6, progress=gr.Progress(track_tqdm=True))
|
| 170 |
@torch.no_grad()
|
| 171 |
def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
|
| 172 |
generator = torch.Generator().manual_seed(seed)
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
# Resize the image to a maximum longest side of 1024 pixels
|
| 178 |
control_image = resize_image_to_max_side(control_image, max_side_length=1024)
|
| 179 |
-
|
| 180 |
w, h = control_image.size
|
| 181 |
-
# Crop to nearest multiple of 32
|
| 182 |
w = w - w % 32
|
| 183 |
h = h - h % 32
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
print(f"Resized image dimensions: {control_image.size[0]}x{control_image.size[1]}")
|
| 188 |
print(f"PromptLog: {repr(prompt)}")
|
| 189 |
-
|
| 190 |
-
# Convert image to RGB for processing, but keep alpha channel for transparency
|
| 191 |
-
control_image_rgb = control_image.convert("RGB")
|
| 192 |
-
control_image_alpha = control_image.split()[-1]
|
| 193 |
-
|
| 194 |
-
# Convert alpha channel to a mask (transparent = white, opaque = black)
|
| 195 |
-
# White corresponds to 1 (to be inpainted), black corresponds to 0 (to be preserved)
|
| 196 |
-
# Convert alpha to numpy array for processing
|
| 197 |
-
alpha_array = np.array(control_image_alpha)
|
| 198 |
-
# Create binary mask (1 for transparent, 0 for opaque)
|
| 199 |
-
mask = (alpha_array > 128).astype(np.float32) # 1 for transparent (to be inpainted), 0 for opaque
|
| 200 |
-
|
| 201 |
-
# Optional: Visualize the mask (for debugging purposes)
|
| 202 |
-
# mask_image = Image.fromarray((mask * 255).astype(np.uint8))
|
| 203 |
-
# mask_image.show()
|
| 204 |
-
|
| 205 |
with torch.inference_mode():
|
| 206 |
image = pipe(
|
| 207 |
-
image=control_image_rgb,
|
| 208 |
generator=generator,
|
| 209 |
prompt=prompt,
|
| 210 |
-
control_image=
|
| 211 |
-
mask_image=mask, # Pass the numpy array as the mask
|
| 212 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 213 |
num_inference_steps=steps,
|
| 214 |
guidance_scale=guidance_scale,
|
|
@@ -217,13 +178,9 @@ def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_
|
|
| 217 |
control_guidance_start=0.0,
|
| 218 |
control_guidance_end=guidance_end,
|
| 219 |
).images[0]
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
image.putalpha(control_image_alpha)
|
| 224 |
-
|
| 225 |
-
return image
|
| 226 |
-
|
| 227 |
def update_parameters(preset):
|
| 228 |
if preset in presets:
|
| 229 |
params = presets[preset]
|
|
@@ -266,12 +223,10 @@ def process_image(control_image, user_prompt, system_prompt, scale, steps,
|
|
| 266 |
seed=seed,
|
| 267 |
guidance_end=guidance_end
|
| 268 |
)
|
|
|
|
| 269 |
try:
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
image.save(output, format="PNG")
|
| 273 |
-
debug_img = Image.open(output).convert("RGBA")
|
| 274 |
-
save_image("/tmp/" + str(seed) + "output.png", debug_img)
|
| 275 |
except Exception as e:
|
| 276 |
print("Error 160: " + str(e))
|
| 277 |
log_params(final_prompt, scale, steps, controlnet_conditioning_scale, guidance_scale, seed, guidance_end, control_image, image)
|
|
|
|
| 6 |
import os
|
| 7 |
import datetime
|
| 8 |
import io
|
|
|
|
| 9 |
import moondream as md
|
| 10 |
from transformers import T5EncoderModel
|
| 11 |
+
from diffusers import FluxControlNetPipeline
|
| 12 |
from diffusers.utils import load_image
|
| 13 |
from PIL import Image
|
| 14 |
from threading import Thread
|
|
|
|
| 70 |
torch_dtype=torch.bfloat16,
|
| 71 |
token=huggingface_token
|
| 72 |
)
|
| 73 |
+
|
| 74 |
+
pipe = FluxControlNetPipeline.from_pretrained(
|
|
|
|
|
|
|
| 75 |
"LPX55/FLUX.1M-8step_upscaler-cnet",
|
| 76 |
torch_dtype=torch.bfloat16,
|
| 77 |
text_encoder_2=text_encoder_2_unquant,
|
| 78 |
token=huggingface_token
|
| 79 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
pipe.to("cuda")
|
|
|
|
|
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
dump_environment_info()
|
|
|
|
| 148 |
except Exception as e:
|
| 149 |
print(f"Error generating focus: {e}")
|
| 150 |
return "highly detailed photo, raw photography.", "Original Image Dimensions: N/A"
|
|
|
|
| 151 |
|
| 152 |
@spaces.GPU(duration=6, progress=gr.Progress(track_tqdm=True))
|
| 153 |
@torch.no_grad()
|
| 154 |
def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
|
| 155 |
generator = torch.Generator().manual_seed(seed)
|
| 156 |
+
# Load control image
|
| 157 |
+
control_image = load_image(control_image)
|
| 158 |
+
|
|
|
|
| 159 |
# Resize the image to a maximum longest side of 1024 pixels
|
| 160 |
control_image = resize_image_to_max_side(control_image, max_side_length=1024)
|
| 161 |
+
|
| 162 |
w, h = control_image.size
|
|
|
|
| 163 |
w = w - w % 32
|
| 164 |
h = h - h % 32
|
| 165 |
+
control_image = control_image.resize((int(w * scale), int(h * scale)), resample=2) # Resample.BILINEAR
|
| 166 |
+
print("Size to: " + str(control_image.size[0]) + ", " + str(control_image.size[1]))
|
|
|
|
|
|
|
| 167 |
print(f"PromptLog: {repr(prompt)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
with torch.inference_mode():
|
| 169 |
image = pipe(
|
|
|
|
| 170 |
generator=generator,
|
| 171 |
prompt=prompt,
|
| 172 |
+
control_image=control_image,
|
|
|
|
| 173 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 174 |
num_inference_steps=steps,
|
| 175 |
guidance_scale=guidance_scale,
|
|
|
|
| 178 |
control_guidance_start=0.0,
|
| 179 |
control_guidance_end=guidance_end,
|
| 180 |
).images[0]
|
| 181 |
+
# print("Type: " + str(type(image)))
|
| 182 |
+
return image
|
| 183 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
def update_parameters(preset):
|
| 185 |
if preset in presets:
|
| 186 |
params = presets[preset]
|
|
|
|
| 223 |
seed=seed,
|
| 224 |
guidance_end=guidance_end
|
| 225 |
)
|
| 226 |
+
|
| 227 |
try:
|
| 228 |
+
debug_img = Image.open(image.save("/tmp/" + str(seed) + "output.png"))
|
| 229 |
+
save_image("/tmp/" + str(seed) + "output.png", debug_img)
|
|
|
|
|
|
|
|
|
|
| 230 |
except Exception as e:
|
| 231 |
print("Error 160: " + str(e))
|
| 232 |
log_params(final_prompt, scale, steps, controlnet_conditioning_scale, guidance_scale, seed, guidance_end, control_image, image)
|