All files / web/src/app/api/vision-training config.ts

0% Statements 0/361
0% Branches 0/1
0% Functions 0/1
0% Lines 0/361

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { exec, execSync, spawn } from 'child_process'
import fs from 'fs'
import path from 'path'
import { promisify } from 'util'

const execAsync = promisify(exec)

/**
 * Shared configuration for vision training.
 *
 * CRITICAL: Both hardware detection and training MUST use the same Python
 * environment so that hardware detection accurately reflects what training
 * will actually use.
 */

const cwd = process.cwd()

/**
 * Check if the current platform supports TensorFlow training.
 * TensorFlow has wheels for:
 * - macOS x86_64 and arm64 (Apple Silicon with tensorflow-macos)
 * - Linux x86_64 and aarch64
 * - Windows x86_64
 */
export function isPlatformSupported(): { supported: boolean; reason?: string } {
  const platform = process.platform
  const arch = process.arch

  if (platform === 'darwin') {
    // macOS - both Intel and Apple Silicon are supported
    return { supported: true }
  }

  if (platform === 'linux') {
    if (arch === 'x64' || arch === 'arm64') {
      return { supported: true }
    }
    return {
      supported: false,
      reason: `TensorFlow is not available for Linux ${arch}. Training requires x86_64 or ARM64.`,
    }
  }

  if (platform === 'win32' && arch === 'x64') {
    return { supported: true }
  }

  return {
    supported: false,
    reason: `TensorFlow is not available for ${platform} ${arch}. Training should be done on macOS, Linux (x86_64/ARM64), or Windows x86_64.`,
  }
}

/**
 * Path to the training scripts directory
 */
export const TRAINING_SCRIPTS_DIR = path.join(cwd, 'scripts/train-column-classifier')

/**
 * Path to the venv directory
 * We use data/vision-training/.venv because:
 * 1. The data/ directory is mounted as a volume in Docker (persists across restarts)
 * 2. It's writable by the container
 * 3. Scripts directory may not exist in production Docker images
 */
const VENV_DIR = path.join(cwd, 'data/vision-training/.venv')

/**
 * Path to the Python executable in the venv
 */
export const TRAINING_PYTHON = path.join(VENV_DIR, 'bin/python')

/**
 * Common environment variables for Python subprocesses
 */
export const PYTHON_ENV = {
  ...process.env,
  PYTHONUNBUFFERED: '1',
  PYTHONWARNINGS: 'ignore::FutureWarning', // Suppress keras warning
}

/**
 * Setup state - cached to avoid repeated setup attempts
 */
let setupPromise: Promise<SetupResult> | null = null

interface SetupResult {
  success: boolean
  python: string
  error?: string
  isAppleSilicon: boolean
  hasGpu: boolean
}

/**
 * Required Python modules for training
 */
const REQUIRED_MODULES = [
  { name: 'tensorflow', importName: 'tensorflow', pipName: 'tensorflow' },
  { name: 'tensorflowjs', importName: 'tensorflowjs', pipName: 'tensorflowjs' },
  { name: 'PIL (Pillow)', importName: 'PIL', pipName: 'Pillow' },
  {
    name: 'sklearn (scikit-learn)',
    importName: 'sklearn',
    pipName: 'scikit-learn',
  },
  { name: 'numpy', importName: 'numpy', pipName: 'numpy' },
  { name: 'OpenCV', importName: 'cv2', pipName: 'opencv-python-headless' },
]

export interface DependencyCheckResult {
  allInstalled: boolean
  missing: { name: string; pipName: string }[]
  installed: { name: string; pipName: string }[]
  error?: string
}

/**
 * Check if all required Python dependencies are installed
 */
export async function checkDependencies(): Promise<DependencyCheckResult> {
  if (!fs.existsSync(TRAINING_PYTHON)) {
    return {
      allInstalled: false,
      missing: REQUIRED_MODULES.map((m) => ({
        name: m.name,
        pipName: m.pipName,
      })),
      installed: [],
      error: 'Python virtual environment not found',
    }
  }

  const missing: { name: string; pipName: string }[] = []
  const installed: { name: string; pipName: string }[] = []

  for (const mod of REQUIRED_MODULES) {
    try {
      await execAsync(`"${TRAINING_PYTHON}" -c "import ${mod.importName}"`, {
        timeout: 10000,
      })
      installed.push({ name: mod.name, pipName: mod.pipName })
    } catch {
      missing.push({ name: mod.name, pipName: mod.pipName })
    }
  }

  return {
    allInstalled: missing.length === 0,
    missing,
    installed,
  }
}

/**
 * Find the best Python to use for the venv.
 * On Apple Silicon, prefer Homebrew ARM Python for Metal GPU support.
 */
function findBestPython(): string {
  const isAppleSilicon = process.platform === 'darwin' && process.arch === 'arm64'

  if (isAppleSilicon) {
    // Try Homebrew Python versions (ARM native)
    const homebrewPythons = [
      '/opt/homebrew/opt/python@3.11/bin/python3.11',
      '/opt/homebrew/opt/python@3.12/bin/python3.12',
      '/opt/homebrew/bin/python3',
    ]

    for (const python of homebrewPythons) {
      if (fs.existsSync(python)) {
        // Verify it's actually ARM
        try {
          const result = execSync(`file "${python}"`, { encoding: 'utf-8' })
          if (result.includes('arm64')) {
            return python
          }
        } catch {
          // Continue to next option
        }
      }
    }
  }

  // Fall back to system python3
  return 'python3'
}

