Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,83 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import torch
|
3 |
-
from diffusers import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
)
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
import torch
|
5 |
+
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
|
6 |
+
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
7 |
+
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
8 |
+
|
9 |
+
dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
13 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
14 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
|
15 |
+
torch.cuda.empty_cache()
|
16 |
+
|
17 |
+
MAX_SEED = np.iinfo(np.int32).max
|
18 |
+
MAX_IMAGE_SIZE = 2048
|
19 |
+
|
20 |
+
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
21 |
+
|
22 |
+
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)):
|
23 |
+
if randomize_seed:
|
24 |
+
seed = random.randint(0, MAX_SEED)
|
25 |
+
generator = torch.Generator().manual_seed(seed)
|
26 |
+
|
27 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
28 |
+
prompt=prompt,
|
29 |
+
guidance_scale=guidance_scale,
|
30 |
+
num_inference_steps=num_inference_steps,
|
31 |
+
width=width,
|
32 |
+
height=height,
|
33 |
+
generator=generator,
|
34 |
+
output_type="pil",
|
35 |
+
good_vae=good_vae,
|
36 |
+
):
|
37 |
+
yield img, seed
|
38 |
+
|
39 |
+
examples = [
|
40 |
+
"a tiny astronaut hatching from an egg on the moon",
|
41 |
+
"a cat holding a sign that says hello world",
|
42 |
+
"an anime illustration of a wiener schnitzel",
|
43 |
+
]
|
44 |
+
|
45 |
+
css = """
|
46 |
+
#col-container {
|
47 |
+
margin: 0 auto;
|
48 |
+
max-width: 520px;
|
49 |
+
}
|
50 |
+
"""
|
51 |
+
|
52 |
+
with gr.Blocks(css=css) as demo:
|
53 |
+
with gr.Column(elem_id="col-container"):
|
54 |
+
gr.Markdown("""# FLUX.1 [dev]
|
55 |
+
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
56 |
+
[[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)]
|
57 |
+
""")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
prompt = gr.Textbox(
|
61 |
+
label="Prompt",
|
62 |
+
show_label=False,
|
63 |
+
max_lines=1,
|
64 |
+
placeholder="Enter your prompt",
|
65 |
+
container=False,
|
66 |
+
)
|
67 |
+
run_button = gr.Button("Run", scale=0)
|
68 |
+
|
69 |
+
result = gr.Image(label="Result", show_label=False)
|
70 |
+
|
71 |
+
with gr.Accordion("Advanced Settings", open=False):
|
72 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
73 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
77 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
|
81 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=28)
|
82 |
+
|
83 |
+
gr.Examples(examples=examples, inputs=[prompt], outputs=[
|