import gradio as gr import numpy as np import random import torch from diffusers import DiffusionPipeline from accelerate import init_empty_weights, load_checkpoint_and_dispatch # Configuración para usar bfloat16 y CUDA si está disponible dtype = torch.bfloat16 device = "cuda" if torch.cuda.is_available() else "cpu" # Inicializar el modelo solo una vez y cargarlo en RAM y GPU pipe = None def load_model(): global pipe if pipe is None: with init_empty_weights(): pipe = DiffusionPipeline.from_pretrained( "black-forest-labs/FLUX.1-schnell", torch_dtype=dtype ) # Despachar los pesos a la GPU (sin "named_parameters") pipe = load_checkpoint_and_dispatch( pipe, "black-forest-labs/FLUX.1-schnell", device_map="auto", # Automatiza el uso de RAM y GPU offload_folder=None, # Evita que se almacenen los pesos temporalmente en el disco ).to(device) MAX_SEED = np.iinfo(np.int32).max MAX_IMAGE_SIZE = 2048 @spaces.GPU() def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, num_images=1, progress=gr.Progress(track_tqdm=True)): load_model() # Asegurarse de que el modelo esté cargado antes de la inferencia if randomize_seed: seed = random.randint(0, MAX_SEED) generator = torch.Generator().manual_seed(seed) images = [] for _ in range(num_images): image = pipe( prompt=prompt, width=width, height=height, num_inference_steps=num_inference_steps, generator=generator, guidance_scale=0.0 ).images[0] images.append(image) return images, seed examples = [ "a tiny astronaut hatching from an egg on the moon", "a cat holding a sign that says hello world", "an anime illustration of a wiener schnitzel", ] css = """ #col-container { margin: 0 auto; max-width: 520px; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.Markdown(f"""# FLUX.1 [schnell] 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/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) results = gr.Gallery(label="Results", show_label=False, elem_id="image-gallery") 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="Number of inference steps", minimum=1, maximum=50, step=1, value=4, ) num_images = gr.Slider( label="Number of images", minimum=1, maximum=300, step=1, value=1, ) gr.Examples( examples = examples, fn = infer, inputs = [prompt], outputs = [results, seed], cache_examples="lazy" ) gr.on( triggers=[run_button.click, prompt.submit], fn = infer, inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps, num_images], outputs = [results, seed] ) # Crear un enlace público con share=True demo.launch(share=True)