All files / web/src/app/vision-training/train/components/wizard TrainingWizard.tsx

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

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 363 364 365 366                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import { css } from '../../../../../../styled-system/css'
import { PhaseSection } from './PhaseSection'
import {
  PHASES,
  serverPhaseToWizardPosition,
  isColumnClassifierDatasetInfo,
  isBoundaryDetectorDatasetInfo,
  isBoundaryDetectorResult,
  type ModelType,
  type SamplesData,
  type HardwareInfo,
  type PreflightInfo,
  type TrainingConfig,
  type ServerPhase,
  type EpochData,
  type DatasetInfo,
  type LoadingProgress,
  type TrainingResult,
  type PhaseStatus,
} from './types'

// localStorage keys for persistence
const STORAGE_KEY_POSITION = 'vision-training-wizard-position'

// Position state saved to localStorage
// Note: modelType is no longer saved - it's determined by the URL path
interface WizardPositionState {
  currentPhaseIndex: number
  currentCardIndex: number
  dataWarningAcknowledged: boolean
}

interface TrainingWizardProps {
  // Model type (determined by URL path, not user selection)
  modelType: ModelType
  // Data state
  samples: SamplesData | null
  samplesLoading: boolean
  // Hardware state
  hardwareInfo: HardwareInfo | null
  hardwareLoading: boolean
  fetchHardware: () => void
  // Preflight state
  preflightInfo: PreflightInfo | null
  preflightLoading: boolean
  fetchPreflight: () => void
  // Config state
  config: TrainingConfig
  setConfig: (config: TrainingConfig | ((prev: TrainingConfig) => TrainingConfig)) => void
  // Training state (from server)
  serverPhase: ServerPhase
  statusMessage: string
  currentEpoch: EpochData | null
  epochHistory: EpochData[]
  datasetInfo: DatasetInfo | null
  loadingProgress: LoadingProgress | null
  result: TrainingResult | null
  error: string | null
  // Actions
  onStart: () => void
  onCancel: () => void
  onStopAndSave?: () => void
  onReset: () => void
  onSyncComplete?: () => void
  // Called to re-run training with same config (from results)
  onRerunTraining?: () => void
}

export function TrainingWizard({
  modelType,
  samples,
  samplesLoading,
  hardwareInfo,
  hardwareLoading,
  fetchHardware,
  preflightInfo,
  preflightLoading,
  fetchPreflight,
  config,
  setConfig,
  serverPhase,
  statusMessage,
  currentEpoch,
  epochHistory,
  datasetInfo,
  loadingProgress,
  result,
  error,
  onStart,
  onCancel,
  onStopAndSave,
  onReset,
  onSyncComplete,
  onRerunTraining,
}: TrainingWizardProps) {
  // Wizard position state
  const [currentPhaseIndex, setCurrentPhaseIndex] = useState(0)
  const [currentCardIndex, setCurrentCardIndex] = useState(0)
  // Track if user explicitly bypassed insufficient data warning
  const [dataWarningAcknowledged, setDataWarningAcknowledged] = useState(false)
  // Track if we've initialized from localStorage
  const initializedRef = useRef(false)

  // Load position state from localStorage on mount
  // Note: modelType is no longer restored from localStorage - it comes from the URL
  useEffect(() => {
    if (initializedRef.current) return
    initializedRef.current = true

    try {
      const saved = localStorage.getItem(STORAGE_KEY_POSITION)
      if (saved) {
        const state = JSON.parse(saved) as WizardPositionState
        // Only restore if we're not actively training
        if (serverPhase === 'idle') {
          setCurrentPhaseIndex(state.currentPhaseIndex)
          setCurrentCardIndex(state.currentCardIndex)
          setDataWarningAcknowledged(state.dataWarningAcknowledged)
        }
      }
    } catch {
      // Ignore localStorage errors
    }
  }, [serverPhase])

  // Save position state to localStorage when it changes
  // Note: modelType is not saved - it comes from the URL
  useEffect(() => {
    // Don't save until we've initialized
    if (!initializedRef.current) return
    // Don't save during active training (let server phase drive position)
    if (serverPhase !== 'idle' && serverPhase !== 'complete' && serverPhase !== 'error') return

    const state: WizardPositionState = {
      currentPhaseIndex,
      currentCardIndex,
      dataWarningAcknowledged,
    }
    try {
      localStorage.setItem(STORAGE_KEY_POSITION, JSON.stringify(state))
    } catch {
      // Ignore localStorage errors
    }
  }, [currentPhaseIndex, currentCardIndex, dataWarningAcknowledged, serverPhase])

  // Derive state
  const isGpu = hardwareInfo?.deviceType === 'gpu'
  const bestAccuracy =
    epochHistory.length > 0 ? Math.max(...epochHistory.map((e) => e.val_accuracy)) : 0
  // Best pixel error (lower is better) - for boundary detector
  const pixelErrors = epochHistory.map((e) => e.val_pixel_error).filter((e) => e !== undefined)
  const bestPixelError = pixelErrors.length > 0 ? Math.min(...pixelErrors) : null
  const hasEnoughData =
    samples?.hasData && samples.dataQuality !== 'none' && samples.dataQuality !== 'insufficient'
  // Allow training if data is sufficient OR user acknowledged the warning
  const canProceedWithData = hasEnoughData || (dataWarningAcknowledged && samples?.hasData)

  // Sync wizard position with server phase during training
  useEffect(() => {
    if (serverPhase !== 'idle') {
      const { phaseIndex, cardIndex } = serverPhaseToWizardPosition(serverPhase)
      setCurrentPhaseIndex(phaseIndex)
      setCurrentCardIndex(cardIndex)
    }
  }, [serverPhase])

  // Progress to next card
  const progressToNextCard = useCallback(() => {
    const currentPhase = PHASES[currentPhaseIndex]

    if (currentCardIndex < currentPhase.cards.length - 1) {
      // More cards in this phase
      setCurrentCardIndex((prev) => prev + 1)
    } else if (currentPhaseIndex < PHASES.length - 1) {
      // Move to next phase
      setCurrentPhaseIndex((prev) => prev + 1)
      setCurrentCardIndex(0)
    }
  }, [currentPhaseIndex, currentCardIndex])

  // Handle starting training (transition from config to training phase)
  const handleStartTraining = useCallback(() => {
    // Move to training phase
    setCurrentPhaseIndex(1)
    setCurrentCardIndex(0)
    // Trigger actual training
    onStart()
  }, [onStart])

  // Handle train again (reset to preparation)
  const handleTrainAgain = useCallback(() => {
    setCurrentPhaseIndex(0)
    setCurrentCardIndex(0)
    setDataWarningAcknowledged(false)
    // Clear localStorage when fully resetting
    try {
      localStorage.removeItem(STORAGE_KEY_POSITION)
    } catch {
      // Ignore
    }
    onReset()
  }, [onReset])

  // Handle data warning acknowledgment (user clicked "Continue Anyway")
  const handleDataWarningAcknowledged = useCallback(() => {
    setDataWarningAcknowledged(true)
  }, [])

  // Navigate to a specific card (for rewinding to prior cards)
  const goToCard = useCallback(
    (phaseIndex: number, cardIndex: number) => {
      // Only allow going backwards or to current position
      if (
        phaseIndex < currentPhaseIndex ||
        (phaseIndex === currentPhaseIndex && cardIndex <= currentCardIndex)
      ) {
        setCurrentPhaseIndex(phaseIndex)
        setCurrentCardIndex(cardIndex)
      }
    },
    [currentPhaseIndex, currentCardIndex]
  )

  // Get phase status based on current position
  const getPhaseStatus = (phaseIndex: number): PhaseStatus => {
    if (phaseIndex < currentPhaseIndex) return 'done'
    if (phaseIndex === currentPhaseIndex) return 'current'
    return 'upcoming'
  }

  // Card summaries for done states
  const getCardSummary = (cardId: string): { label: string; value: string } | null => {
    switch (cardId) {
      case 'data': {
        if (!samples?.hasData) return null
        const count =
          samples.type === 'column-classifier' ? samples.totalImages : samples.totalFrames
        return { label: 'Samples', value: `${count}` }
      }
      case 'hardware':
        if (!hardwareInfo) return null
        return {
          label: hardwareInfo.deviceType === 'gpu' ? 'GPU' : 'CPU',
          value: hardwareInfo.deviceName.split(' ').slice(0, 2).join(' '),
        }
      case 'dependencies':
        if (!preflightInfo?.ready) return null
        return {
          label: 'Packages',
          value: `${preflightInfo.dependencies.installed.length}`,
        }
      case 'config':
        return { label: 'Epochs', value: `${config.epochs}` }
      case 'setup':
        return { label: 'Ready', value: '✓' }
      case 'loading':
        if (!datasetInfo) return { label: 'Loaded', value: '✓' }
        if (isColumnClassifierDatasetInfo(datasetInfo)) {
          return { label: 'Loaded', value: `${datasetInfo.total_images}` }
        }
        if (isBoundaryDetectorDatasetInfo(datasetInfo)) {
          return { label: 'Loaded', value: `${datasetInfo.total_frames}` }
        }
        return { label: 'Loaded', value: '✓' }
      case 'training':
        // Show pixel error for boundary detector, accuracy for column classifier
        if (modelType === 'boundary-detector' && bestPixelError !== null) {
          return {
            label: 'Error',
            value: `${bestPixelError.toFixed(0)}px`,
          }
        }
        return {
          label: 'Accuracy',
          value: `${(bestAccuracy * 100).toFixed(0)}%`,
        }
      case 'export':
        return { label: 'Exported', value: '✓' }
      case 'results':
        if (!result) return null
        // Show pixel error for boundary detector, accuracy for column classifier
        if (isBoundaryDetectorResult(result) && result.final_pixel_error !== undefined) {
          return {
            label: 'Final',
            value: `${result.final_pixel_error.toFixed(0)}px`,
          }
        }
        return {
          label: 'Final',
          value: `${(result.final_accuracy * 100).toFixed(1)}%`,
        }
      default:
        return null
    }
  }

  return (
    <div
      data-component="training-wizard"
      className={css({
        display: 'flex',
        flexDirection: 'column',
        gap: 4,
      })}
    >
      {PHASES.map((phase, phaseIndex) => (
        <PhaseSection
          key={phase.id}
          phase={phase}
          phaseIndex={phaseIndex}
          status={getPhaseStatus(phaseIndex)}
          currentCardIndex={phaseIndex === currentPhaseIndex ? currentCardIndex : -1}
          onGoToCard={goToCard}
          // Model type (from URL)
          modelType={modelType}
          // Data for cards
          samples={samples}
          samplesLoading={samplesLoading}
          hardwareInfo={hardwareInfo}
          hardwareLoading={hardwareLoading}
          fetchHardware={fetchHardware}
          preflightInfo={preflightInfo}
          preflightLoading={preflightLoading}
          fetchPreflight={fetchPreflight}
          config={config}
          setConfig={setConfig}
          isGpu={isGpu}
          // Training data
          serverPhase={serverPhase}
          statusMessage={statusMessage}
          currentEpoch={currentEpoch}
          epochHistory={epochHistory}
          bestAccuracy={bestAccuracy}
          bestPixelError={bestPixelError}
          datasetInfo={datasetInfo}
          loadingProgress={loadingProgress}
          result={result}
          error={error}
          // Summaries
          getCardSummary={getCardSummary}
          // Actions
          onProgress={progressToNextCard}
          onStartTraining={handleStartTraining}
          onCancel={onCancel}
          onStopAndSave={onStopAndSave}
          onTrainAgain={handleTrainAgain}
          onRerunTraining={onRerunTraining}
          onSyncComplete={onSyncComplete}
          onDataWarningAcknowledged={handleDataWarningAcknowledged}
          // Validation - require data, hardware, and dependencies all ready
          // Note: modelType is always defined (comes from URL)
          canStartTraining={
            !!canProceedWithData &&
            !hardwareLoading &&
            !hardwareInfo?.error &&
            !!preflightInfo?.ready
          }
        />
      ))}
    </div>
  )
}