### Example Model Info Output Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt This is an example of the output you should expect when checking the installation. ```text Model Name: gpt2 Tags: ['pytorch', 'tf', 'jax', 'tflite', 'rust', 'safetensors', 'gpt2', 'text-generation', 'en', 'doi:10.57967/hf/0039', 'transformers', 'exbert', 'license:mit', 'has_space'] Task: text-generation ``` -------------------------------- ### Check huggingface_hub Installation Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Verify the installation by running this Python command, which fetches information about the 'gpt2' model from the Hugging Face Hub. ```bash python -c "from huggingface_hub import model_info; print(model_info('gpt2'))" ``` -------------------------------- ### Install Hugging Face Hub with Inference Extras Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Install the `huggingface_hub` library with the `[inference]` extra to enable asynchronous client functionalities. Alternatively, install `aiohttp` separately. ```sh pip install --upgrade huggingface_hub[inference] # or # pip install aiohttp ``` -------------------------------- ### Check Hugging Face CLI Installation Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Verify the CLI installation by displaying all available options and commands. Use the --help option for detailed command information. ```bash huggingface-cli --help ``` -------------------------------- ### Install Hugging Face Hub from Source Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Installs the huggingface_hub library directly from its GitHub repository. This is useful for accessing the latest development version, which may include recent bug fixes or features not yet in a stable release. ```bash pip install git+https://github.com/huggingface/huggingface_hub ``` -------------------------------- ### Editable Install Hugging Face Hub Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Sets up an editable installation of huggingface_hub after cloning the repository locally. This is ideal for developers who plan to contribute to the library, as changes made to the local code will be immediately reflected without needing reinstallation. ```bash # First, clone repo locally git clone https://github.com/huggingface/huggingface_hub.git # Then, install with -e flag cd huggingface_hub pip install -e . ``` -------------------------------- ### Install Hugging Face CLI using Pkgx Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Install the Hugging Face CLI using the Pkgx package manager for cross-platform compatibility. ```bash pkgx install huggingface-cli ``` -------------------------------- ### Install huggingface_hub with Conda Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Use this command to install the huggingface_hub library from the conda-forge channel. ```bash conda install -c conda-forge huggingface_hub ``` -------------------------------- ### Install Hugging Face CLI using Homebrew Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Install the Hugging Face CLI using the Homebrew package manager on macOS and Linux. ```bash brew install huggingface-cli ``` -------------------------------- ### Install Hugging Face CLI Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Install the Hugging Face CLI with extra dependencies for better user experience, especially for cache management commands. ```bash pip install -U "huggingface_hub[cli]" ``` -------------------------------- ### Get Help for huggingface-cli download Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt View all available options for the `huggingface-cli download` command by running it with the `--help` flag. ```bash huggingface-cli download --help ``` -------------------------------- ### Install huggingface_hub CLI dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Before using the `delete-cache` command with the TUI, install the necessary extra dependencies for the CLI. ```bash pip install huggingface_hub["cli"] ``` -------------------------------- ### Get Help for huggingface-cli upload Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Run this command to view all available options and usage details for the huggingface-cli upload command. ```bash >>> huggingface-cli upload --help ``` -------------------------------- ### Instantiate a Local Repository Object Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Create a Repository object pointing to a local directory. Ensure Git-LFS is installed. ```python from huggingface_hub import Repository repo = Repository(local_dir="//") ``` -------------------------------- ### Install Hugging Face Hub with TensorFlow Dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Installs the huggingface_hub library along with optional dependencies required for TensorFlow-specific features. Note that this does not install TensorFlow itself. ```bash pip install 'huggingface_hub[tensorflow]' ``` -------------------------------- ### Install Hugging Face Hub with CLI and Torch Dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Installs the huggingface_hub library with optional dependencies for both the command-line interface (CLI) and PyTorch (torch) features. This enables enhanced CLI functionality and framework-specific operations. ```bash pip install 'huggingface_hub[cli,torch]' ``` -------------------------------- ### Install Hugging Face Hub with Pip Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Installs or upgrades the huggingface_hub library from the Python Package Index (PyPI). This command ensures you have the latest stable version. ```bash pip install --upgrade huggingface_hub ``` -------------------------------- ### Install Hugging Face Hub from Specific Branch Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Installs the huggingface_hub library from a specific branch on GitHub. This allows testing new features or bug fixes before they are merged into the main branch or released. ```bash pip install git+https://github.com/huggingface/huggingface_hub@my-feature-branch ``` -------------------------------- ### Run Hugging Face CLI using Pkgx Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Execute Hugging Face CLI commands directly using Pkgx without a formal installation. ```bash pkgx huggingface-cli --help ``` -------------------------------- ### Display Hugging Face Environment Information Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Use `huggingface-cli env` to print details about your machine setup. This is helpful for debugging and providing information when opening GitHub issues. ```bash >>> huggingface-cli env Copy-and-paste the text below in your GitHub issue. - huggingface_hub version: 0.19.0.dev0 - Platform: Linux-6.2.0-36-generic-x86_64-with-glibc2.35 - Python version: 3.10.12 - Running in iPython ?: No - Running in notebook ?: No - Running in Google Colab ?: No - Token path ?: /home/wauplin/.cache/huggingface/token - Has saved token ?: True - Who am I ?: Wauplin - Configured git credential helpers: store - FastAI: N/A - Tensorflow: 2.11.0 - Torch: 1.12.1 - Jinja2: 3.1.2 - Graphviz: 0.20.1 - Pydot: 1.4.2 - Pillow: 9.2.0 - hf_transfer: 0.1.3 - gradio: 4.0.2 - tensorboard: 2.6 - numpy: 1.23.2 - pydantic: 2.4.2 - aiohttp: 3.8.4 - ENDPOINT: https://huggingface.co - HF_HUB_CACHE: /home/wauplin/.cache/huggingface/hub - HF_ASSETS_CACHE: /home/wauplin/.cache/huggingface/assets - HF_TOKEN_PATH: /home/wauplin/.cache/huggingface/token - HF_HUB_OFFLINE: False - HF_HUB_DISABLE_TELEMETRY: False - HF_HUB_DISABLE_PROGRESS_BARS: None - HF_HUB_DISABLE_SYMLINKS_WARNING: False - HF_HUB_DISABLE_EXPERIMENTAL_WARNING: False - HF_HUB_DISABLE_IMPLICIT_TOKEN: False - HF_HUB_ENABLE_HF_TRANSFER: False - HF_HUB_ETAG_TIMEOUT: 10 - HF_HUB_DOWNLOAD_TIMEOUT: 10 ``` -------------------------------- ### Download Model with `hf_hub_download` Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Example of a `from_pretrained` method that downloads a model file using `hf_hub_download`. It specifies the repository ID, filename, library name, and version for the download. ```python def from_pretrained(model_id: str) -> MyModelClass: # Download model from Hub cached_model = hf_hub_download( repo_id=repo_id, filename="model.pkl", library_name="fastai", library_version=get_fastai_version(), ) # Load model return load_model(cached_model) ``` -------------------------------- ### Load and Save PyTorch Model with Hugging Face Hub Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Demonstrates how to define, save, push, and load a PyTorch model using PyTorchModelHubMixin. This example shows the complete workflow from model creation to loading from the Hub. ```python >>> import torch >>> import torch.nn as nn >>> from huggingface_hub import PyTorchModelHubMixin # Define your Pytorch model exactly the same way you are used to >>> class MyModel( ... nn.Module, ... PyTorchModelHubMixin, # multiple inheritance ... library_name="keras-nlp", ... tags=["keras"], ... repo_url="https://github.com/keras-team/keras-nlp", ... docs_url="https://keras.io/keras_nlp/", ... # ^ optional metadata to generate model card ... ): ... def __init__(self, hidden_size: int = 512, vocab_size: int = 30000, output_size: int = 4): ... super().__init__() ... self.param = nn.Parameter(torch.rand(hidden_size, vocab_size)) ... self.linear = nn.Linear(output_size, vocab_size) ... def forward(self, x): ... return self.linear(x + self.param) # 1. Create model >>> model = MyModel(hidden_size=128) # Config is automatically created based on input + default values >>> model.param.shape[0] 128 # 2. (optional) Save model to local directory >>> model.save_pretrained("path/to/my-awesome-model") # 3. Push model weights to the Hub >>> model.push_to_hub("my-awesome-model") # 4. Initialize model from the Hub => config has been preserved >>> model = MyModel.from_pretrained("username/my-awesome-model") >>> model.param.shape[0] 128 # Model card has been correctly populated >>> from huggingface_hub import ModelCard >>> card = ModelCard.load("username/my-awesome-model") >>> card.data.tags ["keras", "pytorch_model_hub_mixin", "model_hub_mixin"] >>> card.data.library_name "keras-nlp" ``` -------------------------------- ### Example cache deletion configuration file Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Shows the structure and content of the temporary file generated when using `huggingface-cli delete-cache --disable-tui`. Users comment/uncomment revisions to select them for deletion. ```txt # INSTRUCTIONS # ------------ # This is a temporary file created by running `huggingface-cli delete-cache` with the # `--disable-tui` option. It contains a set of revisions that can be deleted from your # local cache directory. # # Please manually review the revisions you want to delete: # - Revision hashes can be commented out with '#'. # - Only non-commented revisions in this file will be deleted. # - Revision hashes that are removed from this file are ignored as well. # - If `CANCEL_DELETION` line is uncommented, the all cache deletion is cancelled and # no changes will be applied. # # Once you've manually reviewed this file, please confirm deletion in the terminal. This # file will be automatically removed once done. # ------------ # KILL SWITCH # ------------ # Un-comment following line to completely cancel the deletion process # CANCEL_DELETION # ------------ # REVISIONS # ------------ # Dataset chrisjay/crowd-speech-africa (761.7M, used 5 days ago) ebedcd8c55c90d39fd27126d29d8484566cd27ca # Refs: main # modified 5 days ago # Dataset oscar (3.3M, used 4 days ago) # 916f956518279c5e60c63902ebdf3ddf9fa9d629 # Refs: main # modified 4 days ago # Dataset wikiann (804.1K, used 2 weeks ago) 89d089624b6323d69dcd9e5eb2def0551887a73a # Refs: main # modified 2 weeks ago # Dataset z-uo/male-LJSpeech-italian (5.5G, used 5 days ago) # 9cfa5647b32c0a30d0adfca06bf198d82192a0d1 # Refs: main # modified 5 days ago ``` -------------------------------- ### List all models on the Hub Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Initialize the `HfApi` client and use `list_models()` to get an iterator over all models. This is a basic way to retrieve repository information. ```python >>> from huggingface_hub import HfApi >>> api = HfApi() >>> models = api.list_models() ``` -------------------------------- ### Create Space with Secrets and Variables Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Sets secrets and variables during the creation of a new Space. This allows for immediate configuration upon Space setup. ```python >>> api.create_repo( ... repo_id=repo_id, ... repo_type="space", ... space_sdk="gradio", ... space_secrets=[{"key"="HF_TOKEN", "value"="hf_api_***"}, ...], ... space_variables=[{"key"="MODEL_REPO_ID", "value"="user/repo"}, ...], ... ) ``` -------------------------------- ### Preupload LFS files and create a single commit Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt This example demonstrates how to pre-upload multiple LFS files incrementally using `preupload_lfs_files` and then create a single commit with all uploaded files using `create_commit`. Ensure that `CommitOperationAdd` objects are stored separately if their content is needed after the `preupload_lfs_files` call, as the content is removed from the object after upload. ```python >>> from huggingface_hub import CommitOperationAdd, preupload_lfs_files, create_commit, create_repo >>> repo_id = create_repo("test_preupload").repo_id >>> operations = [] # List of all `CommitOperationAdd` objects that will be generated >>> for i in range(5): ... content = ... # generate binary content ... addition = CommitOperationAdd(path_in_repo=f"shard_{i}_of_5.bin", path_or_fileobj=content) ... preupload_lfs_files(repo_id, additions=[addition]) ... operations.append(addition) >>> # Create commit >>> create_commit(repo_id, operations=operations, commit_message="Commit all shards") ``` -------------------------------- ### List files in a directory Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Use fs.ls to list all files within a specified directory on the Hub. Set detail=False to get a list of file paths. ```python fs.ls("datasets/my-username/my-dataset-repo/data", detail=False) ``` -------------------------------- ### List top downloaded datasets Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieve a list of the top 5 most downloaded datasets from the Hub. This example demonstrates sorting by downloads in descending order and limiting the results. ```python >>> list(list_datasets(sort="downloads", direction=-1, limit=5)) [DatasetInfo( id='argilla/databricks-dolly-15k-curated-en', author='argilla', sha='4dcd1dedbe148307a833c931b21ca456a1fc4281', last_modified=datetime.datetime(2023, 10, 2, 12, 32, 53, tzinfo=datetime.timezone.utc), private=False, downloads=8889377, (...) ] ``` -------------------------------- ### Save and Upload a PyTorch Model Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Example of using the `commit` context manager to save a PyTorch model's state dictionary and upload it to a repository. ```python import torch model = torch.nn.Transformer() with Repository("torch-model", clone_from="/torch-model", token=True).commit(commit_message="My cool model :)"): torch.save(model.state_dict(), "model.pt") ``` -------------------------------- ### Example TUI cache deletion interaction Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Illustrates a typical interaction and output when using the `huggingface-cli delete-cache` command with the TUI, showing selected revisions and confirmation. ```txt ✗ huggingface-cli delete-cache --dir ~/.cache/huggingface/hub ? Select revisions to delete: 2 revision(s) selected. ? 2 revisions selected counting for 3.1G. Confirm deletion ? Yes Start deletion. Done. Deleted 1 repo(s) and 0 revision(s) for a total of 3.1G. ``` -------------------------------- ### Activate Virtual Environment (Linux/macOS) Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Activates the previously created virtual environment on Linux and macOS systems. This ensures that subsequent package installations are isolated to this environment. ```bash source .env/bin/activate ``` -------------------------------- ### Download Files with Include/Exclude Patterns using huggingface-cli Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Download files matching specific patterns using `--include` and `--exclude` flags. This example downloads safetensors files, excluding FP16 variants. ```bash >>> huggingface-cli download stabilityai/stable-diffusion-xl-base-1.0 --include "*.safetensors" --exclude "*.fp16.*"* Fetching 8 files: 0%| | 0/8 [00:00>> from huggingface_hub import AsyncInferenceClient >>> client = AsyncInferenceClient() >>> image = await client.text_to_image("An astronaut riding a horse on the moon.") >>> image.save("astronaut.png") ``` -------------------------------- ### Read and Write Pandas DataFrame to Hub Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Demonstrates reading a CSV file into a Pandas DataFrame from a Hub repository and writing a DataFrame back to a Hub repository. Ensure the 'pandas' library is installed. ```python import pandas as pd # Read a remote CSV file into a dataframe df = pd.read_csv("hf://datasets/my-username/my-dataset-repo/train.csv") # Write a dataframe to a remote CSV file df.to_csv("hf://datasets/my-username/my-dataset-repo/test.csv") ``` -------------------------------- ### Custom Image Request with InferenceClient.post() Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Use `InferenceClient.post()` to send custom requests to the Inference API. This example retrieves raw image bytes instead of a PIL Image, useful when Pillow is not installed or for binary content. ```python >>> from huggingface_hub import InferenceClient >>> client = InferenceClient() >>> response = client.post(json={"inputs": "An astronaut riding a horse on the moon."}, model="stabilityai/stable-diffusion-2-1") >>> response.content # raw bytes b'...' ``` -------------------------------- ### Initialize HfFileSystem Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Instantiate the HfFileSystem to begin interacting with the Hugging Face Hub. ```python from huggingface_hub import HfFileSystem fs = HfFileSystem() ``` -------------------------------- ### Initialize Repository from Local Directory Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Initialize a `Repository` object from an existing local directory that is already a clone of a Hub repository. ```python from huggingface_hub import Repository repo = Repository(local_dir="path/to/local/repo") ``` -------------------------------- ### Create and Clone a Repository Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Combine `create_repo` with `Repository` to first create a new repository on the Hub and then clone it locally. ```python repo_url = create_repo(repo_id="repo_name") repo = Repository(local_dir="repo_local_path", clone_from=repo_url) ``` -------------------------------- ### Using HfApi Client and Root Methods Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Demonstrates how to list models using both the direct root method and a configured HfApi client instance. The HfApi client allows for custom endpoints, tokens, and user-agents. ```python from huggingface_hub import HfApi, list_models # Use root method models = list_models() # Or configure a HfApi client hf_api = HfApi( endpoint="https://huggingface.co", # Can be a Private Hub endpoint. token="hf_xxx", # Token is not persisted on the machine. ) models = hf_api.list_models() ``` -------------------------------- ### Create a Public Repository Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Use the `create_repo` function to create a new public repository on the Hugging Face Hub. Ensure you are logged in. ```python from huggingface_hub import HfApi api = HfApi() api.create_repo(repo_id="super-cool-model") ``` -------------------------------- ### Create a Webhook Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Use `create_webhook` to set up a new webhook. Specify the URL, watched events, and optionally domains and a secret for security. ```python from huggingface_hub import create_webhook # Example: Creating a webhook webhook = create_webhook( url="https://webhook.site/your-custom-url", watched=[{"type": "user", "name": "your-username"}, {"type": "org", "name": "your-org-name"}], domains=["repo", "discussion"], secret="your-secret" ) ``` -------------------------------- ### Create Space with Hardware and Storage Configuration Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Configures hardware, storage, and sleep time during Space creation. This allows for immediate optimization of the Space's resources. ```python >>> api.create_repo( ... repo_id=repo_id, ... repo_type="space", ... space_sdk="gradio" ... space_hardware="cpu-upgrade", ... space_storage="small", ... space_sleep_time="7200", # 2 hours in secs ... ) ``` -------------------------------- ### Get Space Runtime Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieves the current runtime status and hardware information for a Space. ```APIDOC ## GET /api/spaces/{repo_id}/runtime ### Description Retrieves the runtime status and hardware details of a Space repository. ### Method GET ### Endpoint `/api/spaces/{repo_id}/runtime` ### Parameters #### Path Parameters - **repo_id** (string) - Required - The identifier of the Space repository (e.g., "user/space_name"). ### Response #### Success Response (200) - **stage** (string) - The current stage of the Space runtime (e.g., "RUNNING_BUILDING", "RUNNING", "SLEEPING"). - **hardware** (string) - The currently active hardware configuration. - **requested_hardware** (string) - The hardware that was last requested. #### Response Example ```json { "stage": "RUNNING", "hardware": "cpu-basic", "requested_hardware": "t4-medium" } ``` ``` -------------------------------- ### Hugging Face Space App Skeleton for Dynamic Hardware Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt This Python code demonstrates the skeleton of a Hugging Face Space application that checks for scheduled tasks on startup. If a task exists, it requests specific hardware for finetuning; otherwise, it launches a Gradio interface for new task requests. It also handles the process of finetuning and resetting hardware upon task completion. ```python # Space will need your token to request hardware: set it as a Secret ! HF_TOKEN = os.environ.get("HF_TOKEN") # Space own repo_id TRAINING_SPACE_ID = "Wauplin/dreambooth-training" from huggingface_hub import HfApi, SpaceHardware api = HfApi(token=HF_TOKEN) # On Space startup, check if a task is scheduled. If yes, finetune the model. If not, # display an interface to request a new task. task = get_task() if task is None: # Start Gradio app def gradio_fn(task): # On user request, add task and request hardware add_task(task) api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM) gr.Interface(fn=gradio_fn, ...).launch() else: runtime = api.get_space_runtime(repo_id=TRAINING_SPACE_ID) # Check if Space is loaded with a GPU. if runtime.hardware == SpaceHardware.T4_MEDIUM: # If yes, finetune base model on dataset ! train_and_upload(task) # Then, mark the task as "DONE" mark_as_done(task) # DO NOT FORGET: set back CPU hardware api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.CPU_BASIC) else: api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM) ``` -------------------------------- ### Update Inference Endpoint Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Updates an existing Inference Endpoint, for example, to change its configuration or scale. ```APIDOC ## PUT /inference-endpoints/{endpoint_id} ### Description Updates an existing Inference Endpoint. ### Method PUT ### Endpoint /inference-endpoints/{endpoint_id} ### Parameters #### Path Parameters - **endpoint_id** (string) - Required - The unique identifier of the Inference Endpoint to update. #### Request Body - **instance_count** (integer) - Optional - The desired number of replicas for the endpoint. - **instance_type** (string) - Optional - The desired instance type for the endpoint. ### Request Example { "instance_count": 3 } ### Response #### Success Response (200) - **id** (string) - The unique identifier of the updated Inference Endpoint. - **status** (string) - The updated status of the endpoint. #### Response Example { "id": "my-endpoint-id", "status": "UPDATING" } ``` -------------------------------- ### Get All Discussions as a List Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Converts the generator returned by get_repo_discussions into a Python list for easier manipulation. ```python from huggingface_hub import get_repo_discussions discussions_list = list(get_repo_discussions(repo_id="bert-base-uncased")) ``` -------------------------------- ### Retrieve All Discussions for a Repository Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Fetches all discussions and pull requests for a specified repository. Useful for getting an overview of all conversations. ```python from huggingface_hub import get_repo_discussions for discussion in get_repo_discussions(repo_id="bigscience/bloom"): print(f"{discussion.num} - {discussion.title}, pr: {discussion.is_pull_request}") ``` -------------------------------- ### Create a Space with Hardware and Sleep Time Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt When creating a new Space, you can specify the desired hardware and sleep time directly in the `create_repo` call. ```python >>> api.create_repo( ... repo_id=repo_id, ... repo_type="space", ... space_sdk="gradio" ... space_hardware="t4-medium", ... space_sleep_time="3600", ... ) ``` -------------------------------- ### List Inference Endpoints Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieves a list of all Inference Endpoints associated with the authenticated user. This is useful for getting an overview of deployed endpoints. ```APIDOC ## GET /inference-endpoints ### Description Retrieves a list of all Inference Endpoints associated with the authenticated user. ### Method GET ### Endpoint /inference-endpoints ### Response #### Success Response (200) - **List of Inference Endpoint objects** (array) - Each object contains details about an Inference Endpoint. #### Response Example [ { "id": "endpoint-1", "model_id": "bert-base-uncased", "status": "RUNNING", "region": "us-west-2" }, { "id": "endpoint-2", "model_id": "distilbert-base-uncased", "status": "PENDING", "region": "eu-central-1" } ] ``` -------------------------------- ### Upload Model with `HfApi` and `upload_folder` Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Illustrates a `push_to_hub` method that creates a repository, saves model weights and a README in a temporary directory, and then uploads the folder using `api.upload_folder`. This approach handles repo creation and file uploads in a single commit. ```python def push_to_hub(model: MyModelClass, repo_name: str) -> None: api = HfApi() # Create repo if not existing yet and get the associated repo_id repo_id = api.create_repo(repo_name, exist_ok=True) # Save all files in a temporary directory and push them in a single commit with TemporaryDirectory() as tmpdir: tmpdir = Path(tmpdir) # Save weights save_model(model, tmpdir / "model.safetensors") # Generate model card card = generate_model_card(model) (tmpdir / "README.md").write_text(card) # Save logs # Save figures # Save evaluation metrics # ... # Push to hub return api.upload_folder(repo_id=repo_id, folder_path=tmpdir) ``` -------------------------------- ### List Collections by Owner Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieve all collections owned by a specific user. This is a basic usage of `list_collections()` to get an iterable of `Collection` objects. ```python from huggingface_hub import list_collections collections = list_collections(owner="teknium") ``` -------------------------------- ### Duplicate an Existing Hugging Face Space Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Duplicates an existing Space repository. This is useful for starting from a template or controlling configurations of public Spaces. ```python >>> api.duplicate_space("multimodalart/dreambooth-training") ``` -------------------------------- ### Import Logging Utility Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Import the logging utility from the huggingface_hub package. ```python from huggingface_hub import logging ``` -------------------------------- ### Download a Dataset with huggingface-cli Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Download an entire dataset from the Hub by specifying the dataset's repo ID and using the `--repo-type dataset` flag. ```bash # https://huggingface.co/datasets/HuggingFaceH4/ultrachat_200k >>> huggingface-cli download HuggingFaceH4/ultrachat_200k --repo-type dataset ``` -------------------------------- ### Activate Virtual Environment (Windows) Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Activates the previously created virtual environment on Windows systems. This ensures that subsequent package installations are isolated to this environment. ```bash .env/Scripts/activate ``` -------------------------------- ### Login via CLI Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Authenticate with the Hugging Face Hub using the command-line interface. This saves your token locally for future use. ```bash huggingface-cli login ``` -------------------------------- ### Get Inference Endpoint Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieves a specific Inference Endpoint by its name. Requires the endpoint name and optionally accepts a namespace to specify the organization or user. ```APIDOC ## get_inference_endpoint() ### Description Retrieves a specific Inference Endpoint by its name. Requires the endpoint name and optionally accepts a namespace to specify the organization or user. ### Method `get_inference_endpoint(name: str, namespace: Optional[str] = None) ### Parameters #### Required Parameters - **name** (str) - The name of the Inference Endpoint to retrieve. #### Optional Parameters - **namespace** (str) - The namespace (username or organization) the endpoint belongs to. Defaults to the current user's namespace if not provided. ### Returns - **InferenceEndpoint** - An object representing the requested Inference Endpoint. ### Request Example ```python from huggingface_hub import get_inference_endpoint endpoint = get_inference_endpoint("my-endpoint-name") ``` ``` -------------------------------- ### Download an Entire Repository with huggingface-cli Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Download all files from a repository by specifying only the repository ID. The command will fetch all contents of the specified repo. ```bash >>> huggingface-cli download HuggingFaceH4/zephyr-7b-beta Fetching 23 files: 0%| | 0/23 [00:00>> huggingface-cli scan-cache REPO ID REPO TYPE SIZE ON DISK NB FILES LAST_ACCESSED LAST_MODIFIED REFS LOCAL PATH --------------------------- --------- ------------ -------- ------------- ------------- ------------------- ------------------------------------------------------------------------- glue dataset 116.3K 15 4 days ago 4 days ago 2.4.0, main, 1.17.0 /home/wauplin/.cache/huggingface/hub/datasets--glue google/fleurs dataset 64.9M 6 1 week ago 1 week ago refs/pr/1, main /home/wauplin/.cache/huggingface/hub/datasets--google--fleurs Jean-Baptiste/camembert-ner model 441.0M 7 2 weeks ago 16 hours ago main /home/wauplin/.cache/huggingface/hub/models--Jean-Baptiste--camembert-ner bert-base-cased model 1.9G 13 1 week ago 2 years ago /home/wauplin/.cache/huggingface/hub/models--bert-base-cased t5-base model 10.1K 3 3 months ago 3 months ago main /home/wauplin/.cache/huggingface/hub/models--t5-base t5-small model 970.7M 11 3 days ago 3 days ago refs/pr/1, main /home/wauplin/.cache/huggingface/hub/models--t5-small Done in 0.0s. Scanned 6 repo(s) for a total of 3.4G. Got 1 warning(s) while scanning. Use -vvv to print details. ``` -------------------------------- ### Get Inference Endpoint Information Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieves information about a specific Inference Endpoint. This method allows users to fetch details about a deployed model's endpoint. ```APIDOC ## GET /inference-endpoints/{endpoint_id} ### Description Retrieves information about a specific Inference Endpoint. ### Method GET ### Endpoint /inference-endpoints/{endpoint_id} ### Parameters #### Path Parameters - **endpoint_id** (string) - Required - The unique identifier of the Inference Endpoint. ### Response #### Success Response (200) - **id** (string) - The unique identifier of the Inference Endpoint. - **model_id** (string) - The identifier of the model deployed on the endpoint. - **status** (string) - The current status of the endpoint (e.g., PENDING, RUNNING, DELETING). - **region** (string) - The cloud region where the endpoint is deployed. - **created_at** (string) - Timestamp when the endpoint was created. - **updated_at** (string) - Timestamp when the endpoint was last updated. #### Response Example { "id": "my-endpoint-id", "model_id": "gpt2", "status": "RUNNING", "region": "us-east-1", "created_at": "2023-10-27T10:00:00Z", "updated_at": "2023-10-27T10:05:00Z" } ``` -------------------------------- ### Scale Inference Endpoint to Zero Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Demonstrates scaling an Inference Endpoint to zero, which allows it to restart automatically on the first inference call with a cold start delay. ```python # Scale to zero >>> endpoint.scale_to_zero() InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='scaledToZero', url='https://jpj7k2q4j805b727.us-east-1.aws.endpoints.huggingface.cloud') # Endpoint is not 'running' but still has a URL and will restart on first call. ``` -------------------------------- ### Initialize InferenceClient Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Replace `InferenceApi` initialization with `InferenceClient`. Ensure the `repo_id` is passed as `model`. ```python from huggingface_hub import InferenceApi inference = InferenceApi(repo_id="bert-base-uncased", token=API_TOKEN) ``` ```python from huggingface_hub import InferenceClient inference = InferenceClient(model="bert-base-uncased", token=API_TOKEN) ``` -------------------------------- ### Download Multiple Files with CLI Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Download multiple files simultaneously using `huggingface-cli download`. A progress bar is displayed, and the command returns the snapshot path containing the downloaded files. ```bash >>> huggingface-cli download gpt2 config.json model.safetensors Fetching 2 files: 100%|████████████████████████████████████████████| 2/2 [00:00<00:00, 23831.27it/s] /home/wauplin/.cache/huggingface/hub/models--gpt2/snapshots/11c5a3d5811f50298f278a704980280950aedb10 ``` -------------------------------- ### Delete cache without TUI Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Starts the cache deletion process by disabling the TUI, which prompts the user to edit a temporary file to select revisions for deletion. ```bash huggingface-cli delete-cache --disable-tui ``` -------------------------------- ### Upload Folder with CLI Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Uploads an entire local folder to a specified repository on the Hugging Face Hub. If `path_in_repo` is omitted, files are uploaded to the root of the repository. ```bash >>> huggingface-cli upload Wauplin/my-cool-model ./models . https://huggingface.co/Wauplin/my-cool-model/tree/main ``` -------------------------------- ### Get Hugging Face Space Runtime Information Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieves the current runtime status and hardware configuration of a Space. Useful for checking if hardware upgrade requests have been processed. ```python >>> runtime = api.get_space_runtime(repo_id=repo_id) >>> runtime.stage "RUNNING_BUILDING" >>> runtime.hardware "cpu-basic" >>> runtime.requested_hardware "t4-medium" ``` -------------------------------- ### Download a Space with huggingface-cli Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Download the contents of a Hugging Face Space by providing its repo ID and using the `--repo-type space` flag. ```bash # https://huggingface.co/spaces/HuggingFaceH4/zephyr-chat >>> huggingface-cli download HuggingFaceH4/zephyr-chat --repo-type space ``` -------------------------------- ### Download a Single File with CLI Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Use `huggingface-cli download` to fetch a specific file from the Hub. The command prints the absolute path to the downloaded file. ```bash >>> huggingface-cli download gpt2 config.json /home/wauplin/.cache/huggingface/hub/models--gpt2/snapshots/11c5a3d5811f50298f278a704980280950aedb10/config.json ``` -------------------------------- ### PyTorchModelHubMixin Inheritance Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Shows the basic inheritance structure for creating a PyTorch model class that integrates with the Hugging Face Hub. This is the starting point for custom PyTorch integrations. ```python from huggingface_hub import ModelHubMixin class PyTorchModelHubMixin(ModelHubMixin): (...) ``` -------------------------------- ### Duplicate Space with Hardware and Storage Configuration Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Configures hardware, storage, and sleep time when duplicating a Space. This ensures the new Space starts with the desired resource allocation. ```python >>> api.duplicate_space( ... from_id=repo_id, ... hardware="cpu-upgrade", ... storage="small", ... sleep_time="7200", # 2 hours in secs ... ) ``` -------------------------------- ### Download a Single File with huggingface-cli Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Download a specific file from a repository by providing the repo ID and filename. The command prints the local path to the downloaded file. ```bash >>> huggingface-cli download gpt2 config.json downloading https://huggingface.co/gpt2/resolve/main/config.json to /home/wauplin/.cache/huggingface/hub/tmpwrq8dm5o (…)ingface.co/gpt2/resolve/main/config.json: 100%|██████████████████████████████████| 665/665 [00:00<00:00, 2.49MB/s] /home/wauplin/.cache/huggingface/hub/models--gpt2/snapshots/11c5a3d5811f50298f278a704980280950aedb10/config.json ``` -------------------------------- ### Run Text Generation Inference Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Demonstrates how to perform text generation using the synchronous client and the asynchronous client for an Inference Endpoint. ```python # Run text_generation task: >>> endpoint.client.text_generation("I am") ' not a fan of the idea of a "big-budget" movie. I think it\'s a' # Or in an asyncio context: >>> await endpoint.async_client.text_generation("I am") ``` -------------------------------- ### List Inference Endpoints Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Retrieves a list of all Inference Endpoints. You can filter by namespace to get endpoints from a specific organization or user. Using '*' lists endpoints from all organizations the user belongs to. ```APIDOC ## list_inference_endpoints() ### Description Retrieves a list of all Inference Endpoints. You can filter by namespace to get endpoints from a specific organization or user. Using '*' lists endpoints from all organizations the user belongs to. ### Method `list_inference_endpoints(namespace: Optional[str] = None) ### Parameters #### Optional Parameters - **namespace** (str) - The namespace to filter endpoints by. Can be a username, organization name, or "*" to list all endpoints across all accessible namespaces. ### Returns - **List[InferenceEndpoint]** - A list of InferenceEndpoint objects. ### Request Example ```python from huggingface_hub import list_inference_endpoints # List all endpoints from an organization endpoints = list_inference_endpoints(namespace="huggingface") # List all endpoints from all organizations the user belongs to all_endpoints = list_inference_endpoints(namespace="*") ``` ``` -------------------------------- ### Implementing _from_pretrained for PyTorch Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Illustrates the implementation of the `_from_pretrained` class method for PyTorchModelHubMixin. This method handles loading pretrained weights from the Hub or a local directory. ```python class PyTorchModelHubMixin(ModelHubMixin): (...) @classmethod # Must be a classmethod! def _from_pretrained( cls, *, model_id: str, revision: str, cache_dir: str, force_download: bool, proxies: Optional[Dict], resume_download: bool, local_files_only: bool, token: Union[str, bool, None], map_location: str = "cpu", # additional argument strict: bool = False, # additional argument **model_kwargs, ): """Load Pytorch pretrained weights and return the loaded model.""" model = cls(**model_kwargs) if os.path.isdir(model_id): print("Loading weights from local directory") model_file = os.path.join(model_id, SAFETENSORS_SINGLE_FILE) return cls._load_as_safetensor(model, model_file, map_location, strict) model_file = hf_hub_download( repo_id=model_id, filename=SAFETENSORS_SINGLE_FILE, revision=revision, cache_dir=cache_dir, force_download=force_download, proxies=proxies, resume_download=resume_download, token=token, local_files_only=local_files_only, ) return cls._load_as_safetensor(model, model_file, map_location, strict) ``` -------------------------------- ### Get and List Inference Endpoints Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt Demonstrates how to retrieve a specific Inference Endpoint by name or list all available endpoints. Supports filtering by namespace or listing across all accessible organizations. ```python from huggingface_hub import get_inference_endpoint, list_inference_endpoints # Get one >>> get_inference_endpoint("my-endpoint-name") InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='pending', url=None) # List all endpoints from an organization >>> list_inference_endpoints(namespace="huggingface") [InferenceEndpoint(name='aws-starchat-beta', namespace='huggingface', repository='HuggingFaceH4/starchat-beta', status='paused', url=None), ...] # List all endpoints from all organizations the user belongs to >>> list_inference_endpoints(namespace="*") [InferenceEndpoint(name='aws-starchat-beta', namespace='huggingface', repository='HuggingFaceH4/starchat-beta', status='paused', url=None), ...] ``` -------------------------------- ### Create a Dataset Repository Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt To create a repository of a different type, such as a dataset, specify the `repo_type` parameter in `create_repo()`. ```python from huggingface_hub import create_repo create_repo("lysandre/test-dataset", repo_type="dataset") ``` -------------------------------- ### Listing Available Tokens Source: https://huggingface-projects-docs-llms-txt.hf.space/huggingface_hub/llms.txt View all access tokens currently saved on your machine using the CLI. ```bash huggingface-cli auth list ```