Spaces:
Running
Running
try flux1 official
Browse files
app.py
CHANGED
@@ -1,65 +1,204 @@
|
|
1 |
-
import torch
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
dtype = torch.bfloat16
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
pipe = FluxPipeline.from_pretrained(
|
15 |
-
base_model, controlnet=controlnet, torch_dtype=dtype
|
16 |
-
).to(device)
|
17 |
-
|
18 |
-
pipe.enable_model_cpu_offload()
|
19 |
-
# pipe.to("cuda")
|
20 |
|
21 |
-
|
|
|
22 |
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
strength=strength,
|
42 |
-
num_inference_steps=num_inference_steps,
|
43 |
-
guidance_scale=guidance_scale,
|
44 |
-
).images[0]
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
gr.Slider(0, 1, value=0.95, label="Strength"),
|
56 |
-
gr.Slider(1, 100, value=50, step=1, label="Number of Inference Steps"),
|
57 |
-
gr.Slider(0, 20, value=5, label="Guidance Scale"),
|
58 |
-
gr.Slider(0, 1, value=0.5, label="ControlNet Conditioning Scale"),
|
59 |
-
],
|
60 |
-
outputs=gr.Image(type="pil", label="Output Image"),
|
61 |
-
title="Flux Inpaint AI Model",
|
62 |
-
description="Upload an image and a mask, then provide a prompt to generate an inpainted image.",
|
63 |
-
)
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
# import spaces
|
5 |
+
import torch
|
6 |
+
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
|
7 |
+
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
8 |
+
# from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
9 |
|
10 |
dtype = torch.bfloat16
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
14 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
15 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
|
16 |
+
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
MAX_SEED = np.iinfo(np.int32).max
|
19 |
+
MAX_IMAGE_SIZE = 2048
|
20 |
|
21 |
+
# pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
22 |
|
23 |
+
# @spaces.GPU(duration=75)
|
24 |
+
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)):
|
25 |
+
if randomize_seed:
|
26 |
+
seed = random.randint(0, MAX_SEED)
|
27 |
+
generator = torch.Generator().manual_seed(seed)
|
28 |
+
|
29 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
30 |
+
prompt=prompt,
|
31 |
+
guidance_scale=guidance_scale,
|
32 |
+
num_inference_steps=num_inference_steps,
|
33 |
+
width=width,
|
34 |
+
height=height,
|
35 |
+
generator=generator,
|
36 |
+
output_type="pil",
|
37 |
+
good_vae=good_vae,
|
38 |
+
):
|
39 |
+
yield img, seed
|
40 |
+
|
41 |
+
examples = [
|
42 |
+
"a tiny astronaut hatching from an egg on the moon",
|
43 |
+
"a cat holding a sign that says hello world",
|
44 |
+
"an anime illustration of a wiener schnitzel",
|
45 |
+
]
|
46 |
|
47 |
+
css="""
|
48 |
+
#col-container {
|
49 |
+
margin: 0 auto;
|
50 |
+
max-width: 520px;
|
51 |
+
}
|
52 |
+
"""
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
with gr.Blocks(css=css) as demo:
|
55 |
+
|
56 |
+
with gr.Column(elem_id="col-container"):
|
57 |
+
gr.Markdown(f"""# FLUX.1 [dev]
|
58 |
+
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
59 |
+
[[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)]
|
60 |
+
""")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
|
64 |
+
prompt = gr.Text(
|
65 |
+
label="Prompt",
|
66 |
+
show_label=False,
|
67 |
+
max_lines=1,
|
68 |
+
placeholder="Enter your prompt",
|
69 |
+
container=False,
|
70 |
+
)
|
71 |
+
|
72 |
+
run_button = gr.Button("Run", scale=0)
|
73 |
+
|
74 |
+
result = gr.Image(label="Result", show_label=False)
|
75 |
+
|
76 |
+
with gr.Accordion("Advanced Settings", open=False):
|
77 |
+
|
78 |
+
seed = gr.Slider(
|
79 |
+
label="Seed",
|
80 |
+
minimum=0,
|
81 |
+
maximum=MAX_SEED,
|
82 |
+
step=1,
|
83 |
+
value=0,
|
84 |
+
)
|
85 |
+
|
86 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
|
90 |
+
width = gr.Slider(
|
91 |
+
label="Width",
|
92 |
+
minimum=256,
|
93 |
+
maximum=MAX_IMAGE_SIZE,
|
94 |
+
step=32,
|
95 |
+
value=1024,
|
96 |
+
)
|
97 |
+
|
98 |
+
height = gr.Slider(
|
99 |
+
label="Height",
|
100 |
+
minimum=256,
|
101 |
+
maximum=MAX_IMAGE_SIZE,
|
102 |
+
step=32,
|
103 |
+
value=1024,
|
104 |
+
)
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
|
108 |
+
guidance_scale = gr.Slider(
|
109 |
+
label="Guidance Scale",
|
110 |
+
minimum=1,
|
111 |
+
maximum=15,
|
112 |
+
step=0.1,
|
113 |
+
value=3.5,
|
114 |
+
)
|
115 |
+
|
116 |
+
num_inference_steps = gr.Slider(
|
117 |
+
label="Number of inference steps",
|
118 |
+
minimum=1,
|
119 |
+
maximum=50,
|
120 |
+
step=1,
|
121 |
+
value=28,
|
122 |
+
)
|
123 |
+
|
124 |
+
gr.Examples(
|
125 |
+
examples = examples,
|
126 |
+
fn = infer,
|
127 |
+
inputs = [prompt],
|
128 |
+
outputs = [result, seed],
|
129 |
+
cache_examples="lazy"
|
130 |
+
)
|
131 |
|
132 |
+
gr.on(
|
133 |
+
triggers=[run_button.click, prompt.submit],
|
134 |
+
fn = infer,
|
135 |
+
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
136 |
+
outputs = [result, seed]
|
137 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
demo.launch()
|
140 |
+
# import torch
|
141 |
+
# import gradio as gr
|
142 |
+
# from diffusers.pipelines.flux.pipeline_flux import FluxPipeline
|
143 |
+
# from diffusers.models.controlnet_flux import FluxControlNetModel
|
144 |
+
# from controlnet_aux import CannyDetector
|
145 |
+
#
|
146 |
+
# dtype = torch.bfloat16
|
147 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
148 |
+
#
|
149 |
+
# base_model = "black-forest-labs/FLUX.1-schnell"
|
150 |
+
# controlnet_model = "YishaoAI/flux-dev-controlnet-canny-kid-clothes"
|
151 |
+
#
|
152 |
+
# controlnet = FluxControlNetModel.from_pretrained(controlnet_model, torch_dtype=dtype)
|
153 |
+
# pipe = FluxPipeline.from_pretrained(
|
154 |
+
# base_model, controlnet=controlnet, torch_dtype=dtype
|
155 |
+
# ).to(device)
|
156 |
+
#
|
157 |
+
# pipe.enable_model_cpu_offload()
|
158 |
+
# # pipe.to("cuda")
|
159 |
+
#
|
160 |
+
# canny = CannyDetector()
|
161 |
+
#
|
162 |
+
#
|
163 |
+
# def inpaint(
|
164 |
+
# image,
|
165 |
+
# mask,
|
166 |
+
# prompt,
|
167 |
+
# strength,
|
168 |
+
# num_inference_steps,
|
169 |
+
# guidance_scale,
|
170 |
+
# controlnet_conditioning_scale,
|
171 |
+
# ):
|
172 |
+
# canny_image = canny(image)
|
173 |
+
#
|
174 |
+
# image_res = pipe(
|
175 |
+
# prompt,
|
176 |
+
# image=image,
|
177 |
+
# control_image=canny_image,
|
178 |
+
# controlnet_conditioning_scale=controlnet_conditioning_scale,
|
179 |
+
# mask_image=mask,
|
180 |
+
# strength=strength,
|
181 |
+
# num_inference_steps=num_inference_steps,
|
182 |
+
# guidance_scale=guidance_scale,
|
183 |
+
# ).images[0]
|
184 |
+
#
|
185 |
+
# return image_res
|
186 |
+
#
|
187 |
+
#
|
188 |
+
# iface = gr.Interface(
|
189 |
+
# fn=inpaint,
|
190 |
+
# inputs=[
|
191 |
+
# gr.Image(type="pil", label="Input Image"),
|
192 |
+
# gr.Image(type="pil", label="Mask Image"),
|
193 |
+
# gr.Textbox(label="Prompt"),
|
194 |
+
# gr.Slider(0, 1, value=0.95, label="Strength"),
|
195 |
+
# gr.Slider(1, 100, value=50, step=1, label="Number of Inference Steps"),
|
196 |
+
# gr.Slider(0, 20, value=5, label="Guidance Scale"),
|
197 |
+
# gr.Slider(0, 1, value=0.5, label="ControlNet Conditioning Scale"),
|
198 |
+
# ],
|
199 |
+
# outputs=gr.Image(type="pil", label="Output Image"),
|
200 |
+
# title="Flux Inpaint AI Model",
|
201 |
+
# description="Upload an image and a mask, then provide a prompt to generate an inpainted image.",
|
202 |
+
# )
|
203 |
+
#
|
204 |
+
# iface.launch()
|