from __future__ import annotations

import json
import subprocess
import tempfile
from pathlib import Path

_LAM_DIR = Path("/mnt/DATA/WORK/LAM_Audio2Expression")
_CONDA = Path.home() / "miniconda3/bin/conda"
_SCRIPT = _LAM_DIR / "run_inference.py"


def audio_to_blendshapes(audio_path: Path) -> dict:
    with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as fh:
        out = Path(fh.name)
    try:
        subprocess.run(
            [
                str(_CONDA), "run", "--no-capture-output", "-n", "lam_a2e",
                "python", str(_SCRIPT),
                "--audio", str(audio_path),
                "--output", str(out),
            ],
            check=True,
            cwd=str(_LAM_DIR),
            timeout=300,
        )
        return json.loads(out.read_text())
    finally:
        out.unlink(missing_ok=True)
