Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
|
4 |
import spaces
|
5 |
import torch
|
6 |
-
import spaces
|
7 |
import random
|
8 |
|
9 |
from diffusers import FluxControlPipeline, FluxTransformer2DModel
|
@@ -15,8 +13,25 @@ MAX_IMAGE_SIZE = 2048
|
|
15 |
pipe = FluxControlPipeline.from_pretrained("black-forest-labs/FLUX.1-Depth-dev", torch_dtype=torch.bfloat16).to("cuda")
|
16 |
processor = DepthPreprocessor.from_pretrained("LiheYoung/depth-anything-large-hf")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
@spaces.GPU
|
19 |
-
def infer(control_image, prompt, seed=42, randomize_seed=False, width=1024, height=1024,
|
|
|
20 |
|
21 |
if randomize_seed:
|
22 |
seed = random.randint(0, MAX_SEED)
|
@@ -32,12 +47,6 @@ def infer(control_image, prompt, seed=42, randomize_seed=False, width=1024, heig
|
|
32 |
generator=torch.Generator().manual_seed(seed),
|
33 |
).images[0]
|
34 |
return image, seed
|
35 |
-
|
36 |
-
examples = [
|
37 |
-
"a tiny astronaut hatching from an egg on the moon",
|
38 |
-
"a cat holding a sign that says hello world",
|
39 |
-
"an anime illustration of a wiener schnitzel",
|
40 |
-
]
|
41 |
|
42 |
css="""
|
43 |
#col-container {
|
@@ -49,14 +58,24 @@ css="""
|
|
49 |
with gr.Blocks(css=css) as demo:
|
50 |
|
51 |
with gr.Column(elem_id="col-container"):
|
52 |
-
gr.Markdown(f"""# FLUX.1 Depth [dev]
|
53 |
12B param rectified flow transformer structural conditioning tuned, guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
54 |
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
55 |
""")
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
control_image = gr.Image(label="Upload the image for control", type="pil")
|
58 |
with gr.Row():
|
59 |
-
|
60 |
prompt = gr.Text(
|
61 |
label="Prompt",
|
62 |
show_label=False,
|
@@ -64,13 +83,11 @@ with gr.Blocks(css=css) as demo:
|
|
64 |
placeholder="Enter your prompt",
|
65 |
container=False,
|
66 |
)
|
67 |
-
|
68 |
run_button = gr.Button("Run", scale=0)
|
69 |
|
70 |
result = gr.Image(label="Result", show_label=False)
|
71 |
|
72 |
with gr.Accordion("Advanced Settings", open=False):
|
73 |
-
|
74 |
seed = gr.Slider(
|
75 |
label="Seed",
|
76 |
minimum=0,
|
@@ -82,7 +99,6 @@ with gr.Blocks(css=css) as demo:
|
|
82 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
83 |
|
84 |
with gr.Row():
|
85 |
-
|
86 |
width = gr.Slider(
|
87 |
label="Width",
|
88 |
minimum=256,
|
@@ -100,7 +116,6 @@ with gr.Blocks(css=css) as demo:
|
|
100 |
)
|
101 |
|
102 |
with gr.Row():
|
103 |
-
|
104 |
guidance_scale = gr.Slider(
|
105 |
label="Guidance Scale",
|
106 |
minimum=1,
|
@@ -117,11 +132,24 @@ with gr.Blocks(css=css) as demo:
|
|
117 |
value=28,
|
118 |
)
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
gr.on(
|
121 |
triggers=[run_button.click, prompt.submit],
|
122 |
-
fn
|
123 |
-
inputs
|
124 |
-
outputs
|
125 |
)
|
126 |
|
127 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
import spaces
|
4 |
import torch
|
|
|
5 |
import random
|
6 |
|
7 |
from diffusers import FluxControlPipeline, FluxTransformer2DModel
|
|
|
13 |
pipe = FluxControlPipeline.from_pretrained("black-forest-labs/FLUX.1-Depth-dev", torch_dtype=torch.bfloat16).to("cuda")
|
14 |
processor = DepthPreprocessor.from_pretrained("LiheYoung/depth-anything-large-hf")
|
15 |
|
16 |
+
def load_lora(lora_path):
|
17 |
+
if not lora_path.strip():
|
18 |
+
return "Please provide a valid LoRA path"
|
19 |
+
try:
|
20 |
+
pipe.load_lora_weights(lora_path)
|
21 |
+
return f"Successfully loaded LoRA weights from {lora_path}"
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error loading LoRA weights: {str(e)}"
|
24 |
+
|
25 |
+
def unload_lora():
|
26 |
+
try:
|
27 |
+
pipe.unload_lora_weights()
|
28 |
+
return "Successfully unloaded LoRA weights"
|
29 |
+
except Exception as e:
|
30 |
+
return f"Error unloading LoRA weights: {str(e)}"
|
31 |
+
|
32 |
@spaces.GPU
|
33 |
+
def infer(control_image, prompt, seed=42, randomize_seed=False, width=1024, height=1024,
|
34 |
+
guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
35 |
|
36 |
if randomize_seed:
|
37 |
seed = random.randint(0, MAX_SEED)
|
|
|
47 |
generator=torch.Generator().manual_seed(seed),
|
48 |
).images[0]
|
49 |
return image, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
css="""
|
52 |
#col-container {
|
|
|
58 |
with gr.Blocks(css=css) as demo:
|
59 |
|
60 |
with gr.Column(elem_id="col-container"):
|
61 |
+
gr.Markdown(f"""# FLUX.1 Depth [dev] with LoRA Support
|
62 |
12B param rectified flow transformer structural conditioning tuned, guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
63 |
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
64 |
""")
|
65 |
|
66 |
+
# LoRA controls
|
67 |
+
with gr.Row():
|
68 |
+
lora_path = gr.Textbox(
|
69 |
+
label="HuggingFace LoRA Path",
|
70 |
+
placeholder="e.g., Borcherding/FLUX.1-dev-LoRA-AutumnSpringTrees"
|
71 |
+
)
|
72 |
+
load_lora_btn = gr.Button("Load LoRA")
|
73 |
+
unload_lora_btn = gr.Button("Unload LoRA")
|
74 |
+
|
75 |
+
lora_status = gr.Textbox(label="LoRA Status", interactive=False)
|
76 |
+
|
77 |
control_image = gr.Image(label="Upload the image for control", type="pil")
|
78 |
with gr.Row():
|
|
|
79 |
prompt = gr.Text(
|
80 |
label="Prompt",
|
81 |
show_label=False,
|
|
|
83 |
placeholder="Enter your prompt",
|
84 |
container=False,
|
85 |
)
|
|
|
86 |
run_button = gr.Button("Run", scale=0)
|
87 |
|
88 |
result = gr.Image(label="Result", show_label=False)
|
89 |
|
90 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
91 |
seed = gr.Slider(
|
92 |
label="Seed",
|
93 |
minimum=0,
|
|
|
99 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
100 |
|
101 |
with gr.Row():
|
|
|
102 |
width = gr.Slider(
|
103 |
label="Width",
|
104 |
minimum=256,
|
|
|
116 |
)
|
117 |
|
118 |
with gr.Row():
|
|
|
119 |
guidance_scale = gr.Slider(
|
120 |
label="Guidance Scale",
|
121 |
minimum=1,
|
|
|
132 |
value=28,
|
133 |
)
|
134 |
|
135 |
+
# Event handlers
|
136 |
+
load_lora_btn.click(
|
137 |
+
fn=load_lora,
|
138 |
+
inputs=[lora_path],
|
139 |
+
outputs=[lora_status]
|
140 |
+
)
|
141 |
+
|
142 |
+
unload_lora_btn.click(
|
143 |
+
fn=unload_lora,
|
144 |
+
inputs=[],
|
145 |
+
outputs=[lora_status]
|
146 |
+
)
|
147 |
+
|
148 |
gr.on(
|
149 |
triggers=[run_button.click, prompt.submit],
|
150 |
+
fn=infer,
|
151 |
+
inputs=[control_image, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
152 |
+
outputs=[result, seed]
|
153 |
)
|
154 |
|
155 |
demo.launch()
|