ole2242527 commited on
Commit
53b8e05
·
verified ·
1 Parent(s): f283c78

Upload scripts/deploy-hf-space.ps1 with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/deploy-hf-space.ps1 +112 -0
scripts/deploy-hf-space.ps1 ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ param(
2
+ [string] $SpaceName = "sub2api",
3
+ [string] $Owner = "",
4
+ [switch] $Private
5
+ )
6
+
7
+ $ErrorActionPreference = "Stop"
8
+
9
+ if (-not $env:HF_TOKEN) {
10
+ throw "请先设置环境变量 HF_TOKEN。示例:`$env:HF_TOKEN = 'hf_xxx'"
11
+ }
12
+
13
+ $root = Resolve-Path (Join-Path $PSScriptRoot "..")
14
+ Set-Location $root
15
+
16
+ $requiredFiles = @(
17
+ "Dockerfile",
18
+ "start-space.sh",
19
+ "README.md",
20
+ "PUBLIC_USAGE.md",
21
+ "ADMIN_SETUP.md",
22
+ ".env.example",
23
+ ".gitattributes",
24
+ ".gitignore"
25
+ )
26
+
27
+ foreach ($file in $requiredFiles) {
28
+ if (-not (Test-Path -LiteralPath (Join-Path $root $file))) {
29
+ throw "缺少部署文件:$file"
30
+ }
31
+ }
32
+
33
+ $headers = @{
34
+ Authorization = "Bearer $env:HF_TOKEN"
35
+ }
36
+
37
+ $whoami = Invoke-RestMethod `
38
+ -Uri "https://huggingface.co/api/whoami-v2" `
39
+ -Headers $headers `
40
+ -Method GET
41
+
42
+ if (-not $Owner) {
43
+ $Owner = $whoami.name
44
+ }
45
+
46
+ $repoId = "$Owner/$SpaceName"
47
+ $createBody = @{
48
+ name = $SpaceName
49
+ type = "space"
50
+ sdk = "docker"
51
+ private = [bool] $Private
52
+ }
53
+
54
+ if ($Owner -ne $whoami.name) {
55
+ $createBody.organization = $Owner
56
+ }
57
+
58
+ $json = $createBody | ConvertTo-Json -Depth 5
59
+
60
+ try {
61
+ Invoke-RestMethod `
62
+ -Uri "https://huggingface.co/api/repos/create" `
63
+ -Headers ($headers + @{ "Content-Type" = "application/json" }) `
64
+ -Method POST `
65
+ -Body $json | Out-Null
66
+ Write-Host "已创建 Hugging Face Space:$repoId"
67
+ } catch {
68
+ $statusCode = $null
69
+ if ($_.Exception.Response) {
70
+ $statusCode = [int] $_.Exception.Response.StatusCode
71
+ }
72
+
73
+ if ($statusCode -eq 409) {
74
+ Write-Host "Space 已存在,继续推送文件:$repoId"
75
+ } else {
76
+ throw
77
+ }
78
+ }
79
+
80
+ git add Dockerfile start-space.sh README.md PUBLIC_USAGE.md ADMIN_SETUP.md .env.example .gitattributes .gitignore package.json scripts/check-supabase.mjs scripts/generate-secrets.ps1 scripts/deploy-hf-space.ps1
81
+
82
+ $hasCommit = $true
83
+ git rev-parse --verify HEAD *> $null
84
+ if ($LASTEXITCODE -ne 0) {
85
+ $hasCommit = $false
86
+ }
87
+
88
+ $hasStagedChanges = $false
89
+ git diff --cached --quiet
90
+ if ($LASTEXITCODE -ne 0) {
91
+ $hasStagedChanges = $true
92
+ }
93
+
94
+ if ($hasStagedChanges) {
95
+ git commit -m "Deploy sub2api HF Space"
96
+ } elseif (-not $hasCommit) {
97
+ throw "没有可提交的文件,请检查 git 状态。"
98
+ } else {
99
+ Write-Host "没有新的本地文件需要提交。"
100
+ }
101
+
102
+ $pushUrl = "https://hf_user:$($env:HF_TOKEN)@huggingface.co/spaces/$repoId"
103
+ git push $pushUrl HEAD:main
104
+
105
+ Write-Host ""
106
+ Write-Host "部署文件已推送。Space 页面:"
107
+ Write-Host "https://huggingface.co/spaces/$repoId"
108
+ Write-Host ""
109
+ Write-Host "构建完成后的公开访问地址:"
110
+ Write-Host "https://$($Owner)-$($SpaceName).hf.space"
111
+ Write-Host ""
112
+ Write-Host "下一步:到 Space Settings -> Variables and secrets 添加 Supabase 和管理员 Secrets。"