Diabetes Predictor with FastAPI and Scikit-Learn

As COVID-19 continues to rage on, there is a continuous need for the application of Machine Learning in Health. Health is a crucial part of our lives that can be significantly improved with the application of machine learning. Some application areas in health include;

  1. Disease Diagnosis

2. Drug Discovery and Manufacturing

3. Personalized Treatment

4. Smart health recording

5. Outbreak prediction and containment

and many others.

I built a model a while ago that predicts Diabetes using an open-source dataset and so I decided to create a web app based on this model. The web app will take in the user’s Age, Glucose level, and BMI to predict the possibility of being diagnosed with Diabetes. Please note this is not meant to be a professional medical diagnosis but can certainly be improved to do so. It is just for learning purposes only.


Let’s begin our app by creating a virtual environment. I will use conda from Anaconda since it is easy to setup. Run the following command on your terminal if you have conda or anaconda installed.

Then install the dependencies from the requirements.txt file:


Next, we will create our app. Our project folder structure will look like the following;

You will realize that I also added a diabetes.sav file which is my saved model from scikit-learn . This file and the code for this project can be found here.

The code for the app.py file will look like the following:

In the file above, we first import our libraries and then create an instance of FasAPI, the web framework we are using to build our app. We then creates a templates variable that points to our template file. After that, we create a variable(filename) that points to our saved model file.

We then create two routes; a get route that serves as our home routes and then displays our form that will take the user's inputs. The post route is where our form processing happen. In this route, we first create an empty dictionary that will contain our prediction. We then make sure that our request method is post which is required for form data. Then we extract the inputs into a list we can feed to our model by calling the .predict method on it. We also get the prediction probability by using the .predict_prob method.

Finally, we render the result in our index.html file.


This file will look like the following:

We are using Tailwindcss; a cool utility-first CSS library that makes building websites fast and we are also using Heropatterns for the cool background.

Our logic in the template is simple. We first check to see if there is a result and then we check to see if the result’s prediction is equal to 1. If it is equal to 1, we say the user is Diabetic. Otherwise, he is not. We are using Jinja2 template engine.

The line below used a SweetAlert2 to display a disclaimer about the app.

Let’s finally run the app by adding the following to the app.py

Then finally run the app with:

If all goes well, you should see the following;


Machine Learning has come to stay and has enormous benefits for us today. It can significantly save the time it takes to diagnose simple diseases like diabetes. This application has just shown you how to do that. There so many other areas in health we can apply machine learning. I hope you are inspired to build your ml app.

AI Engineer, Data Scientist, Full-stack Developer. Co-founder (Trestle Academy Ghana(TAG))

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store