Skip to content

Sync Github Repo to Huggingface Hub

Steps

Create a huggingface token

Put it in the repo secrets with name like HF_TOKEN in related Github repo setitngs:

Run following commands in the git root folder:

sh
git remote add space https://huggingface.co/spaces/<user_name>/<repo_name>
# git remote -v
git remote set-url space https://<user_name>:<token>@huggingface.co/spaces/<user_name>/<repo_name>
git push --force space main
  • <repo_path> is in the form of:
    • models: <user_name>/<repo_name>
    • datasets: datasets/<user_name>/<repo_name>
    • spaces: spaces/<user_name>/<repo_name>

Automate with Github Actions

Then add a sync_to_huggingface_space.yml in .github/workflows:

  • Replace the <user_name> (x2) and <repo_name> in the last line for current repo.
  • The HF_TOKEN name should be the same with the one specified in the step above.
yml
name: Sync to HuggingFace hub
on:
  push:
    branches: [main]
  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 -f https://<user_name>:$HF_TOKEN@huggingface.co/spaces/<user_name>/<repo_name> main

See: Managing Spaces with Github Actions