跳到主要内容

Managing Spaces with Github Actions

你可以借助 Github Actions,让你的应用与 GitHub 仓库保持自动同步。需要注意的是,当文件大于 10MB 时,Spaces 要求使用 Git-LFS。如果你不想使用 Git-LFS,就需要检查仓库中的大文件以及历史记录,并考虑清理它们。可以使用 BFG Repo-Cleaner 这类工具,从历史记录中删除体积过大的文件;BFG Repo-Cleaner 会在本地保留一份仓库的备份副本。

首先,你需要把 GitHub 仓库和 Spaces 应用配好,让 Space 作为现有 Git 仓库的一个额外远程地址(remote):

git remote add space https://huggingface.co/spaces/HF_USERNAME/SPACE_NAME

然后,先做一次强制推送,把当前内容同步到 Spaces:

git push --force space main

接下来,配置一个 GitHub Action,用来在每次更新 main 分支时自动推送到 Spaces。下面的示例中,你需要:

  • HF_USERNAME 替换为你的 Hugging Face 用户名,将 SPACE_NAME 替换为你的 Space 名称。
  • 在 GitHub 仓库中创建一个名为 HF_TOKENGithub secret。你可以在 Hugging Face 个人主页的 API Tokens 页面找到自己的 API token。
name: Sync to Hugging Face hub
on:
push:
branches: [main]

# to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
sync-to-hub:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
lfs: true
- name: Push to hub
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: git push https://HF_USERNAME:[email protected]/spaces/HF_USERNAME/SPACE_NAME main

最后,你可以再创建一个 Action,自动检查每个新的 pull request 中是否包含过大的文件:

name: Check file size
on: # or directly `on: [push]` to run the action on every push on any branch
pull_request:
branches: [main]

# to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
sync-to-hub:
runs-on: ubuntu-latest
steps:
- name: Check large files
uses: ActionsDesk/lfs-[email protected]
with:
filesizelimit: 10485760 # this is 10MB so we can sync to HF Spaces