Spaces:
Running
on
Zero
Running
on
Zero
Create flux_dev.py
Browse files- flux_dev.py +133 -0
flux_dev.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
import torch
|
5 |
+
import spaces
|
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 |
+
from huggingface_hub import login
|
11 |
+
import os
|
12 |
+
token = os.getenv("HF_TOKEN")
|
13 |
+
login(token=token)
|
14 |
+
|
15 |
+
dtype = torch.bfloat16
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
|
18 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
19 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
20 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
|
21 |
+
torch.cuda.empty_cache()
|
22 |
+
|
23 |
+
MAX_SEED = np.iinfo(np.int32).max
|
24 |
+
MAX_IMAGE_SIZE = 2048
|
25 |
+
|
26 |
+
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
27 |
+
|
28 |
+
@spaces.GPU(duration=75)
|
29 |
+
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)):
|
30 |
+
if randomize_seed:
|
31 |
+
seed = random.randint(0, MAX_SEED)
|
32 |
+
generator = torch.Generator().manual_seed(seed)
|
33 |
+
|
34 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
35 |
+
prompt=prompt,
|
36 |
+
guidance_scale=guidance_scale,
|
37 |
+
num_inference_steps=num_inference_steps,
|
38 |
+
width=width,
|
39 |
+
height=height,
|
40 |
+
generator=generator,
|
41 |
+
output_type="pil",
|
42 |
+
good_vae=good_vae,
|
43 |
+
):
|
44 |
+
yield img, seed
|
45 |
+
|
46 |
+
|
47 |
+
def create_flux_tab():
|
48 |
+
examples = [
|
49 |
+
"a tiny astronaut hatching from an egg on the moon",
|
50 |
+
"a cat holding a sign that says hello world",
|
51 |
+
"an anime illustration of a wiener schnitzel",
|
52 |
+
]
|
53 |
+
|
54 |
+
css = """
|
55 |
+
#col-container {
|
56 |
+
margin: 0 auto;
|
57 |
+
max-width: 520px;
|
58 |
+
}
|
59 |
+
"""
|
60 |
+
|
61 |
+
with gr.Blocks(css=css) as flux_demo:
|
62 |
+
with gr.Column(elem_id="col-container"):
|
63 |
+
gr.Markdown(f"""# FLUX.1 [dev]""")
|
64 |
+
with gr.Row():
|
65 |
+
prompt = gr.Text(
|
66 |
+
label="Prompt",
|
67 |
+
show_label=False,
|
68 |
+
max_lines=1,
|
69 |
+
placeholder="Enter your prompt",
|
70 |
+
container=False,
|
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 |
+
seed = gr.Slider(
|
78 |
+
label="Seed",
|
79 |
+
minimum=0,
|
80 |
+
maximum=MAX_SEED,
|
81 |
+
step=1,
|
82 |
+
value=0,
|
83 |
+
)
|
84 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
width = gr.Slider(
|
88 |
+
label="Width",
|
89 |
+
minimum=256,
|
90 |
+
maximum=MAX_IMAGE_SIZE,
|
91 |
+
step=32,
|
92 |
+
value=1024,
|
93 |
+
)
|
94 |
+
height = gr.Slider(
|
95 |
+
label="Height",
|
96 |
+
minimum=256,
|
97 |
+
maximum=MAX_IMAGE_SIZE,
|
98 |
+
step=32,
|
99 |
+
value=1024,
|
100 |
+
)
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
guidance_scale = gr.Slider(
|
104 |
+
label="Guidance Scale",
|
105 |
+
minimum=1,
|
106 |
+
maximum=15,
|
107 |
+
step=0.1,
|
108 |
+
value=3.5,
|
109 |
+
)
|
110 |
+
num_inference_steps = gr.Slider(
|
111 |
+
label="Number of inference steps",
|
112 |
+
minimum=1,
|
113 |
+
maximum=50,
|
114 |
+
step=1,
|
115 |
+
value=28,
|
116 |
+
)
|
117 |
+
|
118 |
+
gr.Examples(
|
119 |
+
examples=examples,
|
120 |
+
fn=infer,
|
121 |
+
inputs=[prompt],
|
122 |
+
outputs=[result, seed],
|
123 |
+
cache_examples="lazy"
|
124 |
+
)
|
125 |
+
|
126 |
+
gr.on(
|
127 |
+
triggers=[run_button.click, prompt.submit],
|
128 |
+
fn=infer,
|
129 |
+
inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
130 |
+
outputs=[result, seed]
|
131 |
+
)
|
132 |
+
|
133 |
+
return flux_demo
|