Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,165 +1,106 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
-
|
5 |
import spaces
|
6 |
import os
|
7 |
-
from huggingface_hub import login
|
8 |
-
from diffusers import StableDiffusionPipeline
|
9 |
import torch
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
hf_token = os.environ.get("HF_TOKEN")
|
13 |
if hf_token:
|
14 |
-
login(token=hf_token)
|
15 |
|
|
|
16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
-
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
"black-forest-labs/FLUX.1-dev",
|
21 |
-
torch_dtype=
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
-
|
26 |
-
pipe =
|
|
|
27 |
|
28 |
MAX_SEED = np.iinfo(np.int32).max
|
29 |
-
MAX_IMAGE_SIZE =
|
30 |
-
|
31 |
-
@spaces.GPU
|
32 |
-
def infer(
|
33 |
-
prompt,
|
34 |
-
negative_prompt,
|
35 |
-
seed,
|
36 |
-
randomize_seed,
|
37 |
-
width,
|
38 |
-
height,
|
39 |
-
guidance_scale,
|
40 |
-
num_inference_steps,
|
41 |
-
progress=gr.Progress(track_tqdm=True),
|
42 |
-
):
|
43 |
if randomize_seed:
|
44 |
seed = random.randint(0, MAX_SEED)
|
45 |
-
|
46 |
generator = torch.Generator().manual_seed(seed)
|
47 |
|
48 |
-
#
|
49 |
-
prompt = "XTON "
|
50 |
|
51 |
-
|
52 |
prompt=prompt,
|
53 |
-
negative_prompt=negative_prompt,
|
54 |
guidance_scale=guidance_scale,
|
55 |
num_inference_steps=num_inference_steps,
|
56 |
width=width,
|
57 |
height=height,
|
58 |
generator=generator,
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
|
65 |
examples = [
|
66 |
-
"
|
67 |
-
"
|
68 |
-
"
|
69 |
]
|
70 |
|
71 |
css = """
|
72 |
#col-container {
|
73 |
margin: 0 auto;
|
74 |
-
max-width:
|
75 |
}
|
76 |
"""
|
77 |
|
78 |
-
with gr.Blocks(css=css) as
|
79 |
with gr.Column(elem_id="col-container"):
|
80 |
-
gr.Markdown("
|
81 |
|
82 |
with gr.Row():
|
83 |
-
prompt = gr.Text(
|
84 |
-
|
85 |
-
show_label=False,
|
86 |
-
max_lines=1,
|
87 |
-
placeholder="Enter your prompt",
|
88 |
-
container=False,
|
89 |
-
)
|
90 |
-
|
91 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
92 |
|
93 |
result = gr.Image(label="Result", show_label=False)
|
94 |
|
95 |
with gr.Accordion("Advanced Settings", open=False):
|
96 |
-
|
97 |
-
label="Negative prompt",
|
98 |
-
max_lines=1,
|
99 |
-
placeholder="Enter a negative prompt",
|
100 |
-
visible=False,
|
101 |
-
)
|
102 |
-
|
103 |
-
seed = gr.Slider(
|
104 |
-
label="Seed",
|
105 |
-
minimum=0,
|
106 |
-
maximum=MAX_SEED,
|
107 |
-
step=1,
|
108 |
-
value=0,
|
109 |
-
)
|
110 |
-
|
111 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
112 |
|
113 |
with gr.Row():
|
114 |
-
width = gr.Slider(
|
115 |
-
|
116 |
-
minimum=256,
|
117 |
-
maximum=MAX_IMAGE_SIZE,
|
118 |
-
step=32,
|
119 |
-
value=1024, # Replace with defaults that work for your model
|
120 |
-
)
|
121 |
-
|
122 |
-
height = gr.Slider(
|
123 |
-
label="Height",
|
124 |
-
minimum=256,
|
125 |
-
maximum=MAX_IMAGE_SIZE,
|
126 |
-
step=32,
|
127 |
-
value=1024, # Replace with defaults that work for your model
|
128 |
-
)
|
129 |
|
130 |
with gr.Row():
|
131 |
-
guidance_scale = gr.Slider(
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
value=0.0, # Replace with defaults that work for your model
|
137 |
-
)
|
138 |
-
|
139 |
-
num_inference_steps = gr.Slider(
|
140 |
-
label="Number of inference steps",
|
141 |
-
minimum=1,
|
142 |
-
maximum=50,
|
143 |
-
step=1,
|
144 |
-
value=2, # Replace with defaults that work for your model
|
145 |
-
)
|
146 |
-
|
147 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
148 |
gr.on(
|
149 |
triggers=[run_button.click, prompt.submit],
|
150 |
fn=infer,
|
151 |
-
inputs=[
|
152 |
-
prompt,
|
153 |
-
negative_prompt,
|
154 |
-
seed,
|
155 |
-
randomize_seed,
|
156 |
-
width,
|
157 |
-
height,
|
158 |
-
guidance_scale,
|
159 |
-
num_inference_steps,
|
160 |
-
],
|
161 |
outputs=[result, seed],
|
162 |
)
|
163 |
|
164 |
if __name__ == "__main__":
|
165 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
4 |
import spaces
|
5 |
import os
|
|
|
|
|
6 |
import torch
|
7 |
|
8 |
+
from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
|
9 |
+
from huggingface_hub import login
|
10 |
+
from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
|
11 |
+
|
12 |
+
# Authenticate for gated repo access
|
13 |
hf_token = os.environ.get("HF_TOKEN")
|
14 |
if hf_token:
|
15 |
+
login(token=hf_token)
|
16 |
|
17 |
+
dtype = torch.bfloat16
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
19 |
|
20 |
+
# Load VAEs
|
21 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
22 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
23 |
+
|
24 |
+
# Load base model with TAEF1 injected
|
25 |
+
pipe = DiffusionPipeline.from_pretrained(
|
26 |
"black-forest-labs/FLUX.1-dev",
|
27 |
+
torch_dtype=dtype,
|
28 |
+
token=hf_token,
|
29 |
+
vae=taef1
|
30 |
+
).to(device)
|
31 |
|
32 |
+
# Inject method + apply LoRA
|
33 |
+
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
34 |
+
pipe.load_lora_weights("ZennyKenny/flux_lora_natalie-diffusion")
|
35 |
|
36 |
MAX_SEED = np.iinfo(np.int32).max
|
37 |
+
MAX_IMAGE_SIZE = 2048
|
38 |
+
|
39 |
+
@spaces.GPU(duration=75)
|
40 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
if randomize_seed:
|
42 |
seed = random.randint(0, MAX_SEED)
|
|
|
43 |
generator = torch.Generator().manual_seed(seed)
|
44 |
|
45 |
+
# Prepend XTON
|
46 |
+
prompt = f"XTON {prompt}"
|
47 |
|
48 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
49 |
prompt=prompt,
|
|
|
50 |
guidance_scale=guidance_scale,
|
51 |
num_inference_steps=num_inference_steps,
|
52 |
width=width,
|
53 |
height=height,
|
54 |
generator=generator,
|
55 |
+
output_type="pil",
|
56 |
+
good_vae=good_vae,
|
57 |
+
):
|
58 |
+
yield img, seed
|
59 |
|
60 |
|
61 |
examples = [
|
62 |
+
"a man walking a in the forest",
|
63 |
+
"a viking ship sailing down a river",
|
64 |
+
"a woman resting by an open fire",
|
65 |
]
|
66 |
|
67 |
css = """
|
68 |
#col-container {
|
69 |
margin: 0 auto;
|
70 |
+
max-width: 520px;
|
71 |
}
|
72 |
"""
|
73 |
|
74 |
+
with gr.Blocks(css=css) as demo:
|
75 |
with gr.Column(elem_id="col-container"):
|
76 |
+
gr.Markdown("# FLUX.1 [Natalie LoRA]\nModel prompt-augmented with `XTON`, adapted from [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)")
|
77 |
|
78 |
with gr.Row():
|
79 |
+
prompt = gr.Text(label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False)
|
80 |
+
run_button = gr.Button("Run", scale=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
result = gr.Image(label="Result", show_label=False)
|
83 |
|
84 |
with gr.Accordion("Advanced Settings", open=False):
|
85 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
87 |
|
88 |
with gr.Row():
|
89 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
90 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
with gr.Row():
|
93 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
|
94 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=28)
|
95 |
+
|
96 |
+
gr.Examples(examples=examples, fn=infer, inputs=[prompt], outputs=[result, seed], cache_examples="lazy")
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
gr.on(
|
99 |
triggers=[run_button.click, prompt.submit],
|
100 |
fn=infer,
|
101 |
+
inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
outputs=[result, seed],
|
103 |
)
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
+
demo.launch()
|