specimba's picture
Upgrade to FLUX.2-klein-4B (Apache, 4B, sub-second) via Flux2KleinPipeline
9a69386 verified
Raw
History Blame
3.57 kB
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 Flux2KleinPipeline
# ---------------------------------------------------------------------------
# Model: FLUX.2 [klein] 4B
# - Apache-2.0, 4B params, BFL's fastest small model (sub-second, ~13GB VRAM)
# - Unified text-to-image + multi-reference editing
# - Released Jan 2026 (current BFL small-model generation)
# ---------------------------------------------------------------------------
MODEL_REPO_ID = "black-forest-labs/FLUX.2-klein-4B"
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load on cuda at module level. (No enable_model_cpu_offload() on ZeroGPU —
# the GPU is only attached inside @spaces.GPU; module-level cuda uses the
# ZeroGPU CUDA-emulation, and offload would conflict.)
pipe = Flux2KleinPipeline.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,
guidance_scale,
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=guidance_scale,
generator=generator,
).images[0]
return image, seed
examples = [
"A magical city at twilight, glowing windows, storybook illustration, warm light",
"A cat holding a sign that says hello world",
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
]
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.2 [klein] 4B")
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
)
guidance_scale = gr.Slider(
label="Guidance scale", minimum=0.0, maximum=5.0, step=0.1, value=1.0
)
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, guidance_scale],
outputs=[result, seed],
)
if __name__ == "__main__":
demo.launch()