/**
 * Check if the venv is already set up with tensorflow
 */
async function isVenvReady(): Promise<boolean> {
  if (!fs.existsSync(TRAINING_PYTHON)) {
    return false
  }

  try {
    // Check if tensorflow is installed
    await execAsync(`"${TRAINING_PYTHON}" -c "import tensorflow"`, {
      timeout: 30000,
    })
    return true
  } catch {
    return false
  }
}

/**
 * Path to the requirements.txt file
 */
const REQUIREMENTS_FILE = path.join(TRAINING_SCRIPTS_DIR, 'requirements.txt')

/**
 * Create the venv and install dependencies
 */
async function createVenv(): Promise<SetupResult> {
  const isAppleSilicon = process.platform === 'darwin' && process.arch === 'arm64'

  const basePython = findBestPython()
  console.log(`[vision-training] Creating venv with ${basePython}...`)

  try {
    // Create venv (--clear removes existing incomplete venv)
    await execAsync(`"${basePython}" -m venv --clear "${VENV_DIR}"`, {
      timeout: 60000,
    })

    // Upgrade pip
    await execAsync(`"${TRAINING_PYTHON}" -m pip install --upgrade pip`, {
      timeout: 120000,
    })

    // Install tensorflow and tensorflowjs together to get compatible versions
    // Note: We skip tensorflow-metal because tensorflowjs requires a newer tensorflow
    // version that's incompatible with the current Metal plugin. CPU training is
    // fast enough for our small dataset (~500 images).
    console.log('[vision-training] Installing tensorflow and tensorflowjs...')
    await execAsync(
      `"${TRAINING_PYTHON}" -m pip install tensorflow tensorflowjs`,
      { timeout: 600000 } // 10 minutes - tensorflow is large
    )

    // Install all other requirements from requirements.txt
    if (fs.existsSync(REQUIREMENTS_FILE)) {
      console.log('[vision-training] Installing additional requirements from requirements.txt...')
      await execAsync(`"${TRAINING_PYTHON}" -m pip install -r "${REQUIREMENTS_FILE}"`, {
        timeout: 600000,
      })
    } else {
      // Fallback: install known required packages individually
      // Note: tensorflow and tensorflowjs are already installed above
      console.log(
        '[vision-training] requirements.txt not found, installing packages individually...'
      )
      const packages = ['Pillow', 'scikit-learn', 'numpy', 'opencv-python-headless']
      for (const pkg of packages) {
        try {
          await execAsync(`"${TRAINING_PYTHON}" -m pip install "${pkg}"`, {
            timeout: 300000,
          })
        } catch (e) {
          console.warn(`[vision-training] Failed to install ${pkg}: ${e}`)
        }
      }
    }

    // Verify installation
    const { stdout } = await execAsync(
      `"${TRAINING_PYTHON}" -c "import tensorflow as tf; print(len(tf.config.list_physical_devices('GPU')))"`,
      { timeout: 60000 }
    )
    const gpuCount = parseInt(stdout.trim(), 10) || 0

    console.log(`[vision-training] Setup complete. GPUs detected: ${gpuCount}`)

    return {
      success: true,
      python: TRAINING_PYTHON,
      isAppleSilicon,
      hasGpu: gpuCount > 0,
    }
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error)
    console.error(`[vision-training] Setup failed: ${message}`)

    return {
      success: false,
      python: TRAINING_PYTHON,
      error: message,
      isAppleSilicon,
      hasGpu: false,
    }
  }
}

/**
 * Ensure the Python venv is set up with tensorflow.
 * Call this before spawning any Python processes.
 *
 * This is lazy and cached - first call does setup, subsequent calls return immediately.
 */
export async function ensureVenvReady(): Promise<SetupResult> {
  // Return cached promise if setup already in progress or complete
  if (setupPromise) {
    return setupPromise
  }

  // Check if already set up
  if (await isVenvReady()) {
    const isAppleSilicon = process.platform === 'darwin' && process.arch === 'arm64'

    // Check for and install any missing dependencies
    const depCheck = await checkDependencies()
    if (!depCheck.allInstalled && depCheck.missing.length > 0) {
      console.log(
        `[vision-training] Installing missing dependencies: ${depCheck.missing.map((m) => m.pipName).join(', ')}`
      )
      for (const dep of depCheck.missing) {
        try {
          await execAsync(`"${TRAINING_PYTHON}" -m pip install "${dep.pipName}"`, {
            timeout: 300000,
          })
          console.log(`[vision-training] Installed ${dep.pipName}`)
        } catch (e) {
          console.warn(`[vision-training] Failed to install ${dep.pipName}: ${e}`)
        }
      }
    }

    // Quick GPU check
    let hasGpu = false
    try {
      const { stdout } = await execAsync(
        `"${TRAINING_PYTHON}" -c "import tensorflow as tf; print(len(tf.config.list_physical_devices('GPU')))"`,
        { timeout: 30000 }
      )
      hasGpu = parseInt(stdout.trim(), 10) > 0
    } catch {
      // Ignore
    }

    setupPromise = Promise.resolve({
      success: true,
      python: TRAINING_PYTHON,
      isAppleSilicon,
      hasGpu,
    })
    return setupPromise
  }

  // Need to set up - do it once
  console.log('[vision-training] Venv not found, setting up...')
  setupPromise = createVenv().then((result) => {
    // If setup failed, clear cache so we can retry next time
    if (!result.success) {
      setupPromise = null
    }
    return result
  })
  return setupPromise
}