---
id: 17112
lang: en
title: "How to Serve a Local Llama 3 Classifier via FastAPI Without Paid Clouds"
published_at: "2026-11-21 01:55:15"
source_name: "Machine Learning Mastery"
source_url: "https://machinelearningmastery.com/serving-scikit-llm-pipelines-as-rest-apis-with-fastapi/"
canonical_url: "https://news.hamidun.com/en/news/17112"
categories: ["llm"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

# How to Serve a Local Llama 3 Classifier via FastAPI Without Paid Clouds

> **Краткая суть (BLUF):** Labeling reviews and support tickets with an LLM is convenient, but paid cloud usage and latency quickly become a problem. Scikit-LLM turns Llama 3 running in Ollama into a regular classifier with fit and predict methods, and FastAPI exposes it at /predict. The model is not fine-tuned: it is simply given a list of allowed labels and picks one, much like a multiple-choice test. For a prototype of an internal labeling service, that is enough, and no paid API is required.  • Three test reviews received labels in a few seconds on the local model • Labels such as Battery Issue are saved to the scikit_llm_model.joblib file • The blocking call is moved to a thread with asyncio.to_thread

Text classification is one of the most down-to-earth tasks for LLMs. You need to sort support requests by topic, separate complaints about batteries from complaints about a shattered screen, and tag positive reviews. The problem starts when the prototype has to be shipped as a service: cloud API bills grow, latency jumps around, and the project is tied to a single vendor. A walkthrough on Machine Learning Mastery shows the minimal way to avoid this: the combination of Scikit-LLM, Ollama, and FastAPI runs entirely on your own machine.

## What the stack is and how it works

Scikit-LLM is a Python framework that hides an LLM behind the familiar scikit-learn interface. You create a classifier, call fit with examples and labels, then call predict with new texts. For anyone who has already written scikit-learn code, the learning curve is nearly zero.

Ollama runs the model locally. The example uses llama3, which you need to download in advance. The key point is that Scikit-LLM connects to an OpenAI-compatible API by default, and Ollama can speak that same protocol. So it is enough to specify the address http://localhost:11434/v1/, put any string in place of the key, and name the model custom_url::llama3. No cloud key is needed, and you pay for no tokens.

An important detail: fit does not train anything here. The model's weights do not change. The classifier stores the list of allowed categories and inserts it into the request, just as if you had written a question with answer options in a prompt. The example has only three pairs: "The battery drains too fast" → Battery Issue, "I love the new camera" → Positive Feedback, "Screen is completely cracked" → Hardware Damage.

## How it becomes a service

The work is split across two files. In train.py, a ZeroShotGPTClassifier is created, fit is called, and the result is saved with joblib to scikit_llm_model.joblib. The entire "training" takes moments, because what gets saved is essentially a configuration with a set of labels.

In app.py, a FastAPI application starts up. The model is loaded into memory once at startup via lifespan, not on every request. There is a single endpoint, POST /predict, which accepts JSON with a list of texts and returns a list of predictions. You can send several texts in one call.

Two engineering decisions are worth adopting. First: because the call to the LLM blocks the thread, the clf.predict call is moved to a background thread via asyncio.to_thread. Otherwise, one slow request would freeze the entire asynchronous server. Second: the Scikit-LLM configuration (the address and the placeholder key) had to be set again in app.py, because these are global process settings rather than part of the saved file. Forgetting this is a typical source of errors during deployment.

The check is simple: you run python app.py, open http://127.0.0.1:8000/docs, click "Try it out", and send three phrases: about a phone that dies by noon, about glass shattered on the sidewalk, and about the best device of your life. A few seconds later the labels Battery Issue, Hardware Damage, and Positive Feedback come back.

## Limitations to keep in mind

The author himself names the main cost of this approach: the closed world. The classifier must assign any text to exactly one label from the list, even if the text is about something entirely different. A letter about delivery will also be stuffed into "Battery Issue" or "Hardware Damage" unless you add a suitable category, such as "Other". You have to add the fallback class yourself.

Beyond that, there are questions the article does not cover. Three training examples are only for demonstration; for a real system, the label sets and phrasings will need to be validated on your own data. Speed depends on hardware: "a few seconds" for three texts is a laptop-mode figure, not a production load. The service listens only on 127.0.0.1 and has no authentication, so it is far from ready for public use. Finally, the word "deterministic" in the text should be understood as "predictable within a set of answers": an LLM may still occasionally answer differently on borderline phrases.

## Takeaways

The practical conclusion for a developer: if the task is labeling texts into predefined categories, you do not necessarily have to pay a cloud provider right away or collect a dataset for fine-tuning. A local model plus a fit/predict interface gives a working prototype in an evening, and FastAPI turns it into a service that is easy to connect to the rest of the system. Because Scikit-LLM is compatible with scikit-learn objects, you can also add preprocessing steps to this classifier within a shared pipeline. For the first stage, which is to test the hypothesis and calculate what such labeling costs on your own traffic, this is enough.

---
- **Первоисточник:** [Machine Learning Mastery](https://machinelearningmastery.com/serving-scikit-llm-pipelines-as-rest-apis-with-fastapi/)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/17112
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
