How to programmatically determine if a given model requires an API token to be invoked?

As in title, how can I check in Python whether a given model requires a token to work with? I’d like to catch and report errors for missing tokens ahead of time in my app, before HF endpoints are actually called. If that’s even possible.

1 Like

It’s a bit of a hassle, but it’s possible. If you look at the contents of the instance obtained using HfApi’s repo_info() or model_info(), you’ll find it written there. However, be careful, as if you don’t explicitly obtain it with expand=[“gated”], it may return None.

Thanks, but what field of ModelInfo has this information then? I went over them from your link but couldn’t pinpoint it.

1 Like

Like this.

from huggingface_hub import HfApi

def is_gated(repo_id: str, repo_type: str="model"):
    try:
        api = HfApi()
        info = api.repo_info(repo_id=repo_id, repo_type=repo_type, expand=["gated"])
        if info.gated is None: return False #
        print(f'{repo_id} ({repo_type}) is {info.gated if info.gated in ["auto", "manual"] else "not"} gated.')
        return True if info.gated in ["auto", "manual"] else False
    except Exception as e:
        print(e)
        return False #

print(is_gated("Qwen/Qwen2.5-VL-7B-Instruct"))
print(is_gated("meta-llama/Llama-3.3-70B-Instruct"))
1 Like

Oh, it was the gated field itself. Thanks a lot!

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.