HuggingfaceClient.Hub.Spaces (huggingface_client v0.1.0)

Copy Markdown View Source

Manage Hugging Face Spaces — hosted ML applications.

Covers secrets, environment variables, hardware management, Space lifecycle (pause, restart, duplicate), and dev mode for iterative development.

See: https://huggingface.co/docs/hub/spaces-overview

Example

# Get Space runtime info
{:ok, runtime} = HuggingfaceClient.get_space_runtime("my-org/my-space",
  access_token: "hf_..."
)

# Set a secret
:ok = HuggingfaceClient.add_space_secret("my-org/my-space",
  key: "API_KEY",
  value: "my-api-key",
  access_token: "hf_..."
)

# Upgrade hardware
{:ok, runtime} = HuggingfaceClient.request_space_hardware("my-org/my-space",
  hardware: "t4-medium",
  access_token: "hf_..."
)

Summary

Functions

Adds or updates a secret environment variable in a Space.

Adds or updates a public environment variable in a Space.

Deletes a secret from a Space.

Deletes an environment variable from a Space.

Enables or disables dev mode on a Space for interactive development.

Duplicates a Space to the authenticated user's account.

Gets the current runtime of a Space (hardware, stage, etc.).

Gets all environment variables for a Space.

Lists all secrets for a Space (returns keys but NOT values).

Pauses a Space (stops compute billing; Space enters sleeping state).

Requests a hardware upgrade or downgrade for a Space.

Restarts a paused or errored Space.

Sets the sleep time for a Space (auto-pause after inactivity).

Functions

add_secret(space_id, opts \\ [])

@spec add_secret(
  String.t(),
  keyword()
) :: :ok | {:error, Exception.t()}

Adds or updates a secret environment variable in a Space.

Secrets are not exposed in the Space's environment to other users.

Example

:ok = HuggingfaceClient.add_space_secret("my-org/my-space",
  key: "OPENAI_API_KEY",
  value: "sk-...",
  description: "OpenAI API key for GPT-4",
  access_token: "hf_..."
)

add_variable(space_id, opts \\ [])

@spec add_variable(
  String.t(),
  keyword()
) :: :ok | {:error, Exception.t()}

Adds or updates a public environment variable in a Space.

Unlike secrets, variables are visible in the Space settings.

Example

:ok = HuggingfaceClient.add_space_variable("my-org/my-space",
  key: "MODEL_ID",
  value: "gpt2",
  access_token: "hf_..."
)

delete_secret(space_id, opts \\ [])

@spec delete_secret(
  String.t(),
  keyword()
) :: :ok | {:error, Exception.t()}

Deletes a secret from a Space.

Example

:ok = HuggingfaceClient.delete_space_secret("my-org/my-space",
  key: "OLD_API_KEY",
  access_token: "hf_..."
)

delete_variable(space_id, opts \\ [])

@spec delete_variable(
  String.t(),
  keyword()
) :: :ok | {:error, Exception.t()}

Deletes an environment variable from a Space.

dev_mode(space_id, opts \\ [])

@spec dev_mode(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Enables or disables dev mode on a Space for interactive development.

In dev mode, you can push changes directly and restart the Space without a full Docker rebuild.

Example

{:ok, info} = HuggingfaceClient.space_dev_mode("my-org/my-space",
  enable: true,
  access_token: "hf_..."
)

duplicate(from_id, opts \\ [])

@spec duplicate(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Duplicates a Space to the authenticated user's account.

Options

  • :private — whether the duplicated Space is private (default: same as source)
  • :exist_ok — if true, don't raise if destination already exists
  • :access_token

Example

{:ok, new_space} = HuggingfaceClient.duplicate_space("gradio/hello_world",
  private: true,
  access_token: "hf_..."
)

get_runtime(space_id, opts \\ [])

@spec get_runtime(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Gets the current runtime of a Space (hardware, stage, etc.).

Example

{:ok, runtime} = HuggingfaceClient.get_space_runtime("gradio/hello_world",
  access_token: "hf_..."
)
IO.puts("Hardware: #{runtime["hardware"]["current"]}")
IO.puts("Stage: #{runtime["stage"]}")

get_variables(space_id, opts \\ [])

@spec get_variables(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Gets all environment variables for a Space.

Example

{:ok, vars} = HuggingfaceClient.get_space_variables("my-org/my-space",
  access_token: "hf_..."
)
IO.inspect(vars)  # %{"MODEL_ID" => %{"value" => "gpt2", ...}}

list_secrets(space_id, opts \\ [])

@spec list_secrets(
  String.t(),
  keyword()
) :: {:ok, [map()]} | {:error, Exception.t()}

Lists all secrets for a Space (returns keys but NOT values).

Example

{:ok, secrets} = HuggingfaceClient.list_space_secrets("my-org/my-space",
  access_token: "hf_..."
)
Enum.each(secrets, fn s -> IO.puts(s["key"]) end)

pause(space_id, opts \\ [])

@spec pause(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Pauses a Space (stops compute billing; Space enters sleeping state).

Example

{:ok, runtime} = HuggingfaceClient.pause_space("my-org/my-space",
  access_token: "hf_..."
)

request_hardware(space_id, opts \\ [])

@spec request_hardware(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Requests a hardware upgrade or downgrade for a Space.

Hardware options

"cpu-basic", "cpu-upgrade", "t4-small", "t4-medium", "a10g-small", "a10g-large", "a10g-largex2", "a100-large", "zero-a10g"

Example

{:ok, runtime} = HuggingfaceClient.request_space_hardware("my-org/my-space",
  hardware: "t4-medium",
  sleep_time: 3600,
  access_token: "hf_..."
)

restart(space_id, opts \\ [])

@spec restart(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Restarts a paused or errored Space.

Example

{:ok, runtime} = HuggingfaceClient.restart_space("my-org/my-space",
  factory_reboot: true,
  access_token: "hf_..."
)

set_sleep_time(space_id, opts \\ [])

@spec set_sleep_time(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Exception.t()}

Sets the sleep time for a Space (auto-pause after inactivity).

Example

{:ok, runtime} = HuggingfaceClient.set_space_sleep_time("my-org/my-space",
  sleep_time: 1800,
  access_token: "hf_..."
)