Running the server¶
bioamla serve runs bioamla's HTTP API on the machine that holds your recordings, so a
laptop, an agent or MagPy can work in workspaces there
without copying the data. This page covers installing it, starting it, keeping it running,
and reaching it safely. The design behind it is in
Workspaces, remote work and agent skills.
What works today
The server currently answers /v1/health (no token), /v1/version and its OpenAPI
spec at /v1/openapi.json. Running operations over HTTP, jobs with progress and cancel,
the workspace and files routes, and the client and --remote routing are being added
for 0.4.0 (see the 0.4.0 milestone).
The setup on this page stays the same as they land.
Security model¶
The server is for one user on a trusted network:
- It listens on
127.0.0.1by default. Reach it through a VPN or an SSH tunnel rather than opening it to a network. - Every request except
/v1/healthmust carry the token:Authorization: Bearer <token>. - Every path a request touches must be inside the workspace root (
--root) or a folder you allowed with--allow-link. Anything else is refused, including through symlinks.
If you start it on another address, it warns you: anyone who can reach that address and has the token can run work on the machine.
Install¶
The server is an optional extra:
$ pip install 'bioamla[server]'
Add the extras for the work you'll run there, e.g. bioamla[server,all] for every domain.
Start it¶
$ bioamla serve --root ~/bioamla-workspaces
Workspaces: /home/me/bioamla-workspaces
Token: in /home/me/.config/bioamla/server-token
API: http://127.0.0.1:8765/v1/ (spec at /v1/openapi.json)
| Option | Default | What it does |
|---|---|---|
--root |
(required) | Folder holding the server's workspaces; created if missing |
--host |
127.0.0.1 |
Address to listen on |
--port |
8765 |
Port to listen on |
--token-file |
~/.config/bioamla/server-token |
File holding the bearer token; created if missing |
--allow-link |
(none) | A folder outside --root that workspaces may link in place; repeat for several |
--max-window |
600 |
The longest audio window (seconds) a client may ask for |
Check that it's up:
$ curl http://127.0.0.1:8765/v1/health
$ curl -H "Authorization: Bearer $(cat ~/.config/bioamla/server-token)" \
http://127.0.0.1:8765/v1/version
The token¶
On first start, bioamla serve writes a random token to --token-file and reuses it after
that. To rotate it, stop the server, delete the file and start it again.
- On macOS and Linux the file is created with mode
600(readable only by you), and a token file that others can read is refused. - On Windows that check isn't made: keep the file in a folder only you can read.
- A token file you write yourself must hold exactly one line, with no spaces in the token.
Clients read the token from an environment variable you choose, never from a stored copy; see "Remotes" in the architecture page.
Reaching it from another machine¶
Keep the server on 127.0.0.1 and bring the connection to it.
SSH tunnel. From your laptop, forward a local port to the server's:
$ ssh -N -L 8765:127.0.0.1:8765 me@recordings-server.example
The API is then at http://127.0.0.1:8765/v1/ on the laptop, for as long as the tunnel
runs.
VPN (e.g. Tailscale or WireGuard). Start the server on the machine's VPN address instead of localhost, so only devices on your VPN can reach it:
$ bioamla serve --root ~/bioamla-workspaces --host <the machine's Tailscale address>
The token is still required. Don't use --host 0.0.0.0 on a machine reachable from the
internet.
Run it in a container¶
examples/server/ has a
Dockerfile and a compose.yaml: the workspace root and the recordings as bind mounts, the
token as a secret, the port published on the host's loopback only, and a build argument for
the extras (the default image is slim; all,cli for prediction and training). From that
folder:
$ python3 -c "import secrets; print(secrets.token_hex(32))" > bioamla-token
$ chmod 600 bioamla-token
$ docker compose up -d --build
Its README has the rest, including the GPU setting.
Keep it running with systemd¶
A user service runs as you, with your files and token; enable lingering so it starts at boot without a login:
# ~/.config/systemd/user/bioamla.service
[Unit]
Description=bioamla server
After=network-online.target
[Service]
ExecStart=%h/.local/bin/bioamla serve --root %h/bioamla-workspaces --allow-link /srv/recordings
Restart=on-failure
[Install]
WantedBy=default.target
$ systemctl --user daemon-reload
$ systemctl --user enable --now bioamla
$ sudo loginctl enable-linger $USER
$ journalctl --user -u bioamla -f
Point ExecStart at the bioamla of the environment you installed it in (for a virtual
environment, /path/to/venv/bin/bioamla). For a system service, put the unit in
/etc/systemd/system/, add User= and Group= for an unprivileged account, and give
--root and --token-file paths that account owns.
Working with recordings already on the machine¶
Large archives shouldn't be copied into a workspace. Allow their folder with --allow-link,
then link recordings into a workspace instead of importing them; they're used where they
are:
$ bioamla serve --root /srv/bioamla-workspaces --allow-link /srv/recordings
Inside a workspace on that machine:
$ bioamla workspace add /srv/recordings/site-a/2024
A linked folder must be under --root or one of the --allow-link folders; the server
refuses others.
Audio windows¶
A viewer needs a few seconds of a recording, not the file. GET
/v1/workspaces/{ws}/audio/{path}?start=S&duration=D returns that window as a float32 WAV,
decoded on the server from just that part of the file; sample_rate=R resamples it,
keep_channels=true keeps a multichannel recording's channels instead of mixing them to
mono, and --max-window caps D (times the channel count when channels are kept, so a
kept window holds no more samples than a mono one could). The X-Bioamla-Start, X-Bioamla-Duration, X-Bioamla-Sample-Rate
and X-Bioamla-Channels headers say what came back (a window is cut short at the end of
the recording). The server holds a window in memory while it encodes it, 4 bytes per sample
per channel: 600 s at 48 kHz is 115 MB, at 384 kHz 0.9 GB, so lower --max-window on a
server whose recordings are ultrasonic. From the command line:
$ bioamla remote audio lab costa-rica raw/site1/rec.wav --start 120 --duration 10 -o clip.wav
In Python, Client.audio_window() returns the window as an AudioData.