In this lesson: what it takes to turn a model that ran once on your screen into an app other people can use: load the model once, keep each request small, let tests check what it decides, and say honestly what it can't do. Every number here comes from our own runs on 2 CPUs. Read the lesson and take the 5-question quiz: each question has a hint if you get stuck, and its explanation shows once you get it right (all of them at full marks). Pass the quiz to finish the lesson and earn its badge.
1. Load once, or load every time
Guess first: on 2 CPUs, how long does loading a small sentence model take? One sentence once it is loaded? A request that loads the model again each time? We timed it with MiniLM, a small sentence model, and one short question, Where is the maths homework for Friday?:
| What we timed | How long |
|---|
| Importing PyTorch and transformers (the libraries) | 4.14 s |
| Loading the model | 1.46 s |
| Request 1, model already loaded | 15 ms |
| Requests 2 and 3, model already loaded | 10 ms each |
| One request that loads the model again | 92 ms |
Importing PyTorch and transformers took about 4.1 seconds and loading the model 1.5 seconds. With the model already loaded, one sentence took about 10 milliseconds (the first a little more). Loading the model again for a request took about 90 milliseconds in this run, and about 80–130 ms over our runs (with the model files already on the computer): roughly ten times as long. That is still far less than the first load; we didn't measure why. A server that started a new Python for every request would be far worse: each visitor would wait at least 5.6 seconds, for 10 ms of real work. That 5.6 s is 4.1 s of imports plus 1.5 s of loading, added up; we didn't time it as one run. Load the model once, when the app starts, and every request after that is fast.
Load once: then a request takes about 10 ms
2. Keep requests small
Small computers are slow at big jobs, so a model app keeps every request small:
- Use the CPUs you have. Tell PyTorch to split its work into 2 threads (strands of work that run at the same time), one per CPU you have.
- Only answer. Switch off what only training needs (PyTorch's no-gradient mode): less memory and less work, the same answer.
- Small inputs. Cut the text (the app below keeps 200 characters), or refuse anything longer. Our runs also cut what goes into the model at 64 tokens (tokens are the pieces a model reads, see H1); MiniLM was trained on inputs of up to 128 tokens, so longer text helps little anyway.
- Small outputs. distilgpt2 took about 0.7 s for 20 new tokens and about 3 s for 100: the time grows with every token it writes, so keep the number of new tokens small.
- Batch when you can. 100 sentences in one batch took about 180 ms, against about 10 ms for one: 100 times the sentences for about 18 times the time.
Five ways to keep a request small
3. A small app that does it all
Here is a small app that does all of that: a notes search with three school notes, a program of just 24 lines of Python. The model and the notes' vectors are ready when the server starts, so each request embeds one sentence and takes three dot products (vectors and dot products: see H2). The question's vector is multiplied with each note's, which gives one number per note; the biggest number wins, and the reply is that note with its score. The question is trimmed of spaces at both ends and cut to 200 characters. An empty or blank box gets a 400 (the web's code for 'your request was wrong'; 200 means OK) instead of a crash. The page and the server send data as JSON, a plain-text format for data; a fuller app would also answer a broken request, one that isn't the JSON it expects, with a 400. MiniLM was trained on English, so the notes and questions are in English. The server is built on Python's own simple web server, which is fine to learn on; its docs say it isn't meant for a real app the public uses.
The three notes:
- Maths homework: page 42, due Friday.
- The PE test is the 800 m run on Tuesday.
- Art club meets in room 3 on Wednesdays.
Guess first: what does it answer to What is the capital of France?, which no note is about? What it answered when we ran it:
| Question | Reply | Score |
|---|
| when is the run | 200, the PE test note | 0.422 |
| drawing club | 200, the art club note | 0.557 |
| What is the capital of France? | 200, the maths homework note | 0.102 |
| (empty box) | 400, "empty" | none |
The France question got the maths note: the biggest of the three numbers always wins, even one as low as 0.102. Every rule is written down where someone can read it and test it.
One sentence embedded, three dot products
4. The program decides, the tests check it
A test is a small program that calls yours and checks one decision with a clear right or wrong. For the notes search, six tests sent requests to the server and checked: a maths question gets the maths note; the score is between −1 and 1; an empty box gets 400; a blank one gets 400; 316 characters are cut and still answered with 200; one request takes under 0.05 s. All six passed in about 0.1 s.
Then we changed one thing: the model was loaded again for every request, instead of once at the start. The speed test failed, with a request taking about 0.1 s (0.08–0.13 s). Clicking around by hand would not have noticed; the test did, the moment it happened. That same speed test would also catch a server that started a new Python for every request (section 1's 5.6 seconds or more).
The speed test caught the slow-down
5. Honest about limits
A published app tells people what it is and isn't:
- What it can't do. A sentence model finds the note whose vector is closest to the question's; it doesn't check whether that note answers the question, and it always returns something, even for a question no note answers. Say so, and show the score.
- What happens when it fails. An empty box gets a clear "empty" message, not a crash (and a fuller app answers a broken request the same way). A slow or missing answer gets a sentence, not a spinner forever.
- What the copy runs. A page on its own doesn't take your Python server with it: the model answers only where a server runs (your own, or a copy of your server that the site showing the app starts for each visitor). If nothing answers, the page should say so and show saved example results instead of pretending.
- What it must never carry. No passwords or keys in any file of the project (code, a settings file such as config.json, README, the page), not even a "private" config.json: any file can travel with the project when it is copied, shared or published. A server reads secrets from its environment (settings handed to the server when it starts, kept outside every project file) or a secrets store (a service that keeps keys locked away and gives them only to the server), never from a project file. No real names or phone numbers in sample data, and no copies of the model files (they are large; the server loads them from its own model folder).
Say what it can't do, and what it never carries
Do it yourself in the ML Lab Club Finder, Ready to Share, step Where "not sure" starts.