top of page
Writer's picturefuatakal

How to Move your Data Application to the Web?

Updated: Mar 16, 2022

Let us say you developed a machine learning model to diagnose Growing Pains (GP) in children. It is a disease, which is typically confused with other diseases and difficult to diagnose. Thus, it makes children suffer and go through unnecessary tests. You do not like this and make your ML model available to pediatricians. Hopefully, your model facilitates the diagnosis process and children will suffer less.


You may be an expert on data science. However, building a Web application and deploying it is another profession. There is an easy way though for you to open your ML model to the outside world.


It is called streamlit. It provides the "fastest" way to build and share your data applications over the Internet.


You can easily install it on your machine. I am using macOS and this is how I installed streamlit on my computer:


pip install streamlit

Before you develop your data application, you should have developed your model, and saved it into a file. You can use pickle for that. It is the standard way of serializing objects in Python. There is one important thing that you should not forget here. You should also save the scaler you used to scale your data set in advance. Do not worry, I am providing you with the entire source code at the very bottom of this post. You can take a look at the implementation details.


I assume you have two files model.pkl and scaler.pkl files to store your model and scaler, respectively. There is one more file needed, which is basically your main program, where you design your Web interface. Let us call it gp.py, short for growing pains. It is also included in the source codes.


Probably, at the beginning of your main program (or, application), you should load both model and scaler files properly.


model = pickle.load(open('model.pkl', 'rb'))
scaler = pickle.load(open('scaler.pkl', 'rb'))

The program also lets users enter an unseen patient's data into the model and the model returns its prediction on this new patient.


def predict_disease(input_data, scaler):
    """get probabilities of each label"""
    user_input = np.array([input_data]).astype(np.float64)
    user_input_scaled = scaler.transform(user_input)
    prediction = model.predict(user_input_scaled)
    prediction_proba = model.predict_proba(user_input_scaled)

    return int(prediction), prediction_proba

Then, you can simply tell streamlit to deploy your data application. The application will be deployed to localhost at port number 8501.


streamlit run gp.py

How you will implement your Web interface to get data from users and present prediction results are up to you. You do not have to be an expert front-end developer to build a data application. I am pretty sure that you can survive this step if you have a working knowledge of how to build simple Web pages.


This is how built my Web application:

Nice and smooth. Now, I can announce this tool to pediatricians. It may be used by inexperienced pediatricians. I have to deploy the application to another server first as it is currently working on my local computer only. This may be a subject to another post though.


Let us see how the application can be used.


First of all, you should provide some data about the patient. As you can imagine, these kind of data were used to build the model. Then, you click the predict button. That's all.


The model predicts the probability that the new patient has GP and displays it. The result on the sample says that the patient has very likely had GP (≈98% probability).


An important note here: Building ML applications and deploying them in the medical domain is tricky. There is a serious process to get through for deploying and using such an application. This is simply because human life matters. Therefore, I added the note at the end "By the way, do not forget that I am just a prototype! Don't take my word for it.".




 

Thank you for reading this post. If you have anything to say/object/correct, please drop a comment down below.


The code used in this post is available at GitHub. Feel free to use, distribute, or contribute.











67 views0 comments

Recent Posts

See All

Comments


bottom of page