| param( |
| [string] $SpaceName = "sub2api", |
| [string] $Owner = "", |
| [switch] $Private |
| ) |
|
|
| $ErrorActionPreference = "Stop" |
|
|
| if (-not $env:HF_TOKEN) { |
| throw "请先设置环境变量 HF_TOKEN。示例:`$env:HF_TOKEN = 'hf_xxx'" |
| } |
|
|
| $root = Resolve-Path (Join-Path $PSScriptRoot "..") |
| Set-Location $root |
|
|
| $requiredFiles = @( |
| "Dockerfile", |
| "start-space.sh", |
| "README.md", |
| "PUBLIC_USAGE.md", |
| "ADMIN_SETUP.md", |
| ".env.example", |
| ".gitattributes", |
| ".gitignore" |
| ) |
|
|
| foreach ($file in $requiredFiles) { |
| if (-not (Test-Path -LiteralPath (Join-Path $root $file))) { |
| throw "缺少部署文件:$file" |
| } |
| } |
|
|
| $headers = @{ |
| Authorization = "Bearer $env:HF_TOKEN" |
| } |
|
|
| $whoami = Invoke-RestMethod ` |
| -Uri "https://huggingface.co/api/whoami-v2" ` |
| -Headers $headers ` |
| -Method GET |
|
|
| if (-not $Owner) { |
| $Owner = $whoami.name |
| } |
|
|
| $repoId = "$Owner/$SpaceName" |
| $createBody = @{ |
| name = $SpaceName |
| type = "space" |
| sdk = "docker" |
| private = [bool] $Private |
| } |
|
|
| if ($Owner -ne $whoami.name) { |
| $createBody.organization = $Owner |
| } |
|
|
| $json = $createBody | ConvertTo-Json -Depth 5 |
|
|
| try { |
| Invoke-RestMethod ` |
| -Uri "https://huggingface.co/api/repos/create" ` |
| -Headers ($headers + @{ "Content-Type" = "application/json" }) ` |
| -Method POST ` |
| -Body $json | Out-Null |
| Write-Host "已创建 Hugging Face Space:$repoId" |
| } catch { |
| $statusCode = $null |
| if ($_.Exception.Response) { |
| $statusCode = [int] $_.Exception.Response.StatusCode |
| } |
|
|
| if ($statusCode -eq 409) { |
| Write-Host "Space 已存在,继续推送文件:$repoId" |
| } else { |
| throw |
| } |
| } |
|
|
| 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 |
|
|
| $hasCommit = $true |
| git rev-parse --verify HEAD *> $null |
| if ($LASTEXITCODE -ne 0) { |
| $hasCommit = $false |
| } |
|
|
| $hasStagedChanges = $false |
| git diff --cached --quiet |
| if ($LASTEXITCODE -ne 0) { |
| $hasStagedChanges = $true |
| } |
|
|
| if ($hasStagedChanges) { |
| git commit -m "Deploy sub2api HF Space" |
| } elseif (-not $hasCommit) { |
| throw "没有可提交的文件,请检查 git 状态。" |
| } else { |
| Write-Host "没有新的本地文件需要提交。" |
| } |
|
|
| $pushUrl = "https://hf_user:$($env:HF_TOKEN)@huggingface.co/spaces/$repoId" |
| git push $pushUrl HEAD:main |
|
|
| Write-Host "" |
| Write-Host "部署文件已推送。Space 页面:" |
| Write-Host "https://huggingface.co/spaces/$repoId" |
| Write-Host "" |
| Write-Host "构建完成后的公开访问地址:" |
| Write-Host "https://$($Owner)-$($SpaceName).hf.space" |
| Write-Host "" |
| Write-Host "下一步:到 Space Settings -> Variables and secrets 添加 Supabase 和管理员 Secrets。" |
|
|