File size: 3,340 Bytes
891fa77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface Health {
  status: 'ready' | 'not_ready'
  device: 'cpu'
  model: { loaded: boolean; assets_available: boolean; reason: string | null; model_id: string; revision: string; head_seed: number }
  limits: { max_upload_bytes: number; max_duration_seconds: number; min_duration_seconds: number; max_queue_jobs: number; job_ttl_seconds: number }
  queue: { active: number; queued: number }
  prediction_task: 'overall_static_valence_arousal'
  calibration: 'none'
}
export interface TimelinePoint {
  index: number; start_seconds: number; end_seconds: number; center_seconds: number; feature_bins: number
  valence: number; arousal: number; display_valence: number; display_arousal: number; out_of_range: boolean
}
export interface PredictionTimeline {
  kind: 'exploratory_static_windows'; window_seconds: 10; hop_seconds: 5
  feature_context: 'reused_full_clip_mert_windows'; validated_dynamic_model: false; smoothed: false
  points: Array<TimelinePoint>; disclaimer: string
}
export interface Prediction {
  timeline?: PredictionTimeline
  valence: number; arousal: number; display_valence: number; display_arousal: number
  out_of_range: boolean; duration_seconds: number; feature_bins: number; processing_seconds: number
  device: 'cpu'; calibration: 'none'; head_seed: number; model_revision: string
}
export interface Job {
  job_id: string
  status: 'queued' | 'running' | 'completed' | 'failed'
  stage: 'queued' | 'decoding' | 'loading_model' | 'extracting_features' | 'predicting' | 'completed' | 'failed'
  progress: number
  result: Prediction | null
  error: string | null
}
export interface Ticket { job_id: string; access_token: string; status: 'queued' }
export const API_BASE = (import.meta.env.VITE_API_BASE_URL || 'http://127.0.0.1:8000').replace(/\/+$/, '')
export const MAX_POLL_MS = 20 * 60 * 1000
export const POLL_INTERVAL_MS = 2000
export type UploadProblem = 'upload.errorM4a' | 'upload.errorEmpty' | 'upload.errorTooLarge'
export function uploadProblem(file: { size: number; name?: string }, maxBytes = 20971520): UploadProblem | null {
  if (file.name && /\.m4a$/i.test(file.name)) return 'upload.errorM4a'
  if (!file.size) return 'upload.errorEmpty'
  if (file.size > maxBytes) return 'upload.errorTooLarge'
  return null
}
export function plotCoordinate(value: number): number { return Math.max(-1, Math.min(1, value)) }
export async function request<T>(path: string, signal: AbortSignal, init: RequestInit = {}): Promise<T> {
  const response = await fetch(`${API_BASE}${path}`, { ...init, signal })
  if (!response.ok) {
    let detail = `HTTP ${response.status}`
    try { const body: unknown = await response.json(); if (body && typeof body === 'object' && 'detail' in body && typeof body.detail === 'string') detail = body.detail } catch { /* Preserve HTTP status when response is not JSON. */ }
    throw new Error(detail)
  }
  return response.json() as Promise<T>
}
export function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error) }
export function jobHeaders(ticket: Ticket): HeadersInit { return { ...uploadHeaders(), 'X-Job-Token': ticket.access_token } }
export function uploadHeaders(): Record<string, string> {
  const token = import.meta.env.VITE_API_AUTH_TOKEN?.trim()
  return token ? { Authorization: `Bearer ${token}` } : {}
}