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 main.py 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 api.py 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

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

  • predicthouseprice.py — 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/predict.py)

And finally we can create implementation for prediction API. Let’s modify fastapi_ml/app/api/api_v1/endpoints/predicthouseprice.py 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 https://github.com/rafeekpro/fastapi_ml/tree/476d2d820aea4d4432ea8e02430a6d4cf7d64c63


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!

Avatar

Rafal Lagowski

Backend Developer, ML, AI, GCP, Accenture

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

Influence

2k

Total Hits

1

Posts