#!/usr/bin/env bash
# Real demos for P189 "CPU vs GPU for LLM Inference: What Actually Changes".
# Jess's roofing company answers customer texts with Llama 3.1 8B. The lab is one rented Google Colab GPU runtime
# (an NVIDIA L4 and an Intel Xeon with 12 CPU threads), driven from the spare Mac with the colab CLI, whose sign-in
# lives in that Mac's GUI login (so lab/colab_run.command is started with `open -a Terminal`). On the VM,
# lab/colab_lab.py installs Ollama, pulls llama3.1:8b, makes the user jess with ~/helper (company.md, ask, crowd)
# and runs every step as jess; the CPU and GPU runs use the same model file on the same VM (ask --cpu keeps every
# layer off the GPU with Ollama's num_gpu 0). Every line in video.json is read from demo_output.json.
set -euo pipefail
cd "$(dirname "$0")"
MAC=${SPARE_MAC:-192.168.1.182}
ssh "$MAC" 'mkdir -p ~/p189-lab && rm -f ~/p189-lab/out.tar'
scp -q lab/company.md lab/ask.py lab/crowd.py lab/colab_lab.py lab/colab_mkdir.py lab/colab_start.py lab/colab_poll.py \
    lab/colab_run.command "$MAC":p189-lab/
ssh "$MAC" 'open -a Terminal ~/p189-lab/colab_run.command'
until ssh "$MAC" 'tail -1 ~/p189-lab/colab_run.log | grep -q "=== done"'; do sleep 30; done
scp -q "$MAC":p189-lab/out.tar lab/out.tar
tar -xf lab/out.tar -C lab
cp lab/out/demo_output.json demo_output.json
echo "demo_output.json: $(python3 -c 'import json;print(len(json.load(open("demo_output.json"))))') steps"
