From 3de5572c4590768926e9d03b97f4d1c7ea1fbfee Mon Sep 17 00:00:00 2001 From: Ayush <106433170+Plasma-69@users.noreply.github.com> Date: Fri, 20 Dec 2024 00:12:27 +0530 Subject: [PATCH] docs: add docs to huggingface space restart script (#5051) * Update factory_restart_space.py just added more readability by adding more comments * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida --- scripts/factory_restart_space.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/scripts/factory_restart_space.py b/scripts/factory_restart_space.py index 2911116a1..b7bdfbde5 100644 --- a/scripts/factory_restart_space.py +++ b/scripts/factory_restart_space.py @@ -1,40 +1,51 @@ # /// script # requires-python = ">=3.10" # dependencies = [ -# "huggingface-hub", -# "rich", +# "huggingface-hub", # Library for interacting with the Hugging Face Hub API. +# "rich", # Library for colourful and formatted console output. # ] # /// + import argparse import sys -from huggingface_hub import HfApi, list_models +from huggingface_hub import HfApi, list_models # Import required functions and classes from huggingface-hub. from rich import print # noqa: A004 -# Use root method +# Fetch and list all models available on the Hugging Face Hub. +# This part is unrelated to restarting a space but demonstrates the usage of list_models. models = list_models() +# Initialize an argument parser to handle command-line inputs. args = argparse.ArgumentParser(description="Restart a space in the Hugging Face Hub.") -args.add_argument("--space", type=str, help="The space to restart.") -args.add_argument("--token", type=str, help="The Hugging Face API token.") +args.add_argument("--space", type=str, help="The space to restart.") # Argument for specifying the space name. +args.add_argument("--token", type=str, help="The Hugging Face API token.") # Argument for providing the API token. +# Parse the command-line arguments. parsed_args = args.parse_args() +# Extract the space name from the parsed arguments. space = parsed_args.space +# Check if the space name is provided; exit with an error message if not. if not space: print("Please provide a space to restart.") sys.exit() +# Check if the API token is provided; exit with an error message if not. if not parsed_args.token: print("Please provide an API token.") sys.exit() -# Or configure a HfApi client +# Create an instance of the HfApi class to interact with the Hugging Face Hub. hf_api = HfApi( - endpoint="https://huggingface.co", # Can be a Private Hub endpoint. - token=parsed_args.token, + endpoint="https://huggingface.co", # Base endpoint URL for Hugging Face Hub. + token=parsed_args.token, # API token used for authentication. ) +# Restart the specified space with a factory reboot. +# The `factory_reboot=True` option resets the space to its original state. space_runtime = hf_api.restart_space(space, factory_reboot=True) + +# Print the runtime status of the restarted space. print(space_runtime)