Files
microduck/spaces/hello/Dockerfile
T
Pierre Rouanet 4e3506b8d4 vision-demo receives the frames instead of pulling them, and six findings about Spaces
The Space is the other half of `media.stream`: FastAPI owns the server, the robot
dials `wss://…/frames`, `av` decodes the access units and OpenCV runs over each
picture — **on Hugging Face hardware**, which was the point of the whole exercise.
The browser only ever receives the processed frame as an image.

Its WebRTC consumer is gone, and with it `aiortc`, the DTLS cipher shim over one
of their private methods, and the ICE stack that could never complete from a data
centre. What is left is `gradio`, `fastapi`, `opencv`, `requests` and `av` — a
decoder rather than a transport.

Frames are filed per account: the endpoint is public, so the robot presents its
own token and this resolves it through `whoami-v2`, exactly as the rendezvous
does. Without that anybody could push frames into the demo and, worse, a visitor
could be shown a stranger's camera.

`fake_duck.py` is the robot's whole side in Python — registers as a producer,
answers `rpc` envelopes, pushes H.264 from the webcam — which is what makes a
deployed Space testable with no board. Verified from a phone on 4G: duck at home,
Space in a data centre, viewer on mobile data, no network in common.

**Six things about Docker Spaces that each cost a round trip**, written down
because none is guessable and every one presented as something else:

- **`SYSTEM=spaces` is what Gradio checks**, not `SPACE_ID`. `get_space()` is
  `os.getenv("SYSTEM") == "spaces"`, Hugging Face sets it for gradio-SDK Spaces
  and not for Docker ones, so Gradio installs its *mocked* OAuth routes and the
  mock raises without a local `hf auth login`. The module then fails at import.
- **`git` must be installed.** Their builder appends `git config --global` to
  every Docker Space; a `-slim` base dies with exit 127 *after* the image built
  fine, reported as a page of cache-miss noise naming one of their own steps.
- **The user must be `user` at `/home/user`.** Their appended layers assume it.
- **`gradio[oauth]`, not `gradio`** — the extra is what carries `itsdangerous`
  and `authlib`, and without them a Space with `hf_oauth: true` fails at import.
- **A crash reports one truncated line, and reading a Space's logs needs write
  access to it.** So `boot.py` imports the app in a `try` and *serves the
  traceback* on the expected port when it cannot; `/env` reports which platform
  variables arrived. Those two are what ended four rounds of blaming the port for
  an import error.
- **Safari drops the OAuth session cookie** as third-party, because a Space runs
  in an iframe. The token box is therefore always offered, and the Space's own
  `.hf.space` URL works too.

`spaces/hello` is the baseline that proved the platform rather than the app:
Docker, FastAPI, one WebSocket route, Gradio mounted, no OAuth. Publish it into a
Space that will not start and it says which of the four is at fault.

One efficiency fix that was mistaken for a Safari bug: the timer polled ten times
a second for a five-frame-a-second stream and re-shipped four hundred log lines
every second, so half the responses re-encoded a picture the page already had.
`render` returns `gr.skip()` when the frame has not changed — 19 of 20 idle polls
now send nothing.

Assisted-by: Claude:claude-opus-5[1m]
2026-09-08 18:21:09 +02:00

23 lines
769 B
Docker

FROM python:3.12-slim
# `git` because Hugging Face appends a `git config --global` layer to every Docker Space, and a
# `-slim` base has none — that layer then fails with exit 127 after the image has built fine.
RUN apt-get update \
&& apt-get install -y --no-install-recommends git \
&& rm -rf /var/lib/apt/lists/*
# The canonical uid-1000 `user` at `/home/user`: the platform's appended layers assume both.
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH \
PYTHONUNBUFFERED=1
WORKDIR $HOME/app
COPY --chown=user requirements.txt $HOME/app/
RUN pip install --no-cache-dir --user -r requirements.txt
COPY --chown=user . $HOME/app
EXPOSE 7860
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]