Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -21,19 +21,25 @@ if not os.path.exists(model_path):
|
|
21 |
with open(model_path, 'wb') as f:
|
22 |
f.write(response.content)
|
23 |
|
24 |
-
#
|
25 |
name = "flux-dev"
|
26 |
device = torch.device("cuda")
|
|
|
27 |
is_schnell = name == "flux-schnell"
|
28 |
|
29 |
def preprocess_image(image, target_width, target_height, crop=True):
|
30 |
if crop:
|
31 |
image = c_crop(image) # Crop the image to square
|
32 |
original_width, original_height = image.size
|
|
|
|
|
33 |
scale = max(target_width / original_width, target_height / original_height)
|
34 |
resized_width = int(scale * original_width)
|
35 |
resized_height = int(scale * original_height)
|
|
|
36 |
image = image.resize((resized_width, resized_height), Image.LANCZOS)
|
|
|
|
|
37 |
left = (resized_width - target_width) // 2
|
38 |
top = (resized_height - target_height) // 2
|
39 |
image = image.crop((left, top, left + target_width, top + target_height))
|
@@ -50,12 +56,13 @@ def preprocess_canny_image(image, target_width, target_height, crop=True):
|
|
50 |
@spaces.GPU(duration=120)
|
51 |
def generate_image(prompt, control_image, num_steps=50, guidance=4, width=512, height=512, seed=42, random_seed=False):
|
52 |
if random_seed:
|
53 |
-
seed = np.random.randint(0,
|
54 |
|
55 |
if not os.path.isdir("./controlnet_results/"):
|
56 |
os.makedirs("./controlnet_results/")
|
57 |
|
58 |
torch_device = torch.device("cuda")
|
|
|
59 |
torch.cuda.empty_cache() # Clear GPU cache
|
60 |
|
61 |
model = load_flow_model(name, device=torch_device)
|
@@ -92,26 +99,22 @@ def generate_image(prompt, control_image, num_steps=50, guidance=4, width=512, h
|
|
92 |
|
93 |
return [processed_input, output_img] # Return both images for slider
|
94 |
|
95 |
-
def update_value(name, value):
|
96 |
-
return f"{name}: {value}"
|
97 |
-
|
98 |
interface = gr.Interface(
|
99 |
fn=generate_image,
|
100 |
inputs=[
|
101 |
gr.Textbox(label="Prompt"),
|
102 |
gr.Image(type="pil", label="Control Image"),
|
103 |
-
gr.Slider(step=1, minimum=1, maximum=64, value=28, label="Num Steps"
|
104 |
-
gr.Slider(minimum=0.1, maximum=10, value=4, label="Guidance"
|
105 |
-
gr.Slider(minimum=128, maximum=1024, step=128, value=512, label="Width"
|
106 |
-
gr.Slider(minimum=128, maximum=1024, step=128, value=512, label="Height"
|
107 |
-
gr.
|
108 |
gr.Checkbox(label="Random Seed")
|
109 |
],
|
110 |
outputs=ImageSlider(label="Before / After"), # Use ImageSlider as the output
|
111 |
title="FLUX.1 Controlnet Canny",
|
112 |
-
description="Generate images using ControlNet and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]"
|
113 |
-
live=True,
|
114 |
)
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
-
interface.launch()
|
|
|
21 |
with open(model_path, 'wb') as f:
|
22 |
f.write(response.content)
|
23 |
|
24 |
+
# Source: https://github.com/XLabs-AI/x-flux.git
|
25 |
name = "flux-dev"
|
26 |
device = torch.device("cuda")
|
27 |
+
offload = False
|
28 |
is_schnell = name == "flux-schnell"
|
29 |
|
30 |
def preprocess_image(image, target_width, target_height, crop=True):
|
31 |
if crop:
|
32 |
image = c_crop(image) # Crop the image to square
|
33 |
original_width, original_height = image.size
|
34 |
+
|
35 |
+
# Resize to match the target size without stretching
|
36 |
scale = max(target_width / original_width, target_height / original_height)
|
37 |
resized_width = int(scale * original_width)
|
38 |
resized_height = int(scale * original_height)
|
39 |
+
|
40 |
image = image.resize((resized_width, resized_height), Image.LANCZOS)
|
41 |
+
|
42 |
+
# Center crop to match the target dimensions
|
43 |
left = (resized_width - target_width) // 2
|
44 |
top = (resized_height - target_height) // 2
|
45 |
image = image.crop((left, top, left + target_width, top + target_height))
|
|
|
56 |
@spaces.GPU(duration=120)
|
57 |
def generate_image(prompt, control_image, num_steps=50, guidance=4, width=512, height=512, seed=42, random_seed=False):
|
58 |
if random_seed:
|
59 |
+
seed = np.random.randint(0, 10000)
|
60 |
|
61 |
if not os.path.isdir("./controlnet_results/"):
|
62 |
os.makedirs("./controlnet_results/")
|
63 |
|
64 |
torch_device = torch.device("cuda")
|
65 |
+
|
66 |
torch.cuda.empty_cache() # Clear GPU cache
|
67 |
|
68 |
model = load_flow_model(name, device=torch_device)
|
|
|
99 |
|
100 |
return [processed_input, output_img] # Return both images for slider
|
101 |
|
|
|
|
|
|
|
102 |
interface = gr.Interface(
|
103 |
fn=generate_image,
|
104 |
inputs=[
|
105 |
gr.Textbox(label="Prompt"),
|
106 |
gr.Image(type="pil", label="Control Image"),
|
107 |
+
gr.Slider(step=1, minimum=1, maximum=64, value=28, label="Num Steps"),
|
108 |
+
gr.Slider(minimum=0.1, maximum=10, value=4, label="Guidance"),
|
109 |
+
gr.Slider(minimum=128, maximum=1024, step=128, value=512, label="Width"),
|
110 |
+
gr.Slider(minimum=128, maximum=1024, step=128, value=512, label="Height"),
|
111 |
+
gr.Number(value=42, label="Seed"),
|
112 |
gr.Checkbox(label="Random Seed")
|
113 |
],
|
114 |
outputs=ImageSlider(label="Before / After"), # Use ImageSlider as the output
|
115 |
title="FLUX.1 Controlnet Canny",
|
116 |
+
description="Generate images using ControlNet and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]"
|
|
|
117 |
)
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
+
interface.launch()
|