Spaces:
Runtime error
Runtime error
add debug log
Browse files
app.py
CHANGED
@@ -10,6 +10,8 @@ from PIL import Image
|
|
10 |
from diffusers import FluxInpaintPipeline
|
11 |
from huggingface_hub import login
|
12 |
import os
|
|
|
|
|
13 |
|
14 |
MARKDOWN = """
|
15 |
# FLUX.1 Inpainting with lora
|
@@ -20,9 +22,35 @@ IMAGE_SIZE = 1024
|
|
20 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
22 |
trigger_word = "a_photo_of_TOK"
|
|
|
|
|
|
|
23 |
|
24 |
login(token=HF_TOKEN)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
|
27 |
image = image.convert("RGBA")
|
28 |
data = image.getdata()
|
@@ -37,34 +65,6 @@ def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
|
|
37 |
image.putdata(new_data)
|
38 |
return image
|
39 |
|
40 |
-
|
41 |
-
EXAMPLES = [
|
42 |
-
[
|
43 |
-
{
|
44 |
-
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
45 |
-
"layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw))],
|
46 |
-
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
|
47 |
-
},
|
48 |
-
"little lion",
|
49 |
-
42,
|
50 |
-
False,
|
51 |
-
0.85,
|
52 |
-
30
|
53 |
-
],
|
54 |
-
[
|
55 |
-
{
|
56 |
-
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
57 |
-
"layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw))],
|
58 |
-
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
|
59 |
-
},
|
60 |
-
"tribal tattoos",
|
61 |
-
42,
|
62 |
-
False,
|
63 |
-
0.85,
|
64 |
-
30
|
65 |
-
]
|
66 |
-
]
|
67 |
-
|
68 |
pipe = FluxInpaintPipeline.from_pretrained(
|
69 |
"black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(DEVICE)
|
70 |
|
@@ -119,27 +119,30 @@ def process(
|
|
119 |
gr.Info("Please draw a mask on the image.")
|
120 |
return None, None
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
128 |
if randomize_seed_checkbox:
|
129 |
seed_slicer = random.randint(0, MAX_SEED)
|
130 |
generator = torch.Generator().manual_seed(seed_slicer)
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
143 |
return result, resized_mask
|
144 |
|
145 |
|
@@ -205,25 +208,6 @@ with gr.Blocks() as demo:
|
|
205 |
with gr.Accordion("Debug", open=False):
|
206 |
output_mask_component = gr.Image(
|
207 |
type='pil', image_mode='RGB', label='Input mask', format="png")
|
208 |
-
with gr.Row():
|
209 |
-
gr.Examples(
|
210 |
-
fn=process,
|
211 |
-
examples=EXAMPLES,
|
212 |
-
inputs=[
|
213 |
-
input_image_editor_component,
|
214 |
-
input_text_component,
|
215 |
-
seed_slicer_component,
|
216 |
-
randomize_seed_checkbox_component,
|
217 |
-
strength_slider_component,
|
218 |
-
num_inference_steps_slider_component
|
219 |
-
],
|
220 |
-
outputs=[
|
221 |
-
output_image_component,
|
222 |
-
output_mask_component
|
223 |
-
],
|
224 |
-
run_on_click=True,
|
225 |
-
cache_examples=False
|
226 |
-
)
|
227 |
|
228 |
submit_button_component.click(
|
229 |
fn=process,
|
|
|
10 |
from diffusers import FluxInpaintPipeline
|
11 |
from huggingface_hub import login
|
12 |
import os
|
13 |
+
import time
|
14 |
+
|
15 |
|
16 |
MARKDOWN = """
|
17 |
# FLUX.1 Inpainting with lora
|
|
|
22 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
24 |
trigger_word = "a_photo_of_TOK"
|
25 |
+
lora_path = "jiuface/boy-001"
|
26 |
+
lora_weights = "flux_train_replicate.safetensors"
|
27 |
+
lora_scale = 0.9
|
28 |
|
29 |
login(token=HF_TOKEN)
|
30 |
|
31 |
+
|
32 |
+
|
33 |
+
class calculateDuration:
|
34 |
+
def __init__(self, activity_name=""):
|
35 |
+
self.activity_name = activity_name
|
36 |
+
|
37 |
+
def __enter__(self):
|
38 |
+
self.start_time = time.time()
|
39 |
+
self.start_time_formatted = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.start_time))
|
40 |
+
print(f"Start time: {self.start_time_formatted}")
|
41 |
+
return self
|
42 |
+
|
43 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
44 |
+
self.end_time = time.time()
|
45 |
+
self.elapsed_time = self.end_time - self.start_time
|
46 |
+
self.end_time_formatted = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.end_time))
|
47 |
+
print(f"End time: {self.start_time_formatted}")
|
48 |
+
if self.activity_name:
|
49 |
+
print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
|
50 |
+
else:
|
51 |
+
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
52 |
+
|
53 |
+
|
54 |
def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
|
55 |
image = image.convert("RGBA")
|
56 |
data = image.getdata()
|
|
|
65 |
image.putdata(new_data)
|
66 |
return image
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
pipe = FluxInpaintPipeline.from_pretrained(
|
69 |
"black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(DEVICE)
|
70 |
|
|
|
119 |
gr.Info("Please draw a mask on the image.")
|
120 |
return None, None
|
121 |
|
122 |
+
with calculateDuration("resize image"):
|
123 |
+
width, height = resize_image_dimensions(original_resolution_wh=image.size)
|
124 |
+
resized_image = image.resize((width, height), Image.LANCZOS)
|
125 |
+
resized_mask = mask.resize((width, height), Image.LANCZOS)
|
126 |
+
with calculateDuration("load lora"):
|
127 |
+
pipe.load_lora_weights(lora_path, weight_name=lora_weights)
|
128 |
+
|
129 |
if randomize_seed_checkbox:
|
130 |
seed_slicer = random.randint(0, MAX_SEED)
|
131 |
generator = torch.Generator().manual_seed(seed_slicer)
|
132 |
+
|
133 |
+
with calculateDuration("run pipe"):
|
134 |
+
result = pipe(
|
135 |
+
prompt=f"{input_text} {trigger_word}",
|
136 |
+
image=resized_image,
|
137 |
+
mask_image=resized_mask,
|
138 |
+
width=width,
|
139 |
+
height=height,
|
140 |
+
strength=strength_slider,
|
141 |
+
generator=generator,
|
142 |
+
num_inference_steps=num_inference_steps_slider,
|
143 |
+
joint_attention_kwargs={"scale": lora_scale},
|
144 |
+
).images[0]
|
145 |
+
|
146 |
return result, resized_mask
|
147 |
|
148 |
|
|
|
208 |
with gr.Accordion("Debug", open=False):
|
209 |
output_mask_component = gr.Image(
|
210 |
type='pil', image_mode='RGB', label='Input mask', format="png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
submit_button_component.click(
|
213 |
fn=process,
|