billyaungmyint commited on
Commit
f643bf9
Β·
verified Β·
1 Parent(s): 36b57c8

Sync from GitHub via hub-sync

Browse files
Files changed (2) hide show
  1. VERSION +1 -1
  2. docs/github-hf-custom-domain-guide.md +218 -0
VERSION CHANGED
@@ -1 +1 @@
1
- ba0f6e79436de596d18bfca048d749658bad26c4
 
1
+ 78b2e883b6862c281483b27ff658e551d14e4bbe
docs/github-hf-custom-domain-guide.md ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GitHub β†’ Hugging Face Space with Custom Domain β€” Setup Guide
2
+
3
+ A complete, repeatable playbook. Follow in order. Sections marked ⚠️ are the common pitfall spots.
4
+
5
+ ---
6
+
7
+ ## Table of Contents
8
+
9
+ 1. [Prerequisites](#1-prerequisites)
10
+ 2. [Create the Hugging Face Space](#2-create-the-hugging-face-space)
11
+ 3. [Configure the GitHub Repository](#3-configure-the-github-repository)
12
+ 4. [Set Up the Sync Workflow](#4-set-up-the-sync-workflow)
13
+ 5. [README Front Matter (Critical)](#5-readme-front-matter-critical)
14
+ 6. [Custom Domain Setup](#6-custom-domain-setup)
15
+ 7. [Gradio `root_path` Rule](#7-gradio-root_path-rule)
16
+ 8. [Verify End-to-End](#8-verify-end-to-end)
17
+ 9. [Troubleshooting Reference](#9-troubleshooting-reference)
18
+
19
+ ---
20
+
21
+ ## 1. Prerequisites
22
+
23
+ | What | Where to get it |
24
+ |---|---|
25
+ | Hugging Face account | https://huggingface.co |
26
+ | HF write token (fine-grained or classic) | HF β†’ Settings β†’ Access Tokens β†’ **New token** β†’ Role: **Write** |
27
+ | GitHub repo (public or private) | github.com |
28
+ | Domain registrar access (for custom domain) | Your registrar (Cloudflare, Namecheap, etc.) |
29
+
30
+ ---
31
+
32
+ ## 2. Create the Hugging Face Space
33
+
34
+ 1. Go to https://huggingface.co/new-space
35
+ 2. Fill in:
36
+ - **Owner**: your HF username or org
37
+ - **Space name**: e.g. `github-sync-test`
38
+ - **SDK**: Gradio (or Streamlit)
39
+ - **Visibility**: Public or Private
40
+ 3. Click **Create Space** β€” the Space is created with a default `README.md`.
41
+
42
+ > You do **not** need to push any code manually; the GitHub Action will do it.
43
+
44
+ ---
45
+
46
+ ## 3. Configure the GitHub Repository
47
+
48
+ ### 3.1 Add the HF token as a GitHub Secret
49
+
50
+ 1. GitHub repo β†’ **Settings** β†’ **Secrets and variables** β†’ **Actions**
51
+ 2. Click **New repository secret**
52
+ - Name: `HF_TOKEN`
53
+ - Value: your HF write token from step 1
54
+ 3. Save.
55
+
56
+ ### 3.2 Minimum required files
57
+
58
+ ```
59
+ your-repo/
60
+ β”œβ”€β”€ README.md ← MUST have HF front matter (see Β§5)
61
+ β”œβ”€β”€ main.py ← your Gradio app entry point
62
+ β”œβ”€β”€ requirements.txt ← at minimum: gradio>=5.0
63
+ └── .github/
64
+ └── workflows/
65
+ └── sync.yml
66
+ ```
67
+
68
+ ---
69
+
70
+ ## 4. Set Up the Sync Workflow
71
+
72
+ Create `.github/workflows/sync.yml`:
73
+
74
+ ```yaml
75
+ name: Sync to Hugging Face
76
+
77
+ on:
78
+ push:
79
+ branches: [master] # or "main" β€” match your default branch
80
+ workflow_dispatch: # allows manual trigger from GitHub UI
81
+
82
+ jobs:
83
+ sync:
84
+ runs-on: ubuntu-latest
85
+ steps:
86
+ - uses: actions/checkout@v4
87
+ with:
88
+ fetch-depth: 0 # full history required by hub-sync
89
+ lfs: true
90
+
91
+ - name: Write VERSION file
92
+ run: echo "${{ github.sha }}" > VERSION
93
+
94
+ - uses: huggingface/hub-sync@main
95
+ with:
96
+ github_repo_id: YOUR_GH_USERNAME/YOUR_GH_REPO
97
+ huggingface_repo_id: YOUR_HF_USERNAME/YOUR_SPACE_NAME
98
+ repo_type: space # MUST be "space" for HF Spaces
99
+ hf_token: ${{ secrets.HF_TOKEN }}
100
+ ```
101
+
102
+ **Replace** `YOUR_GH_USERNAME/YOUR_GH_REPO` and `YOUR_HF_USERNAME/YOUR_SPACE_NAME` with your actual values.
103
+
104
+ ---
105
+
106
+ ## 5. README Front Matter (Critical)
107
+
108
+ ⚠️ **The single most common cause of sync failures.**
109
+
110
+ The `README.md` at the repo root **must** start with this YAML front matter block. Without it, HF rejects the push with "Missing configuration in README".
111
+
112
+ ```markdown
113
+ ---
114
+ title: your-space-name
115
+ emoji: "πŸ€—"
116
+ colorFrom: blue
117
+ colorTo: indigo
118
+ sdk: gradio
119
+ app_file: main.py
120
+ pinned: false
121
+ ---
122
+
123
+ # Your Space Title
124
+ ...rest of your README...
125
+ ```
126
+
127
+ | Field | Notes |
128
+ |---|---|
129
+ | `title` | Must match (or at least not conflict with) your HF Space name |
130
+ | `sdk` | `gradio` or `streamlit` |
131
+ | `app_file` | The Python file that launches your app (e.g. `main.py`) |
132
+ | `emoji`, `colorFrom`, `colorTo` | Cosmetic β€” any valid values work |
133
+ | `pinned` | `true` pins the Space on your profile |
134
+
135
+ ---
136
+
137
+ ## 6. Custom Domain Setup
138
+
139
+ ### 6.1 Add the domain in HF Space settings
140
+
141
+ 1. HF Space β†’ **Settings** β†’ **Custom domains**
142
+ 2. Enter your domain: e.g. `demo.example.com`
143
+ 3. HF shows you a **CNAME target** (looks like `billyaungmyint-github-sync-test.hf.space`)
144
+ 4. Copy that CNAME target value.
145
+
146
+ ### 6.2 Add the DNS record at your registrar
147
+
148
+ | Type | Host | Value | TTL |
149
+ |---|---|---|---|
150
+ | `CNAME` | `demo` (subdomain part only) | `billyaungmyint-github-sync-test.hf.space` | 300 or Auto |
151
+
152
+ > If you want an apex/root domain (`example.com`), use a **CNAME Flattening** or **ALIAS** record β€” check your registrar's docs. Most registrars support this via Cloudflare's "CNAME at root" feature.
153
+
154
+ ### 6.3 Wait for DNS propagation
155
+
156
+ - Typical: 1–5 minutes on Cloudflare, up to 48 hours on others.
157
+ - Verify with: `nslookup demo.example.com` β€” should resolve to HF's servers.
158
+ - HF will automatically provision a TLS certificate once the CNAME resolves.
159
+
160
+ ### 6.4 ⚠️ No `GRADIO_ROOT_PATH` needed
161
+
162
+ When using a custom domain at the **root path** (e.g. `https://demo.example.com`):
163
+
164
+ - Do **not** set `GRADIO_ROOT_PATH` in HF Space variables.
165
+ - Leave it unset (or set to `""`).
166
+ - Setting it to the old `.hf.space` URL **after** a domain change causes `"Could not get API info. fetch failed"`.
167
+
168
+ Only set `GRADIO_ROOT_PATH` if your app is served at a **subpath**, e.g. `https://example.com/myapp` β†’ set `GRADIO_ROOT_PATH=/myapp`.
169
+
170
+ ---
171
+
172
+ ## 7. Gradio `root_path` Rule
173
+
174
+ Use this pattern in your `main.py` β€” it is safe for all scenarios:
175
+
176
+ ```python
177
+ import os
178
+
179
+ if __name__ == "__main__":
180
+ # Strip trailing slash to avoid double-slash issues.
181
+ # Leave GRADIO_ROOT_PATH unset (or "") for custom domains at root.
182
+ _root_path = os.getenv("GRADIO_ROOT_PATH", "").rstrip("/")
183
+ demo.launch(server_name="0.0.0.0", root_path=_root_path)
184
+ ```
185
+
186
+ | Deployment scenario | `GRADIO_ROOT_PATH` env var value |
187
+ |---|---|
188
+ | Default `.hf.space` URL | *(unset)* or `""` |
189
+ | Custom domain at root (`myapp.com`) | *(unset)* or `""` |
190
+ | Custom domain at subpath (`myapp.com/demo`) | `/demo` |
191
+ | Local dev | *(unset)* β€” runs fine |
192
+
193
+ ---
194
+
195
+ ## 8. Verify End-to-End
196
+
197
+ Run through this checklist after initial setup or after any DNS/domain change:
198
+
199
+ - [ ] `nslookup your-domain.com` resolves (no NXDOMAIN)
200
+ - [ ] `https://your-domain.com` loads the Space (no TLS error)
201
+ - [ ] Browser DevTools β†’ Network: no failed `/info` or `queue/join` requests
202
+ - [ ] Push a trivial commit to GitHub β†’ Action runs green β†’ Space rebuilds β†’ change appears
203
+ - [ ] HF Space settings β†’ Custom domains β†’ Status shows βœ… (not "Pending")
204
+
205
+ ---
206
+
207
+ ## 9. Troubleshooting Reference
208
+
209
+ | Error | Cause | Fix |
210
+ |---|---|---|
211
+ | `Missing configuration in README` | README has no HF YAML front matter | Add the front matter block (see Β§5) |
212
+ | `Could not get API info. fetch failed` | `GRADIO_ROOT_PATH` set to wrong URL, or DNS not yet propagated | Clear `GRADIO_ROOT_PATH` in Space variables (see Β§6.4 & Β§7) |
213
+ | `HF_TOKEN` secret not found / 401 | Secret missing or token lacks write scope | Re-add secret with a **Write** token (see Β§3.1) |
214
+ | Action fails: `repo_type: space` error | Wrong `repo_type` in workflow | Set `repo_type: space` in `sync.yml` (see Β§4) |
215
+ | Space stuck on "Building" | Dependency error in `requirements.txt` | Check Space build logs; pin a working version |
216
+ | TLS certificate "Pending" for >30 min | CNAME not propagated yet or wrong target | Verify CNAME value matches exactly what HF shows |
217
+ | Custom domain works but assets 404 | `root_path` set to a subpath that doesn't exist | Set `GRADIO_ROOT_PATH=""` or the correct subpath |
218
+ | Space rebuilds but shows old code | `fetch-depth: 0` missing in workflow | Add `fetch-depth: 0` to `actions/checkout@v4` (see Β§4) |