File size: 1,493 Bytes
0d33de8
 
 
 
e7d080f
0d33de8
 
 
 
 
 
 
 
 
 
 
 
 
0dac0d1
0d33de8
e7d080f
 
 
 
0d33de8
 
05bce11
 
 
 
 
 
0d33de8
 
 
b1fa113
05bce11
0d33de8
 
b1fa113
0dac0d1
0d33de8
 
 
05bce11
0d33de8
05bce11
0d33de8
 
 
0dac0d1
 
0d33de8
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
import spaces
import torch

from model import iter_trajectory_frames, load_model

MODEL_READY = False


def ensure_model_loaded():
    global MODEL_READY
    if not MODEL_READY:
        load_model()
        MODEL_READY = True


@spaces.GPU
@torch.inference_mode()
def predict(label: int, steps: int):
    ensure_model_loaded()
    for idx, (image, step_idx, total_steps, total) in enumerate(
        iter_trajectory_frames(label=label, steps=steps), start=1
    ):
        yield image, f"trajectory checkpoint {idx}/{total} | integration step {step_idx}/{total_steps}"


theme = gr.themes.Default(
    primary_hue="orange",
)


with gr.Blocks(title="MNIST Flow", theme=theme) as demo:
    gr.Markdown("# MNIST Flow")
    gr.Markdown(
        "Flow-matching DiT model for MNIST digits. "
        "The demo streams one sample from noise to digit with fixed CFG=2.0. "
        "Sampling caps ODE steps at 128 and shows 32 trajectory checkpoints."
    )

    grid = gr.Image(label="Trajectory", show_label=True)
    status = gr.Textbox(label="Status")

    with gr.Row():
        label = gr.Dropdown([str(i) for i in range(10)], value="1", label="Label")
        steps = gr.Slider(32, 128, value=128, step=1, label="Steps")

    generate_btn = gr.Button("Generate", variant="primary")

    generate_btn.click(
        fn=predict,
        inputs=[label, steps],
        outputs=[grid, status],
        scroll_to_output=True,
    )


if __name__ == "__main__":
    demo.launch()