Creating APIs For Machine Learning Models with FastAPI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn how to easily turn our machine learning model into a usable API so let us get right into [Music] it all right so we're going to learn how to turn our machine learning models into apis using fast API in Python today now the reason behind this is that often times when you train a machine learning model and it performs well it makes good prediction you don't want to just hand that model to the user maybe because you want to sell it or because the user doesn't know how to work with the model not everyone is a computer scientist or programmer not everyone knows how to use the model and load a data set and so on so usually what you do is you make the model part of an application so there's a feature or there's an endpoint in your application that uses the model or in the simplest way you just provide an API interface to the model so API already the stands for interface so saying API interface is redundant but you can provide an interface in the form of an API to your model and one very easy way to do that is using fast API this is what we're going to do in this video today uh what we're going to do specifically is we're going to train a simple machine learning model to classify handwritten digits and then we're going to make that model accessible uh using an API so this is going to be a very simple example here for this we're going to open up the command line and we're going to install four packages using pip so pip or pip 3 install and we're going to install first of all of course fast API then also uicorn to run the application then also pillow to work with images and also scikit-learn to do the machine learning part once you have all of this installed we can go ahead and create a python file to train the model so let's call this train model.py and here we're just going to build our model this is not going to be any part of the API yet this is just going to uh load the data set and train a simple random Forest classifier on the data set so we're going to say here from SK learn. data sets import uh fetch open ml then from SK learn. Ensemble we're going to import a random forest classifier and from escale learn. model selection we're going to import the train test split so that we can have a train and a test set so that we can also evaluate the performance of our model and at the top we also want to import pickle for serialization because of course we want to ort our model so that we can use it in the API so the first thing we do is we say X and Y is going to be equal to fetch open ML and the data set that we're going to use is the mnist data set mnist 784 I think that's 28 * 28 because of the pixels so 784 pixels um we're going to say version equals 1 and we're going to say return X and Y true so that we get it as a tle X and Y here all right so that is our data and now we can split that into XT Trin and X test into y train and Y test that is going to be the result of the train test split applied onto X and Y with a test size of 0.2 so we have 20% test data 80% uh train data and then our classifier will be a random force classifier and I'm going to provide here n jobs is going to be equal to -1 so that we can use all CPU cores for the training to speed it up because otherwise it's going to take quite some uh time and then we're going to do clf doore X test and uh y test actually first of all I forgot to do the fitting so clf fit X train and Y train and then we're going to evaluate this on X test and Y test and finally after this we're going to say with open um mistore model pickle in writing bytes mode as F we're going to say pickle. dump clf into uh into into F there you go so the file stream F and the classifier clf so we can run this now this is going to train the model to do the handwritten digit classification this is not anything new on this channel we have multiple videos here or I have multiple videos here where I show you how to do that I have I think two or three videos where I show how to do specifically handwritten digit recognition and we have many more machine learning tutorials this is just a simple part now we're going to take this model once it's trained and exported and we're going to turn it into an API using fast API and then we're going to also build a simple HTML page that is going going to work Standalone of course uh in a real application you would have a front end or something we're just going to use a simple HTML file to access the API but you could also do it in a command line if you want want to so I'm going to run now or I'm going to create now a python file that I'm going to call Main py and here I'm going to import the following things first of all IO which is a core python package second of all pickle which is also a core python package then numpy as NP I think numpy should be already installed as part of pyed learn if not just install numpy as well then we're going to import uh pillow so p. uh image actually no image Ops sorry and then also from pill we're going to import image or actually let's just go ahead and do do it like this um import pill image and import pill image Ops and then we're going to say from Fast API import fast API file and upload file now fast API works with type hinting so we need to specify what kind of file types uh what kind of parameter types our parameters half data types parameters half uh and then we're going to say from Fast api. middleware doc uh C RS which stands for cross origin uh resource sharing and this is now only important for the demonstration in this video because I'm going to send a request to the server that is running from an HTML file and it's not going to allow it because it doesn't have the proper origin but to circumvent all of this I'm just going to allow all the origins all the methods all the headers and so on which is why we need the course middleware all right so these are the Imports then we're going to load our trained model which is the Mist model we just trained so Mist model pickle file reading bytes is f and then we're going to say that the model is equal to pickle load and we're going to load the content of f all right so then let's create our API we're going to say app equals fast API very similar to flask here then we're going to say app add middleware and here we're going to now add the Cs middleware and we're going to say allow underscore Origins is going to be equal to a list containing an asterisk then I'm going to copy this and we're going to change this here to uh credentials first of all is going to be equal to true I don't even know if I need all of them I just brute force it here so methods is going to be equal to asterisk as well and then finally we had uh headers as well there you go so this is just so I can send requests using uh the HTML file now the actual endpoint the actual API uh function here will be the following we're going to say app so at app like a decorator post and we're going to have the uh URL pattern here predict image and the function here is going to be an asynchronous function so async Define the function predictor image and this is going to take a file as a parameter which is going to have the type upload file and we're going to say this equal to file and then three dots here so this is just a type hinting this is important for um for fast API now in Python itself type hinting is not important other than for readability and for other tools but in in the case of fast API the typ hinting is actually relevant and necessary so contents that we get here from this file is going to be equal to await file. read so we're going to rate a wait for the file to be read this is then going to be our content and now we're going to extract the image from that content we're going to say the pillow image is going to be equal to image. openen and now we're going to get the by stream io. bytes iio cont like this and we're going to convert this to grayscale so to an L in my case the images that I'm going to pass are going to be uh already grayscale of course you need to say pill. image not just image um and I think does this work why is it [Music] underlined uh actually like this there you go yeah like this so we open the by stream of contents and then we convert that to grayscale that's what we're doing here now since the way that uh psychic learn processes the images and the way uh pillow processes the images is slightly different we need to also invert it so we need to turn black into white and white into black and so on so we're going to say pillow image is going to be equal to pill image Ops um do invert at least if you want to have the digits like this otherwise an easy way if you don't want to invert is you can just uh draw them white on black this is also possible so image Ops and then we're going to do invert the pillow image then we're going to say pillow image is equal to pillow image. resize in the case you don't pass a 28 * 28 pixels image which I'm not going to do I'm going to pass the proper size already uh you want to resize this and you want to do that with antialiasing so pillow image anti-alias why doesn't this work anti-alias shouldn't this work I think it should let's see if it produces any problems when I run this um then we're going to say the image array is going to be equal to numpy array of the image so of the pillow image and we're going to reshape this like this and then we're going to feed our image into the model and get a prediction so we're going to say model predict image array and then we're going to return the following dictionary the key is going to be prediction and the content or the value is going to be the integer of prediction zero like this all right so this is now our uh API application before we can use it though we need to somehow uh write an application or a website or an HTML page that uses it of course you can also use it with curl but we're going to create an index HTML file which is going to be our front end here um in an actual application you would have a separate front end application that calls the API or you would have some tool that calls the API but we're just going to keep it simple and create an index HTML file here and um what we're going to do here let's call this image classifier or actually digit classifier uh what we're going to do is we're going to have a uh form or actually we don't need a form even we can do everything with JavaScript or we have to do everything with JavaScript we're going to say input type text is going to be uh an input or actually not text sorry this is a file obviously where uploading an image not text input type file will have the ID uh image input and we're going to accept all kinds of images so image slash and we don't want to limit ourselves to a specific type here so we're going to just go with an asterisk and then we want to have a simple button and this button is going to call a function a JavaScript function that we need to write to upload uh and send the image and to also process the response for the given image so I'm going to say button on click is going to be equal to upload image which is a function um and the button will have the text upload or let's call it classify like this and then below this button we want to have an element with the ID prediction result and by default it's going to be empty but when we press the button when we call this function it's going to fill it up with information provided that the request succeeds so we're going to now write the JavaScript section here we're going to say script typee um text JavaScript and we're going to Define our asynchronous function here async function upload image now we need to use Cy bracket since we're not in Python I'm going to say that the input element that we're interested in is document. getet element by ID image input so we're accessing this particular input here and what we want to do is want to say if input. files zero so if we have a file here then of course we're going to uh process it otherwise what we're checking here is if there's not such a file we're going to just do it simple and call an alert please select a file to upload and we're going to return otherwise we're just going to say constant file is equal to input files zero so we're going to get that file which is existent and we're going to say form data is going to be equal to new form data and we're going to say form data append pend file file so we're sending the file to uh we're creating a form but we're doing it in the background asynchronously so we're not going to actually post and then reload the page or be redirected we're going to do everything here in the background which is why we create form data we append the file to the form data and then we're going to try to send the request to our API so we're going to do try and we're going to say const response is equal to a wait Fetch and we're going to now access in our case since this is running on Local Host HTTP colon1 1271 so Local Host on Port 8000 which is the default fast API port and the Endo was predict image which is this one here so that is the URL and what we want to do here now is we want to specify the method uh method post and the content the body is going to be the form data so this is what we're actually passing what we're actually posting to the endpoint that is what we do now what we do is we say the result is going to be equal to await response. Json so we want to get the Json version of the response remember we get a diction here with prediction and then we want to do document. get element by [Music] ID prediction result which is our paragraph up here that should display the result we're going to say text content of this is going to be prediction and then dollar and uh curly brackets result. prediction so since this is a Json object it's going to have the field prediction uh we're going to get the prediction and display it here in uh in the paragraph right so in any case of an error so if some error happens here we're going to catch it and we're going to just log it to the console as an error message so we're going to say error and then error like this and then we're going to say alert failed for some reason doesn't really matter and that is now our logic in the front end the most important part actually is just getting some image data and posting it to the back end so this is actually the whole Magic this is just sending this to the back end everything else is getting the data and showing the result here so that is the the magic here sending the data to the actual endpoint so I can run this now and if I didn't make any mistakes of course I cannot just run it like this we need to open up a terminal uh can can I open a terminal here there you go current now I can say uvicorn let me just see the proper command here uvicorn Main and then also the name of our app main is the name of the file and app is the name of the app this is what we created here the fast API app and then D- reload to don't have to constantly um restart the application when we make changes and then I want to open up my index HTML file in the browser so there you go we have our index HTML file here now I can browse for a file and I can open for example digit one and classify it and you can see it has the prediction five and you can see it is actually a five so digit two should be an eight there you go eight and digit three should be a three there you go three so yeah we turned our uh we turned our machine learning model into an API that we can use in the browser of course you can make this much more professional by using uh a proper front end and a proper API with different functionalities and so on but this is how simple it is to just take a pickle file the exported version of your model and to put it into an API because actually the whole API is this and you don't even need that if you don't want to just uh play around with it in fact this is not recommended in an actual application I just did it so that I can send the request but the whole Magic is this here and loading the model that's it and once you have the all you can easily do that and then you just have to write some front-end code to actually use it so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Info
Channel: NeuralNine
Views: 15,143
Rating: undefined out of 5
Keywords: machine learning, artificial intelligence, python, ml models, fastapi, web app, api, machine learning api, deploy, deployment, deploy machine learning models, machine learning fastapi
Id: 5PgqzVG9SCk
Channel Id: undefined
Length: 20min 5sec (1205 seconds)
Published: Mon Apr 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.