diff --git a/README.md b/README.md index ca515c2..75e1b94 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,11 @@ The agent is built from parts you can buy and solder: - **The box:** a Raspberry Pi 3B with a Google AIY Voice Kit v1 (speaker, two mics, the button), running [NixOS](box/module.nix). - **The IR emitter:** a Raspberry Pi Pico H with an IR transceiver ([wiring](ir-pico/wiring.md)), on a USB cord from the box, aimed at the equipment. - **The camera:** an RTSP camera on the LAN, aimed at the TV. +- **Home server:** any Linux machine with Nix and systemd, for speech and the brain; x86_64 with AVX2 is plenty. whisper.cpp's base.en model transcribes an 11 s clip in about 2 s. + +## Make your own + +You'll need the parts above, a Linux home server with Nix, and an afternoon. You choose a few names: the box's, your home server's, your Wi-Fi network's. Everything else has a default. The [setup guide](docs/setup.md) walks through it. ## License diff --git a/box/Makefile b/box/Makefile index 0f0b706..3c916fa 100644 --- a/box/Makefile +++ b/box/Makefile @@ -7,6 +7,8 @@ # make card DEV=/dev/sdX # look first # make flash DEV=/dev/sdX IMG=.../sd-image/x.img CONFIRM=erase # then write # make wifi-secret SSID=... +# Then, on the home server, as the brain's user, +# with speech and the brain installed: make keys. BOX = box ADMIN = $(USER) @@ -19,8 +21,16 @@ TEXT = Hello. Press my button and talk to me. AMP = 100 VOL = 30% SECS = 6 +# The keys: each connection's, at both its ends. The brain's copies, and the camera's URL (its password +# in it: kept outside the brain's sandbox), here; speech's key, root's, here too. +BRAIN_KEYS = $(HOME)/.config/pdtv26-brain +CAMERA_URL = $(HOME)/.config/pdtv26-camera/rtsp-url +SPEECH_KEY = /etc/pdtv26-speech/speech-key +# The speech server's host, if it's not this one: its key comes over ssh. +SPEECH = +ON_BOX = ssh $(ADMIN)@$(HOST) -.PHONY: help test card flash wifi-secret say volume record +.PHONY: help test card flash wifi-secret say volume record keys speech-key ask-key ir-key look-key rtsp-url ## list targets help: @@ -56,3 +66,26 @@ volume: ## chime, then record SECS seconds from the box's mics to heard.wav (32-bit stereo, 16 kHz) record: ssh $(ADMIN)@$(HOST) 'aplay -q -D speaker /etc/pdtv26-earcons/open.wav && arecord -q -D hw:CARD=voicehat,DEV=0 -f S32_LE -c 2 -r 16000 -d $(SECS) -t wav -' > heard.wav + +## every key, at both ends of its connection, by pipe: never on disk but where it's used +keys: speech-key ask-key ir-key look-key rtsp-url + +## speech's key, from here (or SPEECH) to the box's /etc/pdtv26/speech-key (root's alone) +speech-key: + $(if $(SPEECH),ssh $(SPEECH) )sudo cat $(SPEECH_KEY) | $(ON_BOX) 'sudo install -D -m 600 /dev/stdin /etc/pdtv26/speech-key && sudo wc -c /etc/pdtv26/speech-key' + +## the brain's ask key (made by ../brain if there's none), from here to the box's /etc/pdtv26/ask-key +ask-key: + $(MAKE) -C ../brain CONF=$(BRAIN_KEYS) $(BRAIN_KEYS)/ask-key + $(ON_BOX) 'sudo install -D -m 600 /dev/stdin /etc/pdtv26/ask-key && sudo wc -c /etc/pdtv26/ask-key' < $(BRAIN_KEYS)/ask-key + +## the box's ir and look keys: made on the box if there are none, and copied to the brain's keys here +ir-key look-key: + $(ON_BOX) 'sudo test -s /etc/pdtv26/$@ || head -c 20 /dev/urandom | base32 | tr A-Z a-z | tr -d = | sudo install -D -m 600 /dev/stdin /etc/pdtv26/$@' + install -d -m 700 $(BRAIN_KEYS) + umask 077; $(ON_BOX) sudo cat /etc/pdtv26/$@ > $(BRAIN_KEYS)/$@ + wc -c $(BRAIN_KEYS)/$@ + +## the camera's RTSP URL, password and all, from CAMERA_URL here to the box's /etc/pdtv26/rtsp-url +rtsp-url: + $(ON_BOX) 'sudo install -D -m 600 /dev/stdin /etc/pdtv26/rtsp-url && sudo wc -c /etc/pdtv26/rtsp-url' < $(CAMERA_URL) diff --git a/docs/setup.md b/docs/setup.md new file mode 100644 index 0000000..fa4424a --- /dev/null +++ b/docs/setup.md @@ -0,0 +1,70 @@ +# Setting up the agent at the TV + +You'll need the [parts](../README.md#parts), a Linux home server with Nix and systemd, a private network both it and the box can join (Tailscale, by default), and an afternoon. The steps below are in the order they depend on each other. Each `make` target says more with `make help`. + +## 1. Make a repository for your household + +A private git repository of your own holds what's yours: the names you choose, and later who the brain serves. None of that belongs in pdtv26. Its `flake.nix`: + +```nix +{ + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05"; + inputs.pdtv26 = { + url = "git+https://git.inference.coop/dckc/pdtv26"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + outputs = { self, pdtv26, ... }: { + nixosConfigurations.pimote = pdtv26.lib.mkBox { + name = "pimote"; # the box's host name on your network + server = "den-pc"; # your home server's host name: speech and the brain + wifi = "MyNetwork"; # your Wi-Fi network's name (SSID), spelled exactly + admin = "pat"; # who looks after the box, and logs in to deploy + sshKeys = [ "ssh-ed25519 AAAA... pat@laptop" ]; + }; + # The box's SD card image, built on x86_64 Linux. + packages.x86_64-linux.sd-image = self.nixosConfigurations.pimote.config.system.build.sdImage; + }; +} +``` + +Ports have defaults, and the box finds the IR emitter by its USB IDs. Anything else about the box's system goes in `modules = [ ./household.nix ]`, alongside: a status page (`pdtv26.box.statusUrl`), another Wi-Fi network, another private network (`pdtv26.box.network`), or no camera (`pdtv26.box.look.enable = false`). + +## 2. Set up the home server + +Any Linux with systemd and [Nix](https://nixos.org/download/) will do; it needn't be NixOS. Clone pdtv26 there, then: + +1. `sudo make -C speech install`: the speech server, its models (about 200 MB), its key, and its systemd unit. +2. `sudo make -C status install BOX=pimote`: the status page, if you want one. + +The brain comes after the box, in step 5, since it needs the box's keys. + +## 3. Make the box's SD card + +The image carries your household's names, so it's built from your household repository, on any Linux machine with Nix that can run ARM programs (on Debian or Ubuntu, `apt install qemu-user-static`). The Wi-Fi passphrase and the keys stay out of it. + +1. In your household repository: `nix build --option extra-platforms aarch64-linux .#sd-image`. +2. In pdtv26's `box/`: `make card DEV=/dev/sdX` to check you have the right device, then `make flash DEV=/dev/sdX IMG=path/to/household/result/sd-image/*.img CONFIRM=erase`. +3. Also in `box/`: `make wifi-secret SSID=MyNetwork`: it puts the network's PSK on the card from the passphrase in your desktop keyring, not in the image or the repository. Store the passphrase there once, with `secret-tool store --label="Wi-Fi MyNetwork" service wifi ssid MyNetwork`. +4. Put the card in the Pi, plug in the IR emitter's USB cord, and power it up. + +After this, changes to the box go over ssh, from your household repository: `nixos-rebuild switch --flake .#pimote --target-host pat@pimote.local --sudo --option extra-platforms aarch64-linux`. + +## 4. Join the private network + +On the box and the home server: `tailscale up`, or whatever private network you use. + +## 5. Make the keys, and start the brain + +On the home server, as the user the brain will run as, in pdtv26's `box/`: + +1. `make keys BOX=pimote ADMIN=pat`: one key per connection, piped to both its ends; the box's ir and look keys come back for the brain. Put the camera's RTSP URL in `~/.config/pdtv26-camera/rtsp-url` first, or skip `rtsp-url`. If speech runs on another host, add `SPEECH=` that host: its key comes over ssh. +2. Make a fine-grained GitHub token for one repository, Issues read/write only, for problem reports, and save it as `~/.config/pdtv26-brain/report-token`. +3. `make -C ../brain install BOX=pimote REPO=you/your-repo ASSIGNEE=you`, then `make -C ../brain attach` once: log in, trust the folder, and approve the box's channel. + +## 6. Tell it who it's for + +The brain reads `~/pdtv26-brain/household.md`: who looks after it, who it serves, what they like to watch, what to be patient about. `make install` puts an example there; make it yours, and keep it in your household repository. + +## 7. Teach it your remotes + +Press the button and ask: "Learn my TV's power button." Point the TV's remote at the IR emitter and press **Power** when it asks. Do the same for **Input**, the cable box, and the rest. It keeps what it learns in its notes. diff --git a/speech/Makefile b/speech/Makefile index 51e7ea5..2e98523 100644 --- a/speech/Makefile +++ b/speech/Makefile @@ -1,5 +1,6 @@ # speech: the agent's ears and voice, as a capability over Cap'n Web. # Tools come from the flake: nix develop --command make test +# sudo make install # on the home server: the server, its models, its key, and its unit WHISPER = https://huggingface.co/ggerganov/whisper.cpp/resolve/main MODEL = cache/ggml-base.en.bin @@ -8,8 +9,15 @@ VOICE = cache/en_US-danny-low.onnx PORT = 18024 PHRASE = Put on the ballgame. UA = pdtv26-speech-setup (curl) +FLAKE = .. +# Where it listens, and the private network's addresses: the only ones it answers. A tailnet's, by default. +SERVE_PORT = 8024 +RANGES = 100.64.0.0/10 fd7a:115c:a1e0::/48 +LIB = /var/lib/pdtv26-speech +ETC = /etc/pdtv26-speech +UNIT = /etc/systemd/system/pdtv26-speech.service -.PHONY: help test smoke +.PHONY: help test smoke app install .DELETE_ON_ERROR: ## list targets @@ -37,3 +45,37 @@ cache/en_US-danny-low.%: ## a round trip through a real server: speak PHRASE, hear it back, compare smoke: cache/speech $(MODEL) $(VOICE) $(VOICE).json ./smoke.sh cache/speech/bin $(MODEL) $(VOICE) $(PORT) '$(PHRASE)' + +## the server, installed: a GC root in LIB +app: + install -d $(LIB) + nix build --out-link $(LIB)/app '$(FLAKE)#speech' + +$(LIB)/models/%: + install -d -m 755 $(dir $@) + curl -fsSL -A '$(UA)' -o $@ $(WHISPER)/$* + chmod 644 $@ + +$(LIB)/voices/en_US-danny-low.%: + install -d -m 755 $(dir $@) + curl -fsSL -A '$(UA)' -o $@ $(VOICES)/$(notdir $@) + chmod 644 $@ + +## the key the box presents: 160 random bits, root's alone; systemd hands the service a copy +$(ETC)/speech-key: + install -d -m 700 $(dir $@) + umask 077; head -c 20 /dev/urandom | base32 | tr A-Z a-z | tr -d = > $@ + +$(ETC)/settings: Makefile + install -d -m 700 $(dir $@) + printf 'PORT=%s\nMODEL=%s\nVOICE=%s\n' $(SERVE_PORT) $(notdir $(MODEL)) $(basename $(notdir $(VOICE))) > $@ + +$(UNIT): pdtv26-speech.service Makefile + sed 's|@ranges@|$(RANGES)|' $< > $@ + +## build, fetch the models, make the key if there's none, install the unit, and (re)start it +install: app $(LIB)/models/$(notdir $(MODEL)) $(LIB)/voices/$(notdir $(VOICE)) $(LIB)/voices/$(notdir $(VOICE)).json $(ETC)/speech-key $(ETC)/settings $(UNIT) + systemctl daemon-reload + systemctl enable pdtv26-speech + systemctl restart pdtv26-speech + sleep 2; systemctl --no-pager status pdtv26-speech | grep -E '●|Active|listening' diff --git a/speech/pdtv26-speech.service b/speech/pdtv26-speech.service new file mode 100644 index 0000000..1ef1022 --- /dev/null +++ b/speech/pdtv26-speech.service @@ -0,0 +1,27 @@ +[Unit] +Description=pdtv26-speech: the agent's ears and voice, a capability over Cap'n Web on the private network +After=network-online.target +Wants=network-online.target + +[Service] +# Listens on every address, but the kernel lets only the private network and loopback in or out. +EnvironmentFile=/etc/pdtv26-speech/settings +ExecStart=/var/lib/pdtv26-speech/app/bin/speech-server :: ${PORT} /var/lib/pdtv26-speech/models/${MODEL} /var/lib/pdtv26-speech/voices/${VOICE}.onnx +DynamicUser=yes +LoadCredential=speech-key:/etc/pdtv26-speech/speech-key +IPAddressDeny=any +IPAddressAllow=localhost @ranges@ +RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX +NoNewPrivileges=yes +ProtectSystem=strict +ProtectHome=yes +PrivateTmp=yes +PrivateDevices=yes +# whisper uses every core it can; whatever else the home server does comes first. +Nice=10 +MemoryMax=10G +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=multi-user.target