Building a Machine Learning API in 15 Minutes | Coding Challenge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's happening guys welcome to the very first episode of code that a new series where i try to code stuff in a ridiculously short time frame in this episode i'm going to be building and deploying a machine learning api using fast api and scikit learn so what are the rules well first and foremost the time limit you guys were super generous on the community tab and gave me 30 minutes to do this well i'm going to try to do it in 15. second and most importantly i'm not allowed to look at any documentation or stack overflow and no github co-pilot if i do it's a one minute time penalty but the real stakes and the third rule if i fail to make the time limit it's going to be a 50 amazon gift card to you guys so a little bit of background for the guys that aren't so technical what is an api well api stands for application programming interface think of it as the plumbing between different applications in huge organizations they've got a ton of different apis that connect a bunch of stuff together we could use this api that we're about to build to be able to integrate it into a mobile application or a web application and this will allow those different apps to communicate and use our machine learning models ready to do it let's get to it all right let's do this okay so i've got the existing machine learning model so this is a model that's been trained inside a scikit-learn so we are going to be leveraging that so you can't open it it's serialized but i'll share it as well what we do have however is this sample.json file and this tells us how we should be sending the body of our request to our machine learning api so we are going to be leveraging that now the first thing that we need to do is create an environment so i'm just going to inside of my fast api folder that i'm currently working let's make sure you guys can see that so let's create an environment so python dash m v e and v and we'll just call it fast ml for now and we'll let that create so you can see it's creating up there and then we've got to install a bunch of stuff so we need to install well let's activate our environment so dot backwards fast ml dash scripts dash activate and then let's clear that then what we need to do is install that stuff so i believe we need to pip install uv corn which is going to help us actually start our server gynecon which we're going to need when we go and deploy to heroku so we're going to be depleting heroku as well we need fast api we need pydentic and pedentic is going to be used to structure our or pass through our requests and have them in an appropriate file format um we need scikit-learn so i believe we can just type in sklearn and what else are we going to need pandas as well to process our request so let's let that install and then while that's happening let's create a new python file we're just going to call it um ml api and then we while those things are still installing let's start bringing some dependencies so bring in really save this as a python file uh rename a pi and let's just set this particular environment looks like we've installed successfully to pip lists double check that cool so we've got pedantic psychic learn scipy uv corn and gunacon yep cool and fast api all right cool we're looking good so let's create a lightweight api to begin with so bring in lightweight dependencies so we're going to bring in fast api so from fast api import fast api and then we need to create an instance of our app so we'll call it app equals fast api and then what do we need to do so we need to create a decorator to tell our api what the route is going to be so it's going to be app.get initially we're going to change this to post later on and then we need an asynchronous api we're going to define our function now so this is going to be what actually gets called when we go to this route so effectively no explicit route so async def uh we'll call it scoring endpoint and for now we're not going to pass anything through to that uh through to that function it's just going to be a raw function at the moment and then we're going to return just a dictionary so we'll call it hello world all right so this is sort of the first phase so how are we looking that looks okay all right cool let's try starting that so in order to start we can type in uv corn and then the name of our files in our case it's going to be ml api and then the name of our app which is going to be app over here and then we can set hot reloading equal to true so dash dash reload cool that looks alright so you can see that it started up successfully let's open up postman so we know that our api is running on port 8000 so we can go localhost 8 000 and hit send all right you can see that that's working down there so you can see it says hello world right we're looking good can we zoom in on that so you guys can see it a bit better oh okay whatever we got to keep going okay so that's fast api what we now need is pydentic so we actually want to send some information effectively this our scoring request to our api so we need a way to pass that information and this is the way that fast api works so um from pydentic import base model and then over here we are going to create the structure for that so and you can see that our server's still running this is the beauty of setting hot reload equals to true so uh class we're just going to call it um what's called scoring item actually let's just call it item and then we are going to subclass that pass through the base model and then here you need to structure so this is effectively having a typed item so we need to structure it using this so we are going to be grabbing each one of these items or each one of these fields from our machine learning model so in our in the original machine learning model and i'll share that code as well i've got four columns in there so you're at company employee satisfaction position and salary so these are the four different columns that we're going to be expecting inside of our scikit-learn model now what we need to do is structure this in a format that says what types we're expecting so use a company we already know that's going to be a float so we can say that this is going to be a float and let's just comment that it out and then employee satisfaction it's going to be a float over here we can comment that bit out uh position is gonna be either manager or non-manager so what we want that to be is a string str and then salary i think salary was ordinal which means it can be an integer so i've got a floating point but this can be uh let's set it to int for now so basically what we've done is we've created a structure for what our scoring requests are going to look like so when we make a call to our api we're going to be sending it in this particular structure now we need a way to actually pass that through in our endpoint so first things first we want to change our api method from get to post because that's just good practice and then what we're going to do is we're going to capture our item and that item is going to be well actually no we should change this so this should be let's call it scoring scoring item so we're going to capture our item which is going to be the actual request body so we're going to send a json object through to our api so we are going to send that through and we're going to be able to work with it with the value item now we're going to type explicitly define a type so we want it to be of the type scoring item we can pass that there and then rather than returning hello world let's return back item and what we can do i'm just checking we're still recording and what we can do is let's go test this out now so our method should be post so if i send this it's going to fail because you can see down there method not allowed so if i change it to post and send so right now it's saying that the fields are missing and that's because we haven't actually passed through a body so what we can do is go to body go to raw go to json and then we're going to send through a request so let's go and grab this initial request here this json object paste that in and let's get rid i think we can leave the comments so now if we send this through uh property name and closing double quotes what are we doing let's just get rid of this set that to an integer and beautiful okay so we're returning our items so you can see that we've successfully gone and passed the stuff that we're sending through to our api and our api is returning those values back now what we actually need to do is we actually need to bring in our machine learning model which is currently stored as a pickle file so it's serialized using pickle so what we need to do is import pickle so we can uh do that so we have it installed let's just stop our api to begin with so pip list we don't have people let's install it so pip install pickle [Music] not find a version but [Music] python dash m pip install oh no we're gonna have to check documentation on how to install pickle can we just hit from import pickle wait we might already have it in the environment but we don't have it in our custom i environment we're going to have to go to documentation guys [Music] i only got five minutes left oh no all right so let's just try this for now um with open rfmodel.picor and we're gonna read it as a binary and then model equals pickle dot load um f and then we also need pandas so import pandas ah why those pickles difficulty and saw okay all right so then what we want to do is we want to pass our items so we're going to create a data frame equals pd.data frame um we're not going to make this uh so we need to go list no item and then we can access it as a dictionary and then we can grab dot values to grab just the values out of that dictionary and then we need to specify columns to be able to pass through and effectively create a lightweight data frames columns equals uh items dot dict dot items no dot keys what am i saying and then we can make a prediction so y hat equals uh model dot predict and then we can pass through our data frame and then ideally what we want to return back is y hat let's put it inside of a dictionary prediction y hat let's convert it into an integer it could be just a single prediction all right let's restart our api it should be item over here okay no issues uh what is it so uv corn ml api and then app dash dash reload okay no issues send it internal server error what's happened pd is not defined we not imported pandas as pd okay boom we're getting that prediction okay i'm not sure what the pickle issue was but that is successfully we've successfully built the api now what we want to do is actually go and deploy it so in order to deploy we've got to create a bunch of files and we're going to deploy using heroku so um we need a proc file and this defines how we're actually going to start up so we want to go web and then uh so gunner corn dash w to specify how many workers dash k to specify what we actually want to run so it's going to be uv corn dot uh what is it [Music] got workers dot uv corn worker and then we need to run ml api app so that's our proc file defined then we need to freeze our dependencies i'm concerned with the pickle thing that might cause some issues um pip freeze requirements dot txt right so that's now there cool and then we need a runtime this is just going to tell heroku what type of runtime we want to use so we can type in python-version to get that so it's going to be python 3.9.7 python 3.9.7 and then we also need a git ignore file and we just want to ignore underscore underscore pi cache the ach and fast ml we don't want to take in our environment cool so that's now ignored we also don't want uh we do want the model to go up we don't want sample.json to go up okay so now we can create or we'll want to commit that so issues there all right so we're going to create a new git repository so get in it get add all git commit m initial commit and then we need to log into heroku so heroku login this might take a little while oh no we've got a minute left um so we need to log in cool we're logged in and then uh what is it so heroku create cool so that's going to create our api and then we want uh git push roku master fingers cross this works all right i'm going to pause the timer all right so we're uploading right now it's building the source but we can't really do anything else so i'm gonna let this run and then [Music] so right now what's happening is we're actually uploading our api like we can't really do very much right now so i figured look i've given myself the benefit of the doubt we've got 35 seconds less left to test if anything goes wrong we're screwed it's going to be a 50 amazon gift card but uh fingers crossed we can get this deployed in time i'm a little bit concerned that i couldn't install people using pip install people i don't know i'm gonna have to dig into that so right now it's like compressing you can see things are happening but 35 seconds left oh lordy but it was at least running so that's a good sign okay so right now it's actually uploading so this is running a build so over here you can see that we've got this link over here that is the link which is going to allow us to test out the deployed model hopefully pickles just like installed i'm gonna have to go check that out after the fact but that was intense okay it is deployed i'm a little nervous all right time is on let's go so we've got to test it out so we can copy this and fingers crossed we should be able to plug this into here detail not found no that is our app method not allowed all right so that's using a get request we can check our logs heroku logs dash dash tail wait method not allowed so close all right we're clearly going over so let's quickly debug so all right so we got to a post function all right so that's clearly going to be an amazon gift card here you go guys we got close but oh so close uh did i have spaces i had spaces there you go it was actually working okay so i had spaces at the end i'm gonna give give you guys benefit of the doubt i'll give away the gift card but that is it actually deployed so you can see they've successfully gone and deployed it to this link over here which is exactly the link which was from over here so it's successfully deployed you can see that we've made that prediction so if we actually go and change these values if i change this to 15 years of the company we're getting a different prediction now so zero means the employee hasn't churned this is a churn model so we can change our employee satisfaction value so i can set that to uh we can change the position set that to manager and you can see we're actually capturing those different predictions so salary is an ordinal value between one to five you can see we're making predictions so if we go and drop the years at the company to one you can see that this is saying that's going to churn in that particular case all right not quite 15 minutes but we got super close anyway the amazon gift card's there first one to get it gets it thanks again for tuning in guys hopefully you enjoyed the first episode of code that i'll catch in the next one peace thanks so much for tuning in guys hopefully you've enjoyed this brand new series if you want to see some more coding challenges or if you want to see the code that series continued drop a comment below and let me know what challenges you think i should be giving a crap thanks again for tuning in guys peace
Info
Channel: Nicholas Renotte
Views: 72,866
Rating: undefined out of 5
Keywords: coding challenge, python, machine learning, fast api
Id: C82lT9cWQiA
Channel Id: undefined
Length: 20min 3sec (1203 seconds)
Published: Wed Aug 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.