Spaces:
Running on Zero
Running on Zero
Upgrade to FLUX.2-klein-4B (Apache, 4B, sub-second) via Flux2KleinPipeline (#2)
Browse files- Upgrade to FLUX.2-klein-4B (Apache, 4B, sub-second) via Flux2KleinPipeline (9a693860e8b78284eadf391eeaec4bcc8e51d269)
- FLUX.2-klein needs diffusers from git (Flux2KleinPipeline) + Qwen3 text encoder deps (636d2166310638ff70a796fb5c917f39a689c3d6)
- app.py +18 -11
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -4,19 +4,23 @@ import gradio as gr
|
|
| 4 |
import numpy as np
|
| 5 |
import random
|
| 6 |
import torch
|
| 7 |
-
from diffusers import
|
| 8 |
|
| 9 |
# ---------------------------------------------------------------------------
|
| 10 |
-
# Model
|
|
|
|
|
|
|
|
|
|
| 11 |
# ---------------------------------------------------------------------------
|
| 12 |
-
|
| 13 |
-
MODEL_REPO_ID = "black-forest-labs/FLUX.1-schnell"
|
| 14 |
|
| 15 |
dtype = torch.bfloat16
|
| 16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
|
| 18 |
-
# Load at module level
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
MAX_IMAGE_SIZE = 2048
|
|
@@ -30,6 +34,7 @@ def infer(
|
|
| 30 |
width,
|
| 31 |
height,
|
| 32 |
num_inference_steps,
|
|
|
|
| 33 |
progress=gr.Progress(track_tqdm=True),
|
| 34 |
):
|
| 35 |
if randomize_seed:
|
|
@@ -40,8 +45,7 @@ def infer(
|
|
| 40 |
width=width,
|
| 41 |
height=height,
|
| 42 |
num_inference_steps=num_inference_steps,
|
| 43 |
-
guidance_scale=
|
| 44 |
-
max_sequence_length=256,
|
| 45 |
generator=generator,
|
| 46 |
).images[0]
|
| 47 |
return image, seed
|
|
@@ -49,8 +53,8 @@ def infer(
|
|
| 49 |
|
| 50 |
examples = [
|
| 51 |
"A magical city at twilight, glowing windows, storybook illustration, warm light",
|
|
|
|
| 52 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 53 |
-
"A delicious ceviche cheesecake slice",
|
| 54 |
]
|
| 55 |
|
| 56 |
css = """
|
|
@@ -59,7 +63,7 @@ css = """
|
|
| 59 |
|
| 60 |
with gr.Blocks(css=css) as demo:
|
| 61 |
with gr.Column(elem_id="col-container"):
|
| 62 |
-
gr.Markdown("# 🖼️ NEXUS Visual Weaver — FLUX.
|
| 63 |
with gr.Row():
|
| 64 |
prompt = gr.Text(
|
| 65 |
label="Prompt",
|
|
@@ -82,13 +86,16 @@ with gr.Blocks(css=css) as demo:
|
|
| 82 |
num_inference_steps = gr.Slider(
|
| 83 |
label="Inference steps", minimum=1, maximum=8, step=1, value=4
|
| 84 |
)
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 87 |
|
| 88 |
gr.on(
|
| 89 |
triggers=[run_button.click, prompt.submit],
|
| 90 |
fn=infer,
|
| 91 |
-
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 92 |
outputs=[result, seed],
|
| 93 |
)
|
| 94 |
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import random
|
| 6 |
import torch
|
| 7 |
+
from diffusers import Flux2KleinPipeline
|
| 8 |
|
| 9 |
# ---------------------------------------------------------------------------
|
| 10 |
+
# Model: FLUX.2 [klein] 4B
|
| 11 |
+
# - Apache-2.0, 4B params, BFL's fastest small model (sub-second, ~13GB VRAM)
|
| 12 |
+
# - Unified text-to-image + multi-reference editing
|
| 13 |
+
# - Released Jan 2026 (current BFL small-model generation)
|
| 14 |
# ---------------------------------------------------------------------------
|
| 15 |
+
MODEL_REPO_ID = "black-forest-labs/FLUX.2-klein-4B"
|
|
|
|
| 16 |
|
| 17 |
dtype = torch.bfloat16
|
| 18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
|
| 20 |
+
# Load on cuda at module level. (No enable_model_cpu_offload() on ZeroGPU —
|
| 21 |
+
# the GPU is only attached inside @spaces.GPU; module-level cuda uses the
|
| 22 |
+
# ZeroGPU CUDA-emulation, and offload would conflict.)
|
| 23 |
+
pipe = Flux2KleinPipeline.from_pretrained(MODEL_REPO_ID, torch_dtype=dtype).to(device)
|
| 24 |
|
| 25 |
MAX_SEED = np.iinfo(np.int32).max
|
| 26 |
MAX_IMAGE_SIZE = 2048
|
|
|
|
| 34 |
width,
|
| 35 |
height,
|
| 36 |
num_inference_steps,
|
| 37 |
+
guidance_scale,
|
| 38 |
progress=gr.Progress(track_tqdm=True),
|
| 39 |
):
|
| 40 |
if randomize_seed:
|
|
|
|
| 45 |
width=width,
|
| 46 |
height=height,
|
| 47 |
num_inference_steps=num_inference_steps,
|
| 48 |
+
guidance_scale=guidance_scale,
|
|
|
|
| 49 |
generator=generator,
|
| 50 |
).images[0]
|
| 51 |
return image, seed
|
|
|
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
"A magical city at twilight, glowing windows, storybook illustration, warm light",
|
| 56 |
+
"A cat holding a sign that says hello world",
|
| 57 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
|
|
| 58 |
]
|
| 59 |
|
| 60 |
css = """
|
|
|
|
| 63 |
|
| 64 |
with gr.Blocks(css=css) as demo:
|
| 65 |
with gr.Column(elem_id="col-container"):
|
| 66 |
+
gr.Markdown("# 🖼️ NEXUS Visual Weaver — FLUX.2 [klein] 4B")
|
| 67 |
with gr.Row():
|
| 68 |
prompt = gr.Text(
|
| 69 |
label="Prompt",
|
|
|
|
| 86 |
num_inference_steps = gr.Slider(
|
| 87 |
label="Inference steps", minimum=1, maximum=8, step=1, value=4
|
| 88 |
)
|
| 89 |
+
guidance_scale = gr.Slider(
|
| 90 |
+
label="Guidance scale", minimum=0.0, maximum=5.0, step=0.1, value=1.0
|
| 91 |
+
)
|
| 92 |
|
| 93 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 94 |
|
| 95 |
gr.on(
|
| 96 |
triggers=[run_button.click, prompt.submit],
|
| 97 |
fn=infer,
|
| 98 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
|
| 99 |
outputs=[result, seed],
|
| 100 |
)
|
| 101 |
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
spaces
|
| 2 |
accelerate
|
| 3 |
-
diffusers
|
| 4 |
transformers
|
| 5 |
sentencepiece
|
| 6 |
torch
|
|
|
|
| 1 |
spaces
|
| 2 |
accelerate
|
| 3 |
+
git+https://github.com/huggingface/diffusers.git
|
| 4 |
transformers
|
| 5 |
sentencepiece
|
| 6 |
torch
|