File size: 2,809 Bytes
53b8e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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。"