How to deploy our ML trained model?

Question:

I am new to machine learning. I’m done with k-means clustering and the ml model is trained. My question is how to pass input for my trained model?

Example:
Consider a google image processing ML model. For that we pass an image that gives the proper output like emotion from that picture.

Now my doubt is how to do like that I’m done the k-means to predict mall_customer who spending more money to buy a product for this I want to call or pass the input to the my trained model.

I am using python and sci-kit learn.

Asked By: selva kumar

||

Answers:

What you want here is an API where you can send request/input and get response/predictions.

You can create a Flask server, save your trained model as a pickle file and load it when making predictions. This might be some work to do.

Please refer these :

Note: The Flask inbuilt server is not production ready. You might want to refer uwsgi + ngnix

In case you are using docker : https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/ this will be a great help.

Answered By: Pramesh Bajracharya

Since the question was asked in 2019, many Python libraries exist that allow users to quickly deploy machine learning models without having to learn Flask, containerization, and getting a web hosting solution. The best solution depends on factors like how long you need to deploy the model for, and whether it needs to be able handle heavy traffic.

For the use case that the user described, it sounds like the gradio library could be helpful (http://www.gradio.app/), which allows users to soft-deploy models with public links and user interfaces with a few lines of Python code, like below:

enter image description here

Answered By: Curious Student

Let’s say all you know is how to train and save a model and want some way of using it in a real app or some way of presenting it to the world.

Here’s what you’ll need to do:

  1. Create an API (eg: using Flask, FastAPI, Starlette etc) which will serve your model, ie, it will receive inputs, run your model on them, and send back outputs.
  2. Need to setup a webserver (eg: uvicorn), that will host your Flask App and serve as a bridge between host machine and your Flask App.
  3. Deploy the whole thing behind a cloud provider like (netlify, GCP, AWS etc). This will give you a url that can be used to call your API.

Then there are other optional things like:

  1. Docker, which let you package your model, it’s dependencies and your Flask App together inside a docker image, which can be easily deployed on different platforms due to consistency. Your app will then run as a docker container. This solves the environment consistency problems.
  2. Kubernetes, which lets you make sure your Flask App always stays available by spinning up new docker container every time something goes wrong in the one that’s up. This solves availability and scalability problem.

There are multiple tools that ease or automate different parts of this process. You can also check out mia which lets you do all the above and also give a nice frontend UI to your model web app. Its a no-code, low-code tool so you can go from a saved model to a deployed Web App and an API endpoint within minutes.

(Edit – Disclaimer: I’m part of the team responsible for building mia)

Answered By: Karamvir Singh