File size: 5,075 Bytes
b73a334
 
 
266752e
 
b73a334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266752e
 
 
 
 
b73a334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266752e
b73a334
266752e
b73a334
266752e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b73a334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266752e
 
 
 
 
b73a334
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
param(
    [string]$RepoId = "KLP2-PCD/KLP2",
    [string]$TargetDir = "",
    [switch]$SkipInstall,
    [switch]$SkipCodeExtensions
)

$ErrorActionPreference = "Stop"

function Write-Step {
    param([string]$Message)
    Write-Host ""
    Write-Host "==> $Message" -ForegroundColor Cyan
}

function Require-Command {
    param([string]$Name, [string]$InstallHint)
    if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
        throw "$Name tidak ditemukan. $InstallHint"
    }
}

function Get-RepoName {
    param([string]$RepoId)
    $parts = $RepoId.Split("/")
    return $parts[$parts.Length - 1]
}

Write-Step "Cek dependency dasar"
Require-Command "git" "Install Git for Windows: https://git-scm.com/download/win"
Require-Command "python" "Install Python 3.10+ dan aktifkan opsi Add Python to PATH."

$pythonVersion = (& python --version) 2>&1
Write-Host "Python: $pythonVersion"
Write-Host "Git: $(& git --version)"
if (Get-Command "code" -ErrorAction SilentlyContinue) {
    Write-Host "VS Code CLI: tersedia"
} else {
    Write-Host "VS Code CLI: tidak ditemukan. Install VS Code lalu aktifkan command 'code' dari PATH jika ingin auto-install ekstensi."
}

if ([string]::IsNullOrWhiteSpace($TargetDir)) {
    $TargetDir = Join-Path (Get-Location) (Get-RepoName $RepoId)
}

$TargetDir = [System.IO.Path]::GetFullPath($TargetDir)
$repoUrl = "https://huggingface.co/spaces/$RepoId"

Write-Step "Siapkan autentikasi Hugging Face untuk push"
Write-Host "Token harus punya akses write ke repo/organization $RepoId."
Write-Host "Buat token di: https://huggingface.co/settings/tokens"
$hfUsername = Read-Host "Username Hugging Face"
$hfTokenSecure = Read-Host "Token Hugging Face" -AsSecureString
$hfTokenPtr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($hfTokenSecure)
$hfToken = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($hfTokenPtr)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($hfTokenPtr)

if ([string]::IsNullOrWhiteSpace($hfUsername) -or [string]::IsNullOrWhiteSpace($hfToken)) {
    throw "Username dan token wajib diisi."
}

$authRepoUrl = "https://$hfUsername`:$hfToken@huggingface.co/spaces/$RepoId"

if (Test-Path $TargetDir) {
    Write-Step "Folder repo sudah ada, update dari remote"
    Set-Location $TargetDir
    if (-not (Test-Path ".git")) {
        throw "Folder $TargetDir sudah ada tetapi bukan repository Git."
    }
    git remote set-url origin $authRepoUrl
    git pull origin main
} else {
    Write-Step "Clone repo Space"
    git clone $authRepoUrl $TargetDir
    Set-Location $TargetDir
}

Write-Step "Set identitas Git lokal"
$currentName = (git config user.name) 2>$null
$currentEmail = (git config user.email) 2>$null
if ([string]::IsNullOrWhiteSpace($currentName)) {
    $gitName = Read-Host "Nama Git commit"
    git config user.name $gitName
}
if ([string]::IsNullOrWhiteSpace($currentEmail)) {
    $gitEmail = Read-Host "Email Git commit"
    git config user.email $gitEmail
}

Write-Step "Siapkan virtual environment"
if (-not (Test-Path ".venv")) {
    python -m venv .venv
}

$venvPython = Join-Path $TargetDir ".venv\Scripts\python.exe"
if (-not (Test-Path $venvPython)) {
    throw "Virtual environment gagal dibuat: $venvPython tidak ditemukan."
}

if (-not $SkipInstall) {
    Write-Step "Upgrade pip dan install dependency project"
    & $venvPython -m pip install --upgrade pip
    & $venvPython -m pip install --upgrade setuptools wheel
    & $venvPython -m pip install -r requirements.txt
    & $venvPython -m pip check
} else {
    Write-Host "Skip install dependency karena parameter -SkipInstall dipakai."
}

if ((Get-Command "code" -ErrorAction SilentlyContinue) -and (-not $SkipCodeExtensions)) {
    Write-Step "Install ekstensi VS Code yang dibutuhkan"
    $extensions = @(
        "ms-python.python",
        "ms-python.vscode-pylance",
        "ms-toolsai.jupyter"
    )
    foreach ($extension in $extensions) {
        code --install-extension $extension --force
    }
} elseif ($SkipCodeExtensions) {
    Write-Host "Skip install ekstensi VS Code karena parameter -SkipCodeExtensions dipakai."
}

Write-Step "Validasi syntax aplikasi"
& $venvPython -m py_compile app.py

Write-Step "Siapkan remote push"
git remote set-url origin $authRepoUrl
git status --short

Write-Host ""
Write-Host "Setup selesai." -ForegroundColor Green
Write-Host "Folder repo: $TargetDir"
Write-Host "Jalankan app lokal:"
Write-Host "  .\.venv\Scripts\activate"
Write-Host "  python app.py"
Write-Host ""
Write-Host "Dependency Python yang diinstall dari requirements.txt:"
Write-Host "  gradio, ultralytics, opencv-python-headless, huggingface_hub, numpy, matplotlib, seaborn"
Write-Host "Ekstensi VS Code yang disiapkan:"
Write-Host "  Python, Pylance, Jupyter"
Write-Host ""
Write-Host "Alur push setelah edit:"
Write-Host "  git status"
Write-Host "  git add app.py README.md CHANGELOG.md requirements.txt"
Write-Host "  git commit -m `"update fitur`""
Write-Host "  git push origin main"

if (Get-Command "code" -ErrorAction SilentlyContinue) {
    Write-Step "Buka VS Code"
    code $TargetDir
}