import { spawnSync } from "node:child_process"; import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; const envPath = join(process.cwd(), ".env.local"); if (existsSync(envPath)) { for (const line of readFileSync(envPath, "utf8").split("\n")) { const match = line.match(/^([A-Z0-9_]+)=(.*)$/); if (match && !process.env[match[1]]) { process.env[match[1]] = match[2]; } } } const localPython = join(process.cwd(), ".venv", "bin", "python"); const python = process.env.SAM_PYTHON || (existsSync(localPython) ? localPython : "python3"); const checkpoint = process.env.SAM_CHECKPOINT; const result = spawnSync( python, [ "-c", "import torch, segment_anything; print('torch=' + torch.__version__); print('segment_anything=ok')" ], { encoding: "utf8" } ); if (result.status !== 0) { console.error("SAM Python dependencies are not installed for:", python); console.error(result.stderr || result.stdout); process.exit(1); } if (!checkpoint) { console.error("SAM_CHECKPOINT is not set."); process.exit(1); } if (!existsSync(checkpoint)) { console.error(`SAM_CHECKPOINT does not exist: ${checkpoint}`); process.exit(1); } console.log(result.stdout.trim()); console.log(`checkpoint=${checkpoint}`); console.log(`modelType=${process.env.SAM_MODEL_TYPE || "vit_h"}`);