def build_models(mode, resolution, enable_optimization=False):
    """
    Build the models for the given mode, resolution, and configuration.
    """
    # build vae
    from opensora.registry import MODELS, build_module

    if mode == "i2v":
        config = read_config(CONFIG_MAP["v1.3_i2v"])
    else:
        config = read_config(CONFIG_MAP["v1.3"])

    vae = build_module(config.vae, MODELS).cuda()

    # build text encoder
    text_encoder = build_module(config.text_encoder, MODELS)  # T5 must be fp32
    text_encoder.t5.model = text_encoder.t5.model.cuda()

    # Determine model weights based on mode and resolution
    if mode == "i2v":
        weight_path = HF_STDIT_MAP["i2v"]
    else:  # t2v
        weight_path = HF_STDIT_MAP["t2v"].get(resolution, None)
        if not weight_path:
            raise ValueError(f"Unsupported resolution {resolution} for mode {mode}")

    # build stdit
    from opensora.models.stdit.stdit3 import STDiT3

    model_kwargs = {k: v for k, v in config.model.items() if k not in ("type", "from_pretrained", "force_huggingface")}

    print("Load STDIT3 from ", weight_path)
    stdit = STDiT3.from_pretrained(weight_path, **model_kwargs).cuda()

    # build scheduler
    from opensora.registry import SCHEDULERS

    scheduler = build_module(config.scheduler, SCHEDULERS)

    # hack for classifier-free guidance
    text_encoder.y_embedder = stdit.y_embedder

    # move models to device
    vae = vae.to(torch.bfloat16).eval()
    text_encoder.t5.model = text_encoder.t5.model.eval()  # t5 must be in fp32
    stdit = stdit.to(torch.bfloat16).eval()

    # clear cuda
    torch.cuda.empty_cache()
    return vae, text_encoder, stdit, scheduler, config

    Build the models for the given mode, resolution, and configuration.
    
gradio/app.py