import spaces # MUST be imported before any CUDA-touching package (torch/diffusers) import gradio as gr import numpy as np import random import torch from diffusers import DiffusionPipeline # --------------------------------------------------------------------------- # Model # --------------------------------------------------------------------------- # FLUX.1-schnell: Apache-2.0, 1-4 step distilled text-to-image. Fast on ZeroGPU. MODEL_REPO_ID = "black-forest-labs/FLUX.1-schnell" dtype = torch.bfloat16 device = "cuda" if torch.cuda.is_available() else "cpu" # Load at module level on cuda (ZeroGPU CUDA-emulation makes this safe outside @spaces.GPU) pipe = DiffusionPipeline.from_pretrained(MODEL_REPO_ID, torch_dtype=dtype).to(device) MAX_SEED = np.iinfo(np.int32).max MAX_IMAGE_SIZE = 2048 @spaces.GPU(duration=60) def infer( prompt, seed, randomize_seed, width, height, num_inference_steps, progress=gr.Progress(track_tqdm=True), ): if randomize_seed: seed = random.randint(0, MAX_SEED) generator = torch.Generator(device=device).manual_seed(seed) image = pipe( prompt=prompt, width=width, height=height, num_inference_steps=num_inference_steps, guidance_scale=0.0, # schnell is guidance-distilled max_sequence_length=256, generator=generator, ).images[0] return image, seed examples = [ "A magical city at twilight, glowing windows, storybook illustration, warm light", "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", "A delicious ceviche cheesecake slice", ] css = """ #col-container { margin: 0 auto; max-width: 640px; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.Markdown("# 🖼️ NEXUS Visual Weaver — FLUX.1-schnell") with gr.Row(): prompt = gr.Text( label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False, ) run_button = gr.Button("Run", scale=0, variant="primary") result = gr.Image(label="Result", show_label=False) with gr.Accordion("Advanced Settings", open=False): seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0) randomize_seed = gr.Checkbox(label="Randomize seed", value=True) with gr.Row(): width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) with gr.Row(): num_inference_steps = gr.Slider( label="Inference steps", minimum=1, maximum=8, step=1, value=4 ) gr.Examples(examples=examples, inputs=[prompt]) gr.on( triggers=[run_button.click, prompt.submit], fn=infer, inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps], outputs=[result, seed], ) if __name__ == "__main__": demo.launch()