File size: 3,020 Bytes
53b8e05
 
 
 
 
 
 
 
 
f3410f9
53b8e05
 
 
 
 
 
 
f3410f9
53b8e05
 
 
 
 
 
f3410f9
 
 
 
 
53b8e05
 
 
 
f3410f9
53b8e05
 
 
f3410f9
 
53b8e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3410f9
 
53b8e05
 
 
 
 
 
f3410f9
53b8e05
 
 
 
 
f3410f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53b8e05
 
 
 
 
 
 
 
 
 
 
 
 
 
f3410f9
53b8e05
f3410f9
53b8e05
f3410f9
53b8e05
 
 
 
 
 
f3410f9
53b8e05
 
f3410f9
53b8e05
 
f3410f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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."