def model_from_hfconfig()

in docker_images/k2/app/common.py [0:0]


def model_from_hfconfig(hf_repo, hf_config):
    nn_model_filename = hf_hub_download(hf_repo, hf_config["nn_model_filename"])
    token_filename = (
        hf_hub_download(hf_repo, hf_config["token_filename"])
        if "token_filename" in hf_config
        else None
    )
    bpe_model_filename = (
        hf_hub_download(hf_repo, hf_config["bpe_model_filename"])
        if "bpe_model_filename" in hf_config
        else None
    )
    decoding_method = hf_config.get("decoding_method", "greedy_search")
    sample_rate = hf_config.get("sample_rate", 16000)
    num_active_paths = hf_config.get("num_active_paths", 4)

    assert decoding_method in ("greedy_search", "modified_beam_search"), decoding_method

    if decoding_method == "modified_beam_search":
        assert num_active_paths >= 1, num_active_paths

    assert bpe_model_filename is not None or token_filename is not None
    if bpe_model_filename:
        assert token_filename is None

    if token_filename:
        assert bpe_model_filename is None

    return OfflineAsr(
        nn_model_filename,
        bpe_model_filename,
        token_filename,
        decoding_method,
        num_active_paths,
        sample_rate,
    )