CoolFace
Apppublic

multimodalart/FLUX.1-dev-quantized

sourceHugging Facemitupdated 1y agoView on Hugging Face
4likes
app.py142 linesDownload Raw Back to root
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_images9from torchao.quantization.quant_api import Int8WeightOnlyConfig, quantize_10 11dtype = torch.bfloat1612device = "cuda" if torch.cuda.is_available() else "cpu"13 14taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)15good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)16pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)17quantize_(pipe.transformer, Int8WeightOnlyConfig())18 19torch.cuda.empty_cache()20 21MAX_SEED = np.iinfo(np.int32).max22MAX_IMAGE_SIZE = 204823 24pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)25 26@spaces.GPU(duration=75)27def 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)):28    if randomize_seed:29        seed = random.randint(0, MAX_SEED)30    generator = torch.Generator().manual_seed(seed)31    32    for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(33            prompt=prompt,34            guidance_scale=guidance_scale,35            num_inference_steps=num_inference_steps,36            width=width,37            height=height,38            generator=generator,39            output_type="pil",40            good_vae=good_vae,41        ):42            yield img, seed43    44examples = [45    "a tiny astronaut hatching from an egg on the moon",46    "a cat holding a sign that says hello world",47    "an anime illustration of a wiener schnitzel",48]49 50css="""51#col-container {52    margin: 0 auto;53    max-width: 520px;54}55"""56 57with gr.Blocks(css=css) as demo:58    59    with gr.Column(elem_id="col-container"):60        gr.Markdown(f"""# FLUX.1 [dev] 8-bit quantized6112B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)  62[[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)]63        """)64        65        with gr.Row():66            67            prompt = gr.Text(68                label="Prompt",69                show_label=False,70                max_lines=1,71                placeholder="Enter your prompt",72                container=False,73            )74            75            run_button = gr.Button("Run", scale=0)76        77        result = gr.Image(label="Result", show_label=False)78        79        with gr.Accordion("Advanced Settings", open=False):80            81            seed = gr.Slider(82                label="Seed",83                minimum=0,84                maximum=MAX_SEED,85                step=1,86                value=0,87            )88            89            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)90            91            with gr.Row():92                93                width = gr.Slider(94                    label="Width",95                    minimum=256,96                    maximum=MAX_IMAGE_SIZE,97                    step=32,98                    value=1024,99                )100                101                height = gr.Slider(102                    label="Height",103                    minimum=256,104                    maximum=MAX_IMAGE_SIZE,105                    step=32,106                    value=1024,107                )108            109            with gr.Row():110 111                guidance_scale = gr.Slider(112                    label="Guidance Scale",113                    minimum=1,114                    maximum=15,115                    step=0.1,116                    value=3.5,117                )118  119                num_inference_steps = gr.Slider(120                    label="Number of inference steps",121                    minimum=1,122                    maximum=50,123                    step=1,124                    value=28,125                )126        127        gr.Examples(128            examples = examples,129            fn = infer,130            inputs = [prompt],131            outputs = [result, seed],132            cache_examples="lazy"133        )134 135    gr.on(136        triggers=[run_button.click, prompt.submit],137        fn = infer,138        inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],139        outputs = [result, seed]140    )141 142demo.launch()