|
| 1 | +# ============================================================================== |
| 2 | +# Twinkle Client Installation Script for Windows |
| 3 | +# |
| 4 | +# This script sets up a Python environment for using Twinkle client with Tinker. |
| 5 | +# It will: |
| 6 | +# 1. Check if conda is installed; if not, download and install Miniconda |
| 7 | +# 2. Create a new conda environment with Python 3.11 |
| 8 | +# 3. Install twinkle-kit with tinker dependencies |
| 9 | +# |
| 10 | +# Usage (run in PowerShell): |
| 11 | +# .\INSTALL_CLIENT.ps1 [ENV_NAME] |
| 12 | +# |
| 13 | +# Arguments: |
| 14 | +# ENV_NAME - Name of the conda environment (default: twinkle-client) |
| 15 | +# |
| 16 | +# After installation, activate the environment with: |
| 17 | +# conda activate twinkle-client |
| 18 | +# ============================================================================== |
| 19 | + |
| 20 | +param( |
| 21 | + [string]$EnvName = "twinkle-client" |
| 22 | +) |
| 23 | + |
| 24 | +$ErrorActionPreference = "Stop" |
| 25 | +$PythonVersion = "3.11" |
| 26 | +$MinicondaUrl = "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" |
| 27 | + |
| 28 | +Write-Host "==========================================" -ForegroundColor Cyan |
| 29 | +Write-Host "Twinkle Client Installation (Windows)" -ForegroundColor Cyan |
| 30 | +Write-Host "==========================================" -ForegroundColor Cyan |
| 31 | +Write-Host "Environment name: $EnvName" |
| 32 | +Write-Host "Python version: $PythonVersion" |
| 33 | +Write-Host "" |
| 34 | + |
| 35 | +# ============================================================================== |
| 36 | +# Step 1: Check and install Conda |
| 37 | +# ============================================================================== |
| 38 | + |
| 39 | +function Test-CondaInstalled { |
| 40 | + try { |
| 41 | + $condaVersion = conda --version 2>$null |
| 42 | + if ($condaVersion) { |
| 43 | + Write-Host "[OK] Conda is already installed: $condaVersion" -ForegroundColor Green |
| 44 | + return $true |
| 45 | + } |
| 46 | + } catch {} |
| 47 | + |
| 48 | + # Check common installation paths |
| 49 | + $condaPaths = @( |
| 50 | + "$env:USERPROFILE\miniconda3\Scripts\conda.exe", |
| 51 | + "$env:USERPROFILE\anaconda3\Scripts\conda.exe", |
| 52 | + "C:\ProgramData\miniconda3\Scripts\conda.exe", |
| 53 | + "C:\ProgramData\Anaconda3\Scripts\conda.exe" |
| 54 | + ) |
| 55 | + |
| 56 | + foreach ($path in $condaPaths) { |
| 57 | + if (Test-Path $path) { |
| 58 | + $condaDir = Split-Path (Split-Path $path) |
| 59 | + Write-Host "[!] Found conda at: $condaDir" -ForegroundColor Yellow |
| 60 | + Write-Host " Adding to PATH for this session..." |
| 61 | + $env:PATH = "$condaDir\Scripts;$condaDir;$env:PATH" |
| 62 | + return $true |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Write-Host "[!] Conda not found" -ForegroundColor Yellow |
| 67 | + return $false |
| 68 | +} |
| 69 | + |
| 70 | +function Install-Miniconda { |
| 71 | + Write-Host "" |
| 72 | + Write-Host "Installing Miniconda..." -ForegroundColor Cyan |
| 73 | + |
| 74 | + $installerPath = "$env:TEMP\Miniconda3-latest-Windows-x86_64.exe" |
| 75 | + $installDir = "$env:USERPROFILE\miniconda3" |
| 76 | + |
| 77 | + Write-Host "Downloading Miniconda from: $MinicondaUrl" |
| 78 | + |
| 79 | + # Download installer |
| 80 | + try { |
| 81 | + Invoke-WebRequest -Uri $MinicondaUrl -OutFile $installerPath -UseBasicParsing |
| 82 | + } catch { |
| 83 | + Write-Host "[ERROR] Failed to download Miniconda: $_" -ForegroundColor Red |
| 84 | + exit 1 |
| 85 | + } |
| 86 | + |
| 87 | + Write-Host "Installing Miniconda to: $installDir" |
| 88 | + Write-Host "This may take a few minutes..." |
| 89 | + |
| 90 | + # Run installer silently |
| 91 | + Start-Process -FilePath $installerPath -ArgumentList @( |
| 92 | + "/InstallationType=JustMe", |
| 93 | + "/RegisterPython=0", |
| 94 | + "/AddToPath=1", |
| 95 | + "/S", |
| 96 | + "/D=$installDir" |
| 97 | + ) -Wait -NoNewWindow |
| 98 | + |
| 99 | + # Add to PATH for current session |
| 100 | + $env:PATH = "$installDir\Scripts;$installDir;$env:PATH" |
| 101 | + |
| 102 | + # Clean up |
| 103 | + Remove-Item $installerPath -Force -ErrorAction SilentlyContinue |
| 104 | + |
| 105 | + Write-Host "[OK] Miniconda installed successfully" -ForegroundColor Green |
| 106 | + Write-Host "" |
| 107 | + Write-Host "[!] IMPORTANT: Restart PowerShell after installation to use conda globally" -ForegroundColor Yellow |
| 108 | +} |
| 109 | + |
| 110 | +if (-not (Test-CondaInstalled)) { |
| 111 | + $response = Read-Host "Do you want to install Miniconda? [Y/n]" |
| 112 | + if ($response -match "^[Nn]") { |
| 113 | + Write-Host "Installation cancelled. Please install conda manually." |
| 114 | + exit 1 |
| 115 | + } |
| 116 | + Install-Miniconda |
| 117 | +} |
| 118 | + |
| 119 | +# Initialize conda for PowerShell |
| 120 | +try { |
| 121 | + $condaHook = conda shell.powershell hook 2>$null | Out-String |
| 122 | + if ($condaHook) { |
| 123 | + Invoke-Expression $condaHook |
| 124 | + } |
| 125 | +} catch {} |
| 126 | + |
| 127 | +# ============================================================================== |
| 128 | +# Step 2: Create conda environment |
| 129 | +# ============================================================================== |
| 130 | + |
| 131 | +Write-Host "" |
| 132 | +Write-Host "Creating conda environment: $EnvName (Python $PythonVersion)..." -ForegroundColor Cyan |
| 133 | + |
| 134 | +# Accept Conda ToS for default channels (required for conda >= 26.x) |
| 135 | +Write-Host "Accepting Conda Terms of Service for default channels..." |
| 136 | +try { |
| 137 | + conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main 2>$null |
| 138 | + conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r 2>$null |
| 139 | + conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/msys2 2>$null |
| 140 | +} catch { |
| 141 | + # Older conda versions don't have tos command, ignore |
| 142 | +} |
| 143 | + |
| 144 | +# Check if environment already exists |
| 145 | +$envList = conda env list 2>$null |
| 146 | +if ($envList -match "^$EnvName\s") { |
| 147 | + Write-Host "[!] Environment '$EnvName' already exists." -ForegroundColor Yellow |
| 148 | + $response = Read-Host "Do you want to remove and recreate it? [y/N]" |
| 149 | + if ($response -match "^[Yy]") { |
| 150 | + Write-Host "Removing existing environment..." |
| 151 | + conda env remove -n $EnvName -y |
| 152 | + } else { |
| 153 | + Write-Host "Using existing environment..." |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +# Create environment if it doesn't exist |
| 158 | +$envList = conda env list 2>$null |
| 159 | +if (-not ($envList -match "^$EnvName\s")) { |
| 160 | + Write-Host "Running: conda create -n $EnvName python=$PythonVersion -y" |
| 161 | + conda create -n $EnvName python=$PythonVersion -y |
| 162 | + if ($LASTEXITCODE -ne 0) { |
| 163 | + Write-Host "[ERROR] Failed to create conda environment. Exit code: $LASTEXITCODE" -ForegroundColor Red |
| 164 | + exit 1 |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +# Verify environment was created |
| 169 | +$envList = conda env list 2>$null |
| 170 | +if (-not ($envList -match "^$EnvName\s")) { |
| 171 | + Write-Host "[ERROR] Environment '$EnvName' was not created successfully." -ForegroundColor Red |
| 172 | + Write-Host "Please run manually:" -ForegroundColor Yellow |
| 173 | + Write-Host " conda create -n $EnvName python=$PythonVersion" -ForegroundColor Yellow |
| 174 | + exit 1 |
| 175 | +} |
| 176 | + |
| 177 | +Write-Host "[OK] Environment '$EnvName' is ready" -ForegroundColor Green |
| 178 | + |
| 179 | +# ============================================================================== |
| 180 | +# Step 3: Install dependencies |
| 181 | +# ============================================================================== |
| 182 | + |
| 183 | +Write-Host "" |
| 184 | +Write-Host "Activating environment and installing dependencies..." -ForegroundColor Cyan |
| 185 | + |
| 186 | +# Activate environment |
| 187 | +Write-Host "Activating environment '$EnvName'..." |
| 188 | +try { |
| 189 | + conda activate $EnvName |
| 190 | + if ($LASTEXITCODE -ne 0) { |
| 191 | + throw "conda activate failed" |
| 192 | + } |
| 193 | +} catch { |
| 194 | + Write-Host "[!] Standard activation failed, trying alternative method..." -ForegroundColor Yellow |
| 195 | + # Alternative: run commands in the conda environment directly |
| 196 | + $condaBase = (conda info --base 2>$null).Trim() |
| 197 | + $activateScript = Join-Path $condaBase "Scripts\activate.bat" |
| 198 | + if (Test-Path $activateScript) { |
| 199 | + cmd /c "call `"$activateScript`" $EnvName && pip --version" 2>$null |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +# Verify we're in the correct environment |
| 204 | +$currentPython = python --version 2>&1 |
| 205 | +Write-Host "Current Python: $currentPython" |
| 206 | +if ($currentPython -notmatch "3\.11") { |
| 207 | + Write-Host "[!] Warning: Python version mismatch. Expected 3.11, got: $currentPython" -ForegroundColor Yellow |
| 208 | + Write-Host "Attempting to use conda run instead..." -ForegroundColor Yellow |
| 209 | + $UseCondaRun = $true |
| 210 | +} else { |
| 211 | + $UseCondaRun = $false |
| 212 | +} |
| 213 | + |
| 214 | +# Upgrade pip |
| 215 | +if ($UseCondaRun) { |
| 216 | + conda run -n $EnvName pip install --upgrade pip |
| 217 | +} else { |
| 218 | + pip install --upgrade pip |
| 219 | +} |
| 220 | + |
| 221 | +# Check if we're in the twinkle source directory |
| 222 | +$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 223 | + |
| 224 | +if (Test-Path "$ScriptDir\pyproject.toml") { |
| 225 | + Write-Host "" |
| 226 | + Write-Host "Installing twinkle from source (with tinker support)..." -ForegroundColor Cyan |
| 227 | + if ($UseCondaRun) { |
| 228 | + conda run -n $EnvName pip install -e "$ScriptDir[tinker]" |
| 229 | + } else { |
| 230 | + pip install -e "$ScriptDir[tinker]" |
| 231 | + } |
| 232 | +} else { |
| 233 | + Write-Host "" |
| 234 | + Write-Host "Installing twinkle-kit from PyPI (with tinker support)..." -ForegroundColor Cyan |
| 235 | + if ($UseCondaRun) { |
| 236 | + conda run -n $EnvName pip install "twinkle-kit[tinker]" |
| 237 | + } else { |
| 238 | + pip install "twinkle-kit[tinker]" |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +if ($LASTEXITCODE -ne 0) { |
| 243 | + Write-Host "[ERROR] Failed to install twinkle. Exit code: $LASTEXITCODE" -ForegroundColor Red |
| 244 | + exit 1 |
| 245 | +} |
| 246 | + |
| 247 | +# ============================================================================== |
| 248 | +# Step 4: Verify installation |
| 249 | +# ============================================================================== |
| 250 | + |
| 251 | +Write-Host "" |
| 252 | +Write-Host "Verifying installation..." -ForegroundColor Cyan |
| 253 | +Write-Host "" |
| 254 | + |
| 255 | +$verifyScript = @" |
| 256 | +import sys |
| 257 | +print(f'Python: {sys.version}') |
| 258 | +print() |
| 259 | +
|
| 260 | +packages = [ |
| 261 | + 'twinkle', |
| 262 | + 'twinkle_client', |
| 263 | + 'tinker', |
| 264 | + 'transformers', |
| 265 | + 'peft', |
| 266 | + 'modelscope', |
| 267 | + 'datasets', |
| 268 | +] |
| 269 | +
|
| 270 | +print('Installed packages:') |
| 271 | +print('-' * 40) |
| 272 | +
|
| 273 | +for pkg in packages: |
| 274 | + try: |
| 275 | + mod = __import__(pkg) |
| 276 | + version = getattr(mod, '__version__', 'unknown') |
| 277 | + print(f' {pkg}: {version}') |
| 278 | + except ImportError as e: |
| 279 | + print(f' {pkg}: NOT INSTALLED ({e})') |
| 280 | +
|
| 281 | +print() |
| 282 | +print('Testing twinkle client imports...') |
| 283 | +try: |
| 284 | + from twinkle_client import init_tinker_client |
| 285 | + print(' [OK] init_tinker_client available') |
| 286 | +except ImportError as e: |
| 287 | + print(f' [FAIL] init_tinker_client: {e}') |
| 288 | +
|
| 289 | +try: |
| 290 | + from twinkle.dataloader import DataLoader |
| 291 | + from twinkle.dataset import Dataset, DatasetMeta |
| 292 | + from twinkle.preprocessor import SelfCognitionProcessor |
| 293 | + print(' [OK] twinkle core components available') |
| 294 | +except ImportError as e: |
| 295 | + print(f' [FAIL] twinkle core: {e}') |
| 296 | +"@ |
| 297 | + |
| 298 | +if ($UseCondaRun) { |
| 299 | + conda run -n $EnvName python -c $verifyScript |
| 300 | +} else { |
| 301 | + python -c $verifyScript |
| 302 | +} |
| 303 | + |
| 304 | +Write-Host "" |
| 305 | +Write-Host "==========================================" -ForegroundColor Cyan |
| 306 | +Write-Host "Installation complete!" -ForegroundColor Green |
| 307 | +Write-Host "==========================================" -ForegroundColor Cyan |
| 308 | +Write-Host "" |
| 309 | +Write-Host "To activate the environment, run:" |
| 310 | +Write-Host " conda activate $EnvName" -ForegroundColor Yellow |
| 311 | +Write-Host "" |
| 312 | +Write-Host "Example usage (see cookbook/client/tinker/):" |
| 313 | +Write-Host ' $env:MODELSCOPE_TOKEN = "your-token"' -ForegroundColor Yellow |
| 314 | +Write-Host " python cookbook/client/tinker/modelscope_service/self_cognition.py" -ForegroundColor Yellow |
| 315 | +Write-Host "" |
0 commit comments