r/learnmachinelearning • • 29d ago

Project After model.fit(): getting a trained model behind an API in two commands

Post image

Most ML tutorials end when the accuracy number prints. The question that comes right after, how does anything else actually use this model, is the one that made me start Flama back in 2018: an open source (Apache-2.0) Python framework to take a trained model to an API with as little ceremony as possible.

Here's the whole path with a scikit-learn model.

1. Save the model, with its metrics, into one file

import flama
from sklearn.datasets import load_iris
from sklearn.neural_network import MLPClassifier

X, y = load_iris(return_X_y=True)
model = MLPClassifier(activation="tanh", hidden_layer_sizes=(10,), max_iter=2000).fit(X, y)
flama.dump(model, family="ml", path="model.flm", metrics={"accuracy": model.score(X, y)})

The model.flm is a single file with the model and everything about it: the library and version it was trained with, its hyperparameters, and the metrics you passed in.

2. Serve it

pip install "flama[pydantic]"
flama serve --model file=model.flm,url=/model,name=iris

That's a running API. No server code, no Dockerfile.

3. Call it

curl http://127.0.0.1:8000/model/predict/ \
  -H "content-type: application/json" \
  -d '{"input": [[5.1, 3.5, 1.4, 0.2], [6.7, 3.0, 5.2, 2.3]]}'

{"output":[0,2]}

Send something that isn't a list of rows and you get a 400 telling you what's wrong, not a stack trace. And http://127.0.0.1:8000/docs/ has interactive docs for every endpoint, generated for you:

4. Check it without a server

flama model model.flm inspect                 # library, hyperparameters, metrics
flama model model.flm run -i holdout.json     # predictions, offline

Handy in CI: read the accuracy straight from the file and only ship the model if it's good enough.

From there

The same idea works for PyTorch, TensorFlow and Hugging Face models, and for LLMs too. When you outgrow the one-liner, the model becomes a resource in a full API next to your own endpoints (app.models.add_model(...)), with validation, docs and auth from the same framework.

Repo and docs: https://github.com/vortico/flama

If it helps you get a model out of a notebook, a star on GitHub helps more people find it. Happy to answer questions, especially the "but how do I actually deploy this" ones.

1 Upvotes

2 comments sorted by

2

u/[deleted] 29d ago

[removed] — view removed comment

1

u/p3rdy 29d ago

Ha, that was pretty much the itch back in 2018. And when you do need your own logic around the model, it's just a normal endpoint next to it in the same app, so the boilerplate doesn't creep back in.