Serve the Model Over HTTP
A model that only answers the person holding the Python session is a demo. Products talk to models over HTTP, and this lesson puts the model model loading and tokenization loaded behind an endpoint: a real server on the GPU box, called from your laptop through the tunnel the lesson on remote GPU setup taught. Stage 5 of the setup lab rehearsed this call with a stand-in; today the server is real. The server skeleton on this page was run and curled this session (with a stub in the model's place, since the skeleton is what a laptop can verify); the generation outputs are representative runs from the box.
This is also a homecoming. The course's first lesson, inference engineering, opened with a dozen-line serve.py as a cautionary artifact, the thing whose failure under real traffic defines the discipline. You now know enough to build it for real and to see exactly where its edges are.
The server
The field's default pair for a first server is FastAPI, which turns a Python function into an HTTP endpoint, and uvicorn, which runs the app. On the box:
Apart from the two new imports, everything above the app line is model loading and tokenization verbatim, run once at startup so the 16 GB load happens before the first request, not during it. The endpoint itself is the generation from token generation and streaming with a request body around it. The lock line is load-bearing: FastAPI runs a plain def endpoint in a thread pool, so without it two callers' generations would run at once and interleave on the one GPU, both slowing. With it, the server takes one request at a time, a choice the last section returns to. Start it inside tmux:
The server binds to localhost on purpose: the tunnel is the front door, and putting real authentication and admission in front of a model is the production spaces' business, not a first server's.
Calling it
From your laptop, with the tunnel from remote GPU setup open (ssh -L 8000:localhost:8000 gpubox):
The sentence coming back is the greedy continuation from token generation and streaming, now arriving over a network. That response crossing your laptop's terminal is the whole inference lifecycle what inference is drew, operated by you end to end: an artifact chosen and downloaded, loaded onto rented hardware, wrapped in a service, answering requests over a network. Any program that can speak HTTP, a notebook, a script, another service, can now use your model.
Streaming over HTTP
The streaming from token generation and streaming had one consumer: your terminal. Over HTTP, the same effect uses a streaming response, with the generation running in a background thread while the endpoint hands fragments out as they arrive:
Call it with curl -N localhost:8000/stream ... (the -N turns off curl's buffering) and the continuation arrives in your laptop's terminal the way it arrived in the box's terminal in token generation and streaming, fragment by fragment. This is, at skeleton level, how every chat product delivers its answers. The lock rides along in locked_generate, held by the generation thread itself, so a stream and a plain request never share the device either.
What this server is not
Run two curl calls at the same moment and the second answer arrives only after the first finishes: the lock admits one request at a time, and every other caller waits in line. Nothing here shares one read of the weights across users, the opportunity the lesson on inference engineering said nobody serving production traffic leaves unused; nothing admits, queues, retries, or measures. This is the naive stack, complete and working as designed, and it is exactly the artifact inference engines picks up: its opening lesson is titled "Why Plain PyTorch Serving Breaks Down," and its lab benchmarks this very server against a real engine.
The module closes with the lab: this server exists because you assembled it by hand this afternoon, and the lab makes it come back identically from a fresh start.