sub2api / scripts /deploy-hf-space.ps1
LisonEvf's picture
refactor
f3410f9
Raw
History Blame Contribute Delete
3.02 kB
param(
[string] $SpaceName = "sub2api",
[string] $Owner = "",
[switch] $Private
)
$ErrorActionPreference = "Stop"
if (-not $env:HF_TOKEN) {
throw "Set HF_TOKEN first. Example: `$env:HF_TOKEN = 'hf_xxx'"
}
$root = Resolve-Path (Join-Path $PSScriptRoot "..")
Set-Location $root
$requiredFiles = @(
"Dockerfile",
"sub2api-hf-rootfs.tar",
"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"
)
foreach ($file in $requiredFiles) {
if (-not (Test-Path -LiteralPath (Join-Path $root $file))) {
throw "Missing deployment file: $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
}
try {
Invoke-RestMethod `
-Uri "https://huggingface.co/api/repos/create" `
-Headers ($headers + @{ "Content-Type" = "application/json" }) `
-Method POST `
-Body ($createBody | ConvertTo-Json -Depth 5) | Out-Null
Write-Host "Created Hugging Face Space: $repoId"
} catch {
$statusCode = $null
if ($_.Exception.Response) {
$statusCode = [int] $_.Exception.Response.StatusCode
}
if ($statusCode -eq 409) {
Write-Host "Space already exists, continuing: $repoId"
} else {
throw
}
}
git lfs install
git add `
Dockerfile `
sub2api-hf-rootfs.tar `
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 rootfs tar on HF Space"
} elseif (-not $hasCommit) {
throw "No files were staged and the repository has no commits. Check git status."
} else {
Write-Host "No new local files to commit."
}
$pushUrl = "https://hf_user:$($env:HF_TOKEN)@huggingface.co/spaces/$repoId"
git push $pushUrl HEAD:main
Write-Host ""
Write-Host "Deployment files pushed. Space page:"
Write-Host "https://huggingface.co/spaces/$repoId"
Write-Host ""
Write-Host "Public app URL after build:"
Write-Host "https://$($Owner)-$($SpaceName).hf.space"
Write-Host ""
Write-Host "Next: configure Space Settings -> Variables and secrets, then restart the Space."