Spaces:
Sleeping
Sleeping
File size: 2,572 Bytes
90824a5 edb51ad 90824a5 7eff563 90824a5 edb51ad 90824a5 edb51ad 90824a5 edb51ad 90824a5 edb51ad 90824a5 f8dadcb edb51ad 90824a5 | 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 | param(
[Parameter(Mandatory = $true)]
[string]$SpaceId,
[string]$Token = $env:HF_TOKEN,
[string]$RemoteName = "hf",
[string]$CommitMessage = "Deploy to Hugging Face Space"
)
$ErrorActionPreference = "Stop"
if (-not $Token) {
if (Test-Path ".env") {
$tokenLine = Get-Content ".env" | Where-Object { $_ -match "^HF_TOKEN=" } | Select-Object -First 1
if ($tokenLine) {
$Token = ($tokenLine -replace "^HF_TOKEN=", "").Trim()
$Token = $Token.Trim('"').Trim("'")
}
}
}
if (-not $Token) {
throw "HF token not provided. Set HF_TOKEN, keep HF_TOKEN in .env, or pass -Token."
}
if (-not (Test-Path ".git")) {
throw "Run this script from repository root (git repo required)."
}
Write-Host "Logging in to Hugging Face..."
$hfCli = ""
if (Get-Command hf -ErrorAction SilentlyContinue) {
$hfCli = "hf"
} elseif (Test-Path ".venv/Scripts/hf.exe") {
$hfCli = ".venv/Scripts/hf.exe"
} else {
throw "hf CLI not found. Install with: c:/Users/Mahak/FeatureFlag/.venv/Scripts/python.exe -m pip install huggingface_hub"
}
& $hfCli auth login --token $Token --add-to-git-credential --force | Out-Null
if ($LASTEXITCODE -ne 0) {
throw "Hugging Face login failed. Verify HF_TOKEN is valid and has write scope."
}
$hfUrl = "https://huggingface.co/spaces/$SpaceId"
$existingRemote = git remote | Where-Object { $_ -eq $RemoteName }
if (-not $existingRemote) {
Write-Host "Adding remote '$RemoteName' => $hfUrl"
git remote add $RemoteName $hfUrl
if ($LASTEXITCODE -ne 0) {
throw "Failed to add git remote '$RemoteName'."
}
} else {
Write-Host "Remote '$RemoteName' already exists. Updating URL => $hfUrl"
git remote set-url $RemoteName $hfUrl
if ($LASTEXITCODE -ne 0) {
throw "Failed to update git remote '$RemoteName'."
}
}
Write-Host "Committing current changes (if any)..."
git add .
if ($LASTEXITCODE -ne 0) {
throw "Failed to stage changes."
}
git diff --cached --quiet
if ($LASTEXITCODE -ne 0) {
git commit -m $CommitMessage | Out-Null
if ($LASTEXITCODE -ne 0) {
throw "Failed to create commit."
}
Write-Host "Created commit: $CommitMessage"
} else {
Write-Host "No staged changes to commit."
}
Write-Host "Pushing to Hugging Face Space..."
git push $RemoteName HEAD:main --force
if ($LASTEXITCODE -ne 0) {
throw "Push failed. Ensure the Space exists and your token has write access."
}
Write-Host "Deployment pushed successfully."
Write-Host "Space URL: https://huggingface.co/spaces/$SpaceId"
|