@echo off
rem Emberia's realistic voice on this computer. Double-click: the first time it installs (about 6 GB, once), then it starts.
rem Needs Windows, an NVIDIA graphics card with 8 GB or more, and Python 3.11 from python.org.
rem No labels or goto: the site serves this file with plain line ends, which labels do not survive.
setlocal
set "HOME_DIR=%LOCALAPPDATA%\EmberiaVoice"
set "SITE=https://emberia.pages.dev/downloads/emberia-voice"
set "FAILED=echo Something went wrong while installing. Check the internet connection and try again. & pause & exit /b 1"

if not exist "%HOME_DIR%\.venv\Scripts\python.exe" (
  py -3.11 --version >nul 2>&1 || (
    echo Python 3.11 is needed. Install it from python.org, then double-click this file again.
    start "" https://www.python.org/downloads/release/python-3119/
    pause
    exit /b 1
  )
  nvidia-smi >nul 2>&1 || (
    echo No NVIDIA graphics card was found. The realistic voice needs one. The site keeps using the browser voice.
    pause
    exit /b 1
  )
  echo Installing Emberia's voice. This happens once and takes a while.
  mkdir "%HOME_DIR%" 2>nul
  curl -fsSL -o "%HOME_DIR%\server.py" "%SITE%/server.py" || (%FAILED%)
  curl -fsSL -o "%HOME_DIR%\voice.wav" "%SITE%/voice.wav" || (%FAILED%)
  curl -fsSL -o "%HOME_DIR%\phonikud-1.0.int8.onnx" "https://huggingface.co/thewh1teagle/phonikud-onnx/resolve/main/phonikud-1.0.int8.onnx" || (%FAILED%)
  py -3.11 -m venv "%HOME_DIR%\.venv" || (%FAILED%)
  "%HOME_DIR%\.venv\Scripts\python" -m pip install --upgrade pip >nul
  "%HOME_DIR%\.venv\Scripts\python" -m pip install torch==2.6.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu124 || (%FAILED%)
  "%HOME_DIR%\.venv\Scripts\python" -m pip install chatterbox-tts phonikud phonikud-onnx fastapi uvicorn || (%FAILED%)
)

echo Starting Emberia's voice. The first start downloads the voice models.
cd /d "%HOME_DIR%"
"%HOME_DIR%\.venv\Scripts\python" -I server.py
pause
