Huggingface interface functionsΒΆ

Some scripts download models from Huggingface, so they will need to login first. This library file contains a function to do that. Logging in requires an API key which you can get from the Huggingface web-site when you create an account there. Store that api key in file `~/.huggingface_api` and you can use the `HFLogin()` function in this library to connect.

# Utility functions for interacting with huggingface

import os
from huggingface_hub import login


# Get the Huggingface API key - either file or environment variable
def HFlogin():
    """Login to Huggingface using the API key."""
    if "HF_KEY" in os.environ:
        hf_key = os.getenv("HF_KEY")
    else:
        # If not found in environment variable, look for file
        try:
            with open("%s/.huggingface_api" % os.getenv("HOME"), "r") as file:
                hf_key = file.read().strip()
        except FileNotFoundError:
            raise Exception("Huggingface API key not found.")
    login(token=hf_key)