Spaces:
Running
Running
ciover2024
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -25,6 +25,7 @@ image = pipe(prompt).images[0]
|
|
25 |
#import cv2
|
26 |
|
27 |
from diffusers import StableDiffusionInpaintPipeline
|
|
|
28 |
import numpy as np
|
29 |
import os
|
30 |
import shutil
|
@@ -45,10 +46,10 @@ def load_inpainting_model():
|
|
45 |
).to(device)
|
46 |
return pipe
|
47 |
|
|
|
48 |
# Load the model once globally to avoid repeated loading
|
49 |
def load_upscaling_model():
|
50 |
# Load pipeline
|
51 |
-
"""
|
52 |
device = "cpu" # Explicitly use CPU
|
53 |
controlnet = FluxControlNetModel.from_pretrained(
|
54 |
"jasperai/Flux.1-dev-Controlnet-Upscaler",
|
@@ -59,21 +60,21 @@ def load_upscaling_model():
|
|
59 |
controlnet=controlnet,
|
60 |
torch_dtype=torch.float32
|
61 |
).to(device)
|
62 |
-
"""
|
63 |
pipe = DiffusionPipeline.from_pretrained("jasperai/Flux.1-dev-Controlnet-Upscaler")
|
64 |
return pipe
|
|
|
65 |
|
66 |
# Preload the model once
|
67 |
inpaint_pipeline = load_inpainting_model()
|
68 |
# Preload the model once
|
69 |
-
upscale_pipeline = load_upscaling_model()
|
70 |
|
71 |
# Function to resize image (simpler interpolation method for speed)
|
72 |
def resize_to_match(input_image, output_image):
|
73 |
|
74 |
-
w, h = output_image.size
|
75 |
-
control_image = output_image.resize((w * 4, h * 4))
|
76 |
-
|
77 |
scaled_image = pipe(
|
78 |
prompt="",
|
79 |
control_image=control_image,
|
@@ -83,13 +84,13 @@ def resize_to_match(input_image, output_image):
|
|
83 |
height=control_image.size[1],
|
84 |
width=control_image.size[0]
|
85 |
).images[0]
|
86 |
-
|
87 |
-
return scaled_image
|
88 |
|
89 |
#torch_img = pil_to_torch(input_image)
|
90 |
#torch_img_scaled = F.interpolate(torch_img.unsqueeze(0),mode='trilinear').squeeze(0)
|
91 |
#output_image = torchvision.transforms.functional.to_pil_image(torch_img_scaled, mode=None)
|
92 |
-
|
93 |
|
94 |
# Function to generate the mask using Florence SAM Masking API (Replicate)
|
95 |
def generate_mask(image_path, text_prompt="clothing"):
|
@@ -113,7 +114,7 @@ def inpaint_image(input_image, mask_image):
|
|
113 |
prompt = "undress, naked"
|
114 |
result = inpaint_pipeline(prompt=prompt, image=input_image, mask_image=mask_image)
|
115 |
inpainted_image = result.images[0]
|
116 |
-
inpainted_image = resize_to_match(input_image, inpainted_image)
|
117 |
return inpainted_image
|
118 |
|
119 |
# Function to process input image and mask
|
|
|
25 |
#import cv2
|
26 |
|
27 |
from diffusers import StableDiffusionInpaintPipeline
|
28 |
+
|
29 |
import numpy as np
|
30 |
import os
|
31 |
import shutil
|
|
|
46 |
).to(device)
|
47 |
return pipe
|
48 |
|
49 |
+
"""
|
50 |
# Load the model once globally to avoid repeated loading
|
51 |
def load_upscaling_model():
|
52 |
# Load pipeline
|
|
|
53 |
device = "cpu" # Explicitly use CPU
|
54 |
controlnet = FluxControlNetModel.from_pretrained(
|
55 |
"jasperai/Flux.1-dev-Controlnet-Upscaler",
|
|
|
60 |
controlnet=controlnet,
|
61 |
torch_dtype=torch.float32
|
62 |
).to(device)
|
|
|
63 |
pipe = DiffusionPipeline.from_pretrained("jasperai/Flux.1-dev-Controlnet-Upscaler")
|
64 |
return pipe
|
65 |
+
"""
|
66 |
|
67 |
# Preload the model once
|
68 |
inpaint_pipeline = load_inpainting_model()
|
69 |
# Preload the model once
|
70 |
+
#upscale_pipeline = load_upscaling_model()
|
71 |
|
72 |
# Function to resize image (simpler interpolation method for speed)
|
73 |
def resize_to_match(input_image, output_image):
|
74 |
|
75 |
+
#w, h = output_image.size
|
76 |
+
#control_image = output_image.resize((w * 4, h * 4))
|
77 |
+
"""
|
78 |
scaled_image = pipe(
|
79 |
prompt="",
|
80 |
control_image=control_image,
|
|
|
84 |
height=control_image.size[1],
|
85 |
width=control_image.size[0]
|
86 |
).images[0]
|
87 |
+
"""
|
88 |
+
#return scaled_image
|
89 |
|
90 |
#torch_img = pil_to_torch(input_image)
|
91 |
#torch_img_scaled = F.interpolate(torch_img.unsqueeze(0),mode='trilinear').squeeze(0)
|
92 |
#output_image = torchvision.transforms.functional.to_pil_image(torch_img_scaled, mode=None)
|
93 |
+
return output_image.resize(input_image.size, Image.BICUBIC) # Use BILINEAR for faster resizing
|
94 |
|
95 |
# Function to generate the mask using Florence SAM Masking API (Replicate)
|
96 |
def generate_mask(image_path, text_prompt="clothing"):
|
|
|
114 |
prompt = "undress, naked"
|
115 |
result = inpaint_pipeline(prompt=prompt, image=input_image, mask_image=mask_image)
|
116 |
inpainted_image = result.images[0]
|
117 |
+
#inpainted_image = resize_to_match(input_image, inpainted_image)
|
118 |
return inpainted_image
|
119 |
|
120 |
# Function to process input image and mask
|