Deployment Guide
Important Security Notice
Seg-Studio is designed for local or trusted network use, and it ships that
way: every start script under scripts/ defaults to 127.0.0.1, so a fresh
install is reachable only from the machine it runs on. LAN access is opt-in — Settings → Allow access
from LAN, or SEG_HOST=0.0.0.0 — and takes effect at the next restart.
On that loopback default the trainer API needs no token (a Host
allowlist and a same-origin check still apply, so a page you merely visit
cannot drive it). For LAN /
reverse-proxy deployments the shared secret SEG_API_TOKEN requires an
X-API-Token header on every /api/v1/*, /v2/*, and /ws/v2/* request
(WebSockets included — browsers can pass ?api_token=<value> instead of the
header); the Web UI signs in with it once and then uses a session cookie. The
two settings are tied together: the trainer API refuses to start when
SEG_HOST (or the persisted LAN toggle) puts it on a non-loopback address while
SEG_API_TOKEN is empty. That check reads SEG_HOST, not uvicorn’s --host,
so if you pass --host 0.0.0.0 by hand, set SEG_HOST=0.0.0.0 as well.
Skipping it does not expose anything: a server with no token answers only
clients on the machine it runs on, so the port opens and every remote
request is refused. It does mean the failure arrives as a 401 per request
instead of a message at startup.
The token covers the trainer API only. The serving API on port 8001 has no authentication at all — read The serving API (port 8001) is unauthenticated before you open that port. Do not expose either API to the public internet without a reverse proxy providing TLS and real authentication.
Browser sign-in
A browser cannot attach a custom header to an <img src>, a stylesheet, or a
download link, so a header-only credential would leave the bundled Web UI
unable to render a single overlay. Instead the UI signs in once:
- Start the server with
SEG_HOST=0.0.0.0, or enable LAN access in Settings and restart with the launcher for your platform (scripts/windows/start_local_windows.bat,scripts/macos/start_api.sh,scripts/start_local.sh). On the first LAN start the launcher generatesSEG_API_TOKEN, prints it, and stores it asapi_tokeninprojects/runtime_settings.json; later starts reuse it. Set the environment variable yourself to use your own secret instead. - Open
http://<server>:8002/ui/from another machine. The UI asks for the token before it loads. - On success the server issues a session cookie —
HttpOnly(page scripts cannot read the secret),SameSite=Strict(another site cannot ride it), and carrying a hash of the token rather than the token itself. The browser then attaches it to every same-origin request, images and downloads included.
The cookie is derived deterministically from the token, so sessions survive a
server restart, and rotating SEG_API_TOKEN invalidates every existing one.
Because a cookie is an ambient credential, cookie-authenticated requests are
still subject to the same-origin (CSRF) check; an X-API-Token header — used
by scripts and curl — is not. (The bundled seg-sdk client does not send the
token yet, so run it on the machine hosting the server — whose requests are
token-exempt, see the next paragraph — or point it at a server with
SEG_API_TOKEN unset.) No reverse proxy is required for this: it
is only needed if you additionally want TLS or a second authentication layer.
Requests from the machine running the server are exempt from the token: binding
to the LAN should not make the operator’s own browser sign in to its own desktop
app. The exemption keys off the TCP peer address, which — unlike the Host header
— a remote client cannot forge, and it drops only the token: a local request is
still held to the same-origin and Host-allowlist rules that protect the default
loopback install. A request carrying X-Forwarded-For, X-Forwarded-Host,
X-Real-IP or Forwarded is never treated as local, because a reverse proxy on
this host also connects from loopback while the request behind it did not.
The sign-in routes (/api/v1/auth/status, /auth/session, /auth/logout) are
the one part of /api/v1/* that is not token-guarded — they are how a client
with no credential obtains one. /auth/status reveals only whether a token is
required; /auth/session issues nothing unless the caller already knows it.
The serving API (port 8001) is unauthenticated
apps/serving_api is a second, deliberately minimal FastAPI app. It loads the
ONNX model the trainer activated in models/registry/ and answers
POST /segment and POST /count. It does not read SEG_API_TOKEN, and it has
no token, cookie, Host or origin check of any kind. Anything that can reach
port 8001 can run inference, list the registered model ids (GET /models), and
make the process re-read the registry pointer (POST /reload).
That is fine on the loopback default, and only there. The launchers start the
serving API with the same SEG_HOST as the trainer
(scripts/windows/start_local_windows.bat, scripts/macos/start_local_macos.sh,
scripts/start_local.sh, scripts/windows/start_serving_api.bat), so turning on
Settings → Allow access from LAN publishes port 8001 to the LAN too —
without the token gate the trainer gets. Before you do that, pick one:
- Leave the serving API on loopback and keep its callers on the same machine. This is the default and needs no configuration.
- Put it behind a reverse proxy that terminates TLS and authenticates (the nginx
block below, with
proxy_pass http://127.0.0.1:8001;), and block port 8001 at the host firewall so the proxy is the only route in.
docker compose publishes it on 127.0.0.1:8001 only.
Recommended Architecture
[Internet] → [Reverse Proxy (nginx)] → [Seg-Studio API :8002]
↓
TLS termination
Basic/OAuth auth
Rate limiting
Reverse Proxy Setup (nginx)
Below is an example nginx reverse-proxy configuration. It combines a TLS certificate with Basic authentication.
server {
listen 443 ssl;
server_name seg-studio.example.com;
ssl_certificate /etc/ssl/certs/cert.pem;
ssl_certificate_key /etc/ssl/private/key.pem;
# Basic authentication
auth_basic "Seg-Studio";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
proxy_pass http://127.0.0.1:8002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# WebSocket support (train logs /api/v1/ws/..., streaming /ws/v2/...)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
client_max_body_size 2G; # for ZIP uploads
}
}
Set SEG_API_TOKEN for this setup. nginx connects from this machine, but the
X-Real-IP header it adds (as would X-Forwarded-For or Forwarded) tells the
server that the request came from elsewhere, and a server with no token answers
every such request 401 This server has no API token configured. With the
token set, the Web UI signs in once and then uses its session cookie, and other
clients send X-API-Token; neither is checked against the Host allowlist.
SEG_ALLOWED_HOSTS does not change this: it applies only to requests the server
takes as coming from this machine, which a proxied request is not.
Creating the htpasswd file
# If apache2-utils is not installed
sudo apt install apache2-utils
# Create a user
sudo htpasswd -c /etc/nginx/.htpasswd username
Environment Variables
| Variable | Default | Description |
|---|---|---|
SEG_PROJECTS_DIR |
./projects |
Project data directory |
SEG_DB_PATH |
./projects/app.db |
SQLite database path |
SEG_MODELS_DIR |
./models |
Directory for exported/registered models |
SEG_TORCH_DEVICE |
auto |
PyTorch device (cuda:0, mps, cpu, or auto) |
SEG_API_TOKEN |
(empty) | Optional shared secret. When set, /api/v1/*, /v2/*, and /ws/v2/* require X-API-Token (or ?api_token= for browser WebSockets). The Web UI signs in with it once and then uses a session cookie — see Browser sign-in |
SEG_PREPARED_IMAGE_FORMAT |
lossless |
Deprecated. Format for the training copies under prepared/images/. The default writes PNG (lossless for RGB; alpha is dropped). Set jpeg to trade fidelity for much smaller prepared sets and faster decode; see the handbook for the trade-off. Set the format per project instead (PUT /api/v1/projects/{id}/image-store), which governs what is stored in images/ |
LOG_LEVEL |
INFO |
Logging level (DEBUG, INFO, WARNING, or ERROR) |
LOG_FORMAT |
text |
Log format (text or json) |
SEG_HOST |
127.0.0.1 |
Bind address. Set to 0.0.0.0 for LAN access — requires SEG_API_TOKEN, which the server enforces at startup |
SEG_ALLOWED_HOSTS |
(empty) | Comma-separated extra hostnames accepted in the Host header (DNS-rebinding allowlist; loopback names are always accepted). Applies only to requests from this machine with no forwarding header; a reverse proxy that adds X-Real-IP or X-Forwarded-For needs SEG_API_TOKEN instead |
SEG_ANNOTATION_URL, SEG_CVAT_URL |
(unset) | Address the trainer API forwards /annotate/* (or /cvat/*) to. Unset, the route does not exist. The start scripts set SEG_ANNOTATION_URL to http://localhost:8081 only when they start Label Studio (SEG_START_LABEL_STUDIO=1). The route is guarded like /api/v1/*, and the token and the session cookie are not passed on |
SEG_VLM_BACKEND |
(unset) | Model server type for the AI support panel: ollama, openai, mlx, vllm, lmstudio or llamacpp (ollama when none is named). Set here, or with SEG_VLM_BASE_URL or SEG_VLM_API_KEY_ENV, it fixes the server, and the panel shows that server in place of its list of server kinds — see the User Guide |
SEG_VLM_BASE_URL |
(the type’s own address) | http:// or https:// address of the model server. Left out, it is 127.0.0.1 at the server’s usual port for ollama, lmstudio, vllm, mlx and llamacpp, and https://api.openai.com/v1 for openai, so a local OpenAI-compatible server needs this set or its own type named. The pictures a run looks at, the instruction, the project’s name and the tool results are sent there |
SEG_VLM_MODEL |
(unset) | Model the panel uses unless another is picked there |
SEG_VLM_API_KEY_ENV |
(unset) | Name of the environment variable that holds the model server’s API key; its value is sent to that server as a bearer token. Set that variable in the trainer API’s environment too |
PYTORCH_CUDA_ALLOC_CONF |
expandable_segments:True |
CUDA allocator config for better VRAM management. Set by the API at import unless the variable is already present in the environment |
Docker (Optional)
A docker-compose.yml ships with the repository (trainer API on 127.0.0.1:8002, serving API on 127.0.0.1:8001, UI on 127.0.0.1:5173 — ports are published on loopback only).
Set SEG_API_TOKEN first. Each container binds 0.0.0.0 inside its own
network namespace — that is how a published port reaches it — which the startup
check reads as a non-loopback bind, so the trainer container exits immediately
while the secret is empty. Put one in a .env file next to docker-compose.yml
(the repo root is mounted into the container and the app loads that file at
startup):
# Windows: use `python` instead of `python3`
python3 -c "import secrets; print('SEG_API_TOKEN=' + secrets.token_urlsafe(24))" >> .env
docker compose up -d
The compose stack requests no GPU (there is no
deploy.resources.reservations.devices entry), so the trainer container runs on
CPU: annotation, inference and the UI work, and semantic training runs on the
CPU (much slower). Object counting (instance) training needs a native NVIDIA
install — see GPU Requirements.
The AI support panel looks for its model server at 127.0.0.1, which inside
the compose stack is the trainer container itself, so the kinds in the
panel’s list find nothing there. For a model server on the host or on another
machine, set SEG_VLM_BACKEND and SEG_VLM_BASE_URL in the trainer
service’s environment to an address the container can reach.
Otherwise, run directly:
pip install --no-deps -r apps/trainer_api/requirements.txt
# Localhost only (default):
python -m uvicorn apps.trainer_api.app.main:app --port 8002
# LAN access (use with reverse proxy + auth):
SEG_HOST=0.0.0.0 python -m uvicorn apps.trainer_api.app.main:app --host 0.0.0.0 --port 8002
Data Backup
Project data is stored in $SEG_PROJECTS_DIR (projects/ by default):
app.db— Project metadata (SQLite){project_id}/— Images, masks, training runs, models
Back up the entire projects/ directory regularly.
GPU Requirements
NVIDIA (Windows / Linux):
- Minimum: GTX 1650 (4GB VRAM) — training with batch_size=4-8
- Recommended: RTX 3060+ (8GB+ VRAM) — larger batch sizes, SAM2 models
Apple Silicon (macOS):
- M1/M2/M3/M4: MPS acceleration for both training and inference.
autoselects MPS when no CUDA device is present. Mixed precision is disabled on MPS, so a run is slower than on a comparable NVIDIA card, and MPS shares unified memory with the rest of the system — see Troubleshooting if a run runs out of memory. - Intel Mac: CPU only. The installer detects the architecture and says so. There is no automated coverage for this configuration, so treat it as untested.
CPU-only: Annotation, inference, and training work on every platform. CPU
training is exercised by the test suite, but it is far slower than a GPU run.
Set SEG_TORCH_DEVICE=cpu.
Object counting (instance segmentation) requires an NVIDIA GPU. The VRAM
auto-fit only runs on CUDA devices, and the measured requirement for the
small model is 8 GiB at the default batch 8, auto-reducing to batch 4
(5.5 GiB) and batch 2 (3.5 GiB); below 3.5 GiB it is unsupported.
Production Checklist
Check the following before you deploy.
- TLS (HTTPS) is enabled through the reverse proxy
- Authentication (Basic or OAuth) is configured
SEG_PROJECTS_DIRhas enough disk space- A regular backup schedule is in place
- The log level is set appropriately for production (usually
WARNINGor above) - The firewall blocks direct access to port 8002 (only the reverse proxy is allowed)
- The firewall also blocks direct access to port 8001 (the serving API, which has no authentication)