Hub documentation

Spaces 开发模式:在 Spaces 中实现无缝开发

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Spaces 开发模式:在 Spaces 中实现无缝开发

Spaces 开发模式是 PRO 或 Team & Enterprise 套餐的一部分。

Spaces 开发模式

Spaces Dev Mode 是一项功能,可简化应用程序的调试过程,并加快 Spaces 的迭代速度。

每次您向 Space 仓库提交更改时,底层的 Docker 镜像都会被重新构建,随后会配置一台新的虚拟机来托管新的容器。

开发模式允许你通过覆盖 Docker 镜像来更快地更新你的 Space。

Dev Mode Docker 镜像会将您的应用程序作为子进程启动,从而允许您在不停止 Space 容器本身的情况下重启该应用程序。它还会在后台启动一个 SSH 服务器(用于本地编辑器和终端)和一个 VS Code 服务器(用于产品内的 VS Code Web),以便您可以连接到该 Space。

能够连接到正在运行的 Space 解锁了多种用例:

  • 您可以在不每次重建 Space 的情况下修改应用代码
  • 您可以调试正在运行的应用程序并实时监控资源

总体而言,它通过跳过 Docker 镜像重建阶段,使在 Spaces 上进行开发和实验的速度大大加快。

接口

在您的 Space 上启用 Dev Mode 后,您应该会看到如下所示的模态框。

screenshot of the dev mode controls interface.

更改代码后,应用程序不会自动重启。若要在 Space 中显示您的更改,需要使用 Refresh 按钮来重启应用。

如果您使用的是 Gradio SDK,或者您的应用程序基于 Python,请注意依赖项不会自动安装。 您需要手动在 VS Code 或 SSH 中运行 pip install。

连接

Dev Mode 将正在运行的 Space 暴露为一个标准的 SSH 主机。VS Code 及其他任何编辑器都通过该 SSH 端点进行连接。

SSH

Dev Mode 运行后,使用以下命令连接:

ssh <space-subdomain>@ssh.hf.space

子域名显示在 Dev Mode 控制模态框中。您也可以通过编程方式获取它:

from huggingface_hub import HfApi
print(HfApi().space_info("namespace/repo").subdomain)

Hugging Face CLI 可以为您解析子域名并打开会话:

# SSH into the Space's Dev Mode container
hf spaces ssh username/my-space

# Print the SSH command instead of running it
hf spaces ssh username/my-space --dry-run

# Use a specific identity file
hf spaces ssh username/my-space -i ~/.ssh/id_ed25519

传入 --auto 可在未运行时自动启用 Dev Mode,而无需提示。有关完整选项列表,请参阅 hf spaces ssh CLI 参考。

您需要将机器的 SSH 公钥添加到您的用户账户中,才能通过 SSH 连接到 Space。 有关更详细的说明,请参阅Git over SSH文档。

VS Code

您也可以使用本地安装的 VS Code 连接到 Space 容器。为此,请安装 Remote - SSH 扩展,并连接到相同的 <space-subdomain>@ssh.hf.space 主机。

持久化更改

在启用开发模式时所做的更改不会自动持久化到 Space 仓库中。 默认情况下,当禁用开发模式或 Space 进入休眠状态时,这些更改将被丢弃。
If you wish to persist changes made while Dev Mode is enabled, you need to use `git` from inside the Space container (using VS Code or SSH). For example:
# Add changes and commit them
git add .
git commit -m "Persist changes from Dev Mode"

# Push the commit to persist them in the repo
git push

如果该 Space 中存在未提交或未推送的更改,模态框将显示警告:

screenshot of the dev mode controls interface.

启用开发模式

您可以从 Web 界面或通过 API 为您的 Space 启用 Dev Mode。

通过 API

您可以通过编程方式切换 Dev Mode:

POST https://huggingface.co/api/spaces/{namespace}/{repo}/dev-mode
Content-Type: application/json
Authorization: Bearer {token}

{
  "enabled": true
}

通过 Web 界面

screenshot of the dev mode toggle from the contextual menu.
screenshot of the dev mode toggle from the Space settings.
screenshot of the dev mode toggle from the Space logs.

您也可以创建一个启用开发模式的 Space:

screenshot of the dev mode toggle from the Space creation page.

局限性

Dev Mode 目前不适用于静态 Spaces。Docker Spaces 还有一些额外的要求。

Docker Spaces

Dev Mode 支持 Docker Spaces。但是,您的 Space 需要遵循以下规则,Dev Mode 才能正常工作。

  1. 必须安装以下软件包:
  • bash(用于建立 SSH 连接)
  • curl、wget 和 procps(VS Code 服务器进程所需)
  • git 和 git-lfs,以便从您的 Dev Mode 环境中提交并推送更改
  1. 您的应用代码必须位于 /app 文件夹中,以便 Dev Mode 守护进程能够检测到更改。

  2. /app 文件夹必须由 uid 为 1000 的用户拥有,以便您能够修改代码。

  3. Dockerfile 必须包含用于启动的 CMD 指令。有关 CMD 指令的详细信息,请参阅 Docker 的文档。

当基础镜像基于 Debian(例如 Ubuntu)时,Dev Mode 可以正常工作。

更小众的 Linux 发行版(例如 alpine)未经过测试,Dev Mode 在这些发行版上无法保证正常工作。

兼容的 Dockerfile 示例

这是一个兼容 Spaces Dev Mode 的 Dockerfile 示例。

它使用 apt-get 安装所需的软件包,以及几个方便开发者使用的额外软件包(即:top、vim 和 nano)。 然后,它从 /app 启动一个 NodeJS 应用程序。

FROM node:19-slim

RUN apt-get update && \
    apt-get install -y \
      bash \
      git git-lfs \
      wget curl procps \
      htop vim nano && \
    rm -rf /var/lib/apt/lists/*


WORKDIR /app
COPY --link ./ /app
RUN  npm i 

RUN chown 1000 /app
USER 1000
CMD ["node", "index.js"]

本组织中有多个兼容 Dev Mode 的 Docker Spaces 示例。 欢迎将它们复制到您自己的命名空间中!

Python 示例应用(FastAPI HTTP 服务器):https://huggingface.co/spaces/dev-mode-explorers/dev-mode-python

JavaScript 示例应用(Express.js HTTP 服务器):https://huggingface.co/spaces/dev-mode-explorers/dev-mode-javascript

反馈

您可以直接在 HF Hub 上分享您对 Spaces Dev Mode 的反馈:https://huggingface.co/spaces/dev-mode-explorers/README/discussions

Update on GitHub