black-forest-labs/FLUX.1-dev
9.6k
1import gradio as gr2import numpy as np3import random4import spaces5import torch6from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL7from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast8from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images9 10dtype = torch.bfloat1611device = "cuda" if torch.cuda.is_available() else "cpu"12 13taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)14good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)15pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)16torch.cuda.empty_cache()17 18MAX_SEED = np.iinfo(np.int32).max19MAX_IMAGE_SIZE = 204820 21pipe.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)24def 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, seed40 41examples = [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 47css="""48#col-container {49 margin: 0 auto;50 max-width: 520px;51}52"""53 54with gr.Blocks(css=css) as demo:55 56 with gr.Column(elem_id="col-container"):57 gr.Markdown(f"""> FLUX.2 [dev] is here! ✨ [Try it out here](https://huggingface.co/spaces/black-forest-labs/FLUX.2-dev)58 59# FLUX.1 [dev]6012B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) 61[[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)]62 """)63 64 with gr.Row():65 66 prompt = gr.Text(67 label="Prompt",68 show_label=False,69 max_lines=1,70 placeholder="Enter your prompt",71 container=False,72 )73 74 run_button = gr.Button("Run", scale=0)75 76 result = gr.Image(label="Result", show_label=False)77 78 with gr.Accordion("Advanced Settings", open=False):79 80 seed = gr.Slider(81 label="Seed",82 minimum=0,83 maximum=MAX_SEED,84 step=1,85 value=0,86 )87 88 randomize_seed = gr.Checkbox(label="Randomize seed", value=True)89 90 with gr.Row():91 92 width = gr.Slider(93 label="Width",94 minimum=256,95 maximum=MAX_IMAGE_SIZE,96 step=32,97 value=1024,98 )99 100 height = gr.Slider(101 label="Height",102 minimum=256,103 maximum=MAX_IMAGE_SIZE,104 step=32,105 value=1024,106 )107 108 with gr.Row():109 110 guidance_scale = gr.Slider(111 label="Guidance Scale",112 minimum=1,113 maximum=15,114 step=0.1,115 value=3.5,116 )117 118 num_inference_steps = gr.Slider(119 label="Number of inference steps",120 minimum=1,121 maximum=50,122 step=1,123 value=28,124 )125 126 gr.Examples(127 examples = examples,128 fn = infer,129 inputs = [prompt],130 outputs = [result, seed],131 cache_examples="lazy"132 )133 134 gr.on(135 triggers=[run_button.click, prompt.submit],136 fn = infer,137 inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],138 outputs = [result, seed]139 )140 141demo.launch()