Join us

Create a ML project exposed with API

We are going to modify the data for the training format. It will be stored as a json file.

Step 0 — create basic FastApi server

In main app directory we found file

Isside app folder I’ve created 3 folders

  • api/api_v1 —all api settings will be stored in version 1.
  • core —inside I will store the global configuration file
  • models — here I will put the files and scripts related to the training and use of the models

So, in the api/api_v1 directory I have created the file which is the main point that manages all the path files.

And finally inside I have created the endpoints folder and inside I have created two (for now) files

  • — with default response {“hello”: “world”}

  • — this file will manage what to do when someone ask for house prediction.

Now we can run our server to check if the default route is working.

Step 0.1 Create Docker with FastAPI server

At this point — it’s optional, but I really like to make the configuration to use each solution as a docker image — so, let’s create a simple Dockerfile

I create Dockerfile in the main folder.

Now I can build it.

And try it.

Step 1 — Create some model

As a first model I got Predicting House Prices Challenge from HackerRank. Day 6: Multiple Linear Regression: Predicting House Prices | HackerRank

We are going to modify the data for the training format. It will be stored as a json file.

The program that fits based on this data is a simple implementation of the linear sklearn model.

We write the state of fit using pickle to the file. This file is our model. This principle is used in virtually all AI implementations.

Step 2 — Create API to query the model

First, the class we need to return prediction, based on previously trained model is like (fastapi_ml/app/models/MLR_PredictingHousePrices/

And finally we can create implementation for prediction API. Let’s modify fastapi_ml/app/api/api_v1/endpoints/ file-

To test our sollution we use eg. Postman. Sending as POST with json in body.

I hope you liked this little article.

Here is the link to this version of repository

Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!


Rafal Lagowski

Backend Developer, ML, AI, GCP, Accenture

AI and ML enthusiast. Developer for work, entrepreneur for life, researcher for hobby. Depending on context the causes can be interchanged.
User Popularity



Total Hits