CoolFace
Apppublic

rgbguy101/OpenSource101

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py128 linesDownload Raw Back to root
1import gradio as gr2import numpy as np3import random4import spaces5import torch6from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel7from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast8 9dtype = torch.bfloat1610device = "cuda" if torch.cuda.is_available() else "cpu"11 12pipe = DiffusionPipeline.from_pretrained("sayakpaul/FLUX.1-merged", torch_dtype=dtype).to(device)13 14MAX_SEED = np.iinfo(np.int32).max15MAX_IMAGE_SIZE = 204816 17@spaces.GPU()18def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=8, progress=gr.Progress(track_tqdm=True)):19    if randomize_seed:20        seed = random.randint(0, MAX_SEED)21    generator = torch.Generator().manual_seed(seed)22    image = pipe(23        prompt = prompt, 24        width = width,25        height = height,26        num_inference_steps = num_inference_steps, 27        generator = generator,28        guidance_scale=guidance_scale29    ).images[0] 30    return image, seed31 32examples = [33    "a coder learning about AI in his room",34    "a man playing basketball",35    "a disney style statue of liberty",36]37 38css="""39#col-container {40    margin: 0 auto;41    max-width: 520px;42}43"""44 45with gr.Blocks(css=css) as demo:46    47    with gr.Column(elem_id="col-container"):48        gr.Markdown(f"""# [FLUX.1 [merged]](https://huggingface.co/sayakpaul/FLUX.1-merged)49Merge by [Sayak Paul](https://huggingface.co/sayakpaul) of 2 of the 12B param rectified flow transformers [FLUX.1 [dev]](https://huggingface.co/black-forest-labs/FLUX.1-dev) and [FLUX.1 [schnell]](https://huggingface.co/black-forest-labs/FLUX.1-schnell) by [Black Forest Labs](https://blackforestlabs.ai/)50        """)51        52        with gr.Row():53            54            prompt = gr.Text(55                label="Prompt",56                show_label=False,57                max_lines=1,58                placeholder="Enter your prompt",59                container=False,60            )61            run_button = gr.Button("Run", scale=0)62        63        num_inference_steps = gr.Slider(64            label="Number of inference steps",65            minimum=1,66            maximum=50,67            step=1,68            value=8,69        )70        71        result = gr.Image(label="Result", show_label=False)72        73        with gr.Accordion("Advanced Settings", open=False):74            75            seed = gr.Slider(76                label="Seed",77                minimum=0,78                maximum=MAX_SEED,79                step=1,80                value=0,81            )82            83            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)84            85            with gr.Row():86                87                width = gr.Slider(88                    label="Width",89                    minimum=256,90                    maximum=MAX_IMAGE_SIZE,91                    step=32,92                    value=1024,93                )94                95                height = gr.Slider(96                    label="Height",97                    minimum=256,98                    maximum=MAX_IMAGE_SIZE,99                    step=32,100                    value=1024,101                )102            103            with gr.Row():104 105                guidance_scale = gr.Slider(106                    label="Guidance Scale",107                    minimum=1,108                    maximum=15,109                    step=0.1,110                    value=3.5,111                )112        113        gr.Examples(114            examples = examples,115            fn = infer,116            inputs = [prompt],117            outputs = [result, seed],118            cache_examples="lazy"119        )120 121    gr.on(122        triggers=[run_button.click, prompt.submit],123        fn = infer,124        inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],125        outputs = [result, seed]126    )127 128demo.launch()