Deploy ML model in 10 minutes. Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you've built a machine learning model it can swiftly predict whatever you've trained it to predict but that's just a half of the story in order for someone other than you to be able to use this model you need to make it accessible to general public or to other pieces of software that could interact with the model programmatically sending data to that model and retrieving predictions in other words you want to deploy the model today we'll learn how to do this using Docker and fast API we'll create an API for this model and test it through a interface and programmatically first thing we'll need is a Docker engine and it's real easy just go to the official website and I'll post the link below and download the version for your operating system then install it and you are ready to go it provides a cool app that will give you an ability to control your images and containers but we're not going to use it we're going to do everything through the command line next we want to save our trained model as a binary so that we could place it inside our container which would then load it and use this model to predict on the incoming data so here I have a really simple script for a classification model on the iris data set or it I don't know what's the correct name for this so basically what it does it uploads the data set from Psychic learn then it uses a random force classifier model to train on the data and then we use the job library to dump the model or to save it as a model. job file so if we run this script it's going to train this model and save it to our dis now we're not going to need this script anymore it's not going to be used in our container so I'm just going to close this and without even saving it and I'm going to close the python interactive window so the only thing we need here is this model. job file with real life data of course there going to be a few more steps with your data pre-processing and saving additional artifacts for later so that in the inference pipeline your incoming data could be transformed accordingly but for our proof of concept this is not required now let's create our API I'm going to create a new python file and I'm going to call it server we're going to need three libraries the fast API to create an API job lip to load our model and NP so the first thing we want to do is load our model and for interpreting our predictions for this particular model the iris model we're going to need class names and as you can see GitHub copilot is really smart it's already understanding that we're want to create an app using the Fest API so I'm just going to hit tab here and uh create an app Gap method this decorator tells test API that the function right below is going to handle all the requests coming to this path which is going to be the root of our web app it will be really simple it will just return the message the name of our API now let's make a function for sending data to the model so it would predict on that data and we'll create an endpoint predict with the post method and the function is going to receive the data which is going to be a dictionary it will extract features out of that dictionary make a prediction with a model map the prediction to the classes that we have defined here so it would return something meaningful other than 0o one or two and return this response and that's it this is our whole API nothing else we need in here well other than maybe some documentation to this predict function just to give us an example what the request should look like I'm going to create an app folder and place the python code and the model in there because this is our server and everything required for this server is inside that folder and one thing I forgot we're going to need the requirements as well so here in the rout let's create the requirements.txt just make sure that libraries versions will be the same as the ones you've used when creating your model specifically the psychic learn because a different version of psychic learn in the container may not load the model correctly now we need a Docker file our application needs some kind of environment to run in specifically some operating system and with the python version in there and the requirements that we have just specified because currently it runs on your local machine where you have all of this installed but when it's going to run as a deployed container somewhere on the server that server might not have all the necessary requirements so in the docker file we're going to specify which is going to be the operating system the python version the requirements and so forth Docker file without any extensions just like this everything we're going to write here are basically going to be just commands that are going to be executed when the container is created each subsequent command will stack on the result of the previous command and they have to be sequential First Command is always going to be the image that you are going to use to create this container this image is going to be pulled from the Docker Hub there are a lot of different images that satisfy different criteria in our case we're going to use Python 3.11 which is going to pull an Alpine distribution of Linux with python 3.11 installed this next one is just creating a code directory inside our container and assigns it as a working directory since our container is an isolated environment we're going to need to copy all the necessary files that we need the container to execute so let's just first copy the requirements and we'll place them inside the code directory that is going to be created with the previous step now after our requirements are copied we want to install those requirements so we're going to run the PIP install those requirements inside the container and copy the application folder inside our working directory code second to last is going to be Expos some Port because again this is isolated environment and in order to interact with this we need to expose some endpoint to where we can send the data or we could interact with this container and the last one is going to be the command the container is going to run after it is created uicorn is a server Gateway interface it's the binning element that handles the web connections from the browser or API client and then allows Fest API to serve the actual request it also allows you to properly spawn many workers and scale your deployment properly the app. server colon app is really simple it will just run the server.py file from the app directory and initialize our API which in this case is also called an app when we set the network to host a cont container will share the host Network stack and all the interfaces from the host will be available to the container and the last one is going to be the port which we have exposed here in the previous step this means that a request to a container on Port 8,000 will be forwarded to the application bound to Port 8,000 inside the container and this is literally all the code we're going to need to server our model through an API inside a Docker container and a good idea is to always add a read me file so I'm going to just paste whatever I created before which explains everything how to operate this API but we're jumping ahead so let me close this for now I'll put a link in the description to the repo where this code is going to be located now let's spin up our container and use this model through an API we're going to need a terminal make sure that you CD into this route where this app folder Docker file and everything is located before running the container we need to build an image for this container so the command is Docker buil minus t and any name that you want to give to this image I'm just going to say image name and then dot at the end once you run it especially the first time around it's going to take some time because you with this first line of code here you actually download the uh operating system with python version in there and it may take a few minutes and then it will execute all these uh lines one after another and you can see each step uh is being logged into the console so now everything is fine we have successfully built our image now we want to run this image the command is run so uh name of the container that you want to create I'm just going to say container name minus P which is Port 8,000 and the image name is the name of the image which you have created in the previous step and it looks like we have a mistake in our code on the server side so let's go here and append a path to the model. jobet as an app folder because this is actually where it's stored now once you save it or make any changes whatsoever to your code or to the docker file you have to build it again so I'm just going to say Docker build T image name okay and now we can create a container and since we've tried creating a container before this container name that we are trying to use once again has already been used and we cannot use the same name we either have to go into the docker application and delete this container or you can also use the command line for that as well okay so since we have deleted this container we can run this command once again not changing the container name here or otherwise we could have called it container name to or any other name and a second container would have been created okay very good it's telling us that uicorn is running at your local host Port 8000 copy this URL open a Chrome browser and you will see the root of your application as you can see here it tells us the message that we have included into the get method Aras model API now we have also created a predict endpoint if we add a predict here we're not going to see anything because our predict function expects a dictionary and we have not sent any dictionary here uh so there are a few ways you can interact with this API first and easy way to test this API with a web interface is going to the docs endpoint we have not defined this endpoint in our program but this is the default endpoint that goes within the Fest API so any application you have created using fast API is going to have this docs where you can see all your methods so this is our get and post method and you can test them out you can just open it here and send your data through this request body field now let's go back into our code and here we have a convenient example this is how our data is supposed to go to the model as a dictionary with a keyword features and a list of features so I'm just going to copy this and paste it in here execute and it seems that we have some kind of error okay and if you go back into your terminal where you have launched your container you can see the details and I can see the error so one of these brackets has to be right here because we cannot reshape a list we have to reshape a numai aray so all we need to do is replace this bracket over here and we're going to have to close this container and uh build it once again and then run it we're not going to go into the docker app to remove the container we'll just give it a different name so container name two good thing this error happened because here you can see a simple example of debugging this application okay the container is running I'm just going to refresh go to the predict method try it out paste our features in here and click execute okay very good response code 200 this means everything happened correctly and this is our predicted class virginica but the real intention usually for model deployment is to use it programmatically because say you have some data in some database that gets collected there and every once in a while you need to send some data to some model for prediction uh this is where API helps now let's just create a simple inference script welcome call it client.py and we'll use this to send the data from python to an API and receive response let's define our data and just going to use the same sample we've plugged into the web interface the endpoint location convert the data into Json format and send it to this URL as a payload using the request library and store the reply in the response variable and I'm going to print this response let's check it out and Virginia of course you may have much more data say this is an extract from your D database that got collected overnight and you want to classify it so just make a simple Loop to run through these data points create a payload out of each one of them send it to the model and then receive your predictions and store them in the predictions list and here are your predictions you can also interact with the model via curl request and here I have an example in the read file so just through terminal paste that request with your features and the URL of the model and it's endpoint and receive your predictions we have success y deployed a machine learning model into a container created an inference API and made it runable on our local machine and this is a good start deployment to the cloud involves quite a few more things like setting up AWS or kubernetes on the digital ocean writing scripts that interact with your repository and push your code onto the hosting environment scalability issues and other things if you want to dive deeper into the subject below I will link the best corsera and urmi courses and stay tuned [Music] hey
Info
Channel: Danil Zherebtsov
Views: 11,072
Rating: undefined out of 5
Keywords: How to become a Data Scientist, Learn Data Science, Machine Learning, Python courses, Learn python, How to learn machine learning, coursera, udemy, software development, how to become a software developer, big data, data, data analysis, data science tutorial, data science tutorial for beginners, data science with python, how i would learn data science, machine learning model deployment, docker, FastAPI, model serving, model inference, machine learning engineer
Id: vA0C0k72-b4
Channel Id: undefined
Length: 12min 41sec (761 seconds)
Published: Wed Oct 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.