Deploying ML model as Public API using FastAPI and Ngrok in Google Colaboratory

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i am siddharthan in the previous video we have seen how we can deploy a machine learning model as an apa and in that case we have deployed our api in localhost so we can only access it and in this video i want to show you how you can deploy your machine learning model as a public api so public kp in the sense anyone on the internet can access your api through url and they can like use it for their purpose so in the previous video we have deployed our diabetes prediction model which can tell whether a person is diabetic or not based on few input features so the end result we get is once we deploy this model as this public uh apa so we get a url and anyone can post this input feature to this api in order to find whether a person is diabetic or not so this is how all these things work and we are going to do this completely on google collaboratory okay and for deploying this we are going to use this ng rock okay so this ng rock module is used in order to create proxy urls for us okay and for creating aps we will be using fast api library and if you haven't watched the previous video please watch that one i am going to use the same code that we have used there okay where we have created this uh you know api using fast api which is as the end point diabetes underscore prediction so we will be using the same code so if you don't watch that particular video you might not understand what we are doing here okay so i'll give the link for that video in this video description and in the cards as well so please go through that video first okay so the requirements for this video is you need the model file okay so the model that we are working on is diabetes prediction model okay and if you run this collab file you will get that diabetes model so i have this file here which is diabetes underscore model dot sav okay so i've explained in the previous video as well so i have given the same collab file so you just have to upload this diabetes.csv a dataset file okay so you can see this file so i'll also share the link of this dataset file so all you have to do is uh go to this files once your system is connected here upload this dataset file which is diabetes.csv so let me show you how this works so upload it from here so i'll go to my desktop and upload this diabetes.csv okay so once this file is uploaded so now we have to run all the cells come to runtime and run all so this will generate this diabetes model dot sav so this is where we are saving the file so we are using the speaker library and saving this diabetes model dot sav so we have seen this in the previous video as i have told you so this is the model file that we need so go to this option and download this and this is the file that we have to upload in this particular collaboratory okay so i have this diabetes underscore model.sav file so i'll open this circuit collab file where i'm going to build my apa so i'll connect my system and i'll upload the saved model okay so let's wait for it to connect okay now i'll upload this diabetes model dot sav file which is my trained uh machine learning model for diabetes prediction okay and these are all the libraries that you need i'll also give the link for this collaboratory file as well so you can just run the cells so we have the libraries like fast api which is used for creating these apis and uv con access the server and so on and pickle is for loading the saved model pidantic we use it in order to you know structure the format in which we are going to need the data okay so these are all the parameters that we need and uh then we have this uh scikit-learn which is used for this modeling thing and we have this request okay uh which is like for posting the urls and so on and then we have this json so the reason we are using this json library is this data will be posted to us in the form of a json object okay so we have to convert this back to dictionary which we are doing here so for that we are using this json library and then we have this ng rock and nest esync io so i'll explain you why we need all these things so once you run the cells all these libraries will be installed so it will take a few minutes okay and then we are like importing all these libraries here and one of the things that we are importing is this uh corrs course middleware so this is also very important okay so as i told you in the previous video the main idea of deploying a machine learning model as an api is we have to integrate this with ui so we can build our ui with tools like react.js and other ui tools so we have to integrate this api with those ui tools and the reason we are like using this course middleware is so if we don't perform a procedure so this is what you know we do and if you don't run this particular cell it will give you a course error so this will happen only when you are integrating this with the ui so co rs means cross origin resource sharing okay so this is uh to allow the domain to use our apa so when i say this asterisk symbol it will allow all the domains i can also specify like only specified domains so only they can access it so these are like a bit advanced but but you need this when you are integrating this with the ui okay so you can just use this same code but in this case it is not necessary but it is good to know this okay so now i have installed all the libraries that i need now i'll uh run all like run the cell in order to import the libraries that we need and now we are creating an instance of this fast api and saving this in this app okay so i'll run this cell as well and this is for us to give this permission for this course and i'll run this as well so this order should be the same and then this is the format in which we will be passing the data so i'll change this p to capital p so in this format the api will be requiring the data so i'll run this cell as well so we are importing this a base model from this pidantic library again i have explained this in the previous video as well and in this cell we are loading the saved model which is our diabetes underscore model dot sav okay so i'll run this one and we are saving this model in the variable called as diabetes underscore model and we can also load this model within this api so this is the ap that we have created we can also load this model within this api but the problem with that is each time our ap is running or each time our ap is being called uh it will like load the model which is unnecessary okay so yes we are like loading the model before creating this ap okay and this is this is the app that we have created which is app dot app is equal to fast api we are mentioning the method post post means we have to post a few values to our api so these values are nothing but uh these things okay so the input parameters that the model need for prediction and out of these bmi and diabetes pedigree function or floating point values okay so if you open your data set file which is diabetes.csv you can see that this bmi and diabetes pedigree function so these two are in the form of decimal points or floating points hence uh we mentioned this as float okay so bmi and diabetes pedigree function is load and the remaining things will be integer values okay and uh first we are like loading this dictionary so sorry so the data that we receive will be in the form of json and in these two lines we are converting this json data into a dictionary okay and then we are like extracting all these details from this dictionary and and storing it in a list okay so we have this pregnancy glucose bp and so on so these are nothing but the values that we have here so except this outcome so this outcome is what we are trying to predict okay so we have from pregnancies all the way up to age so these are all the variables for it so once we extract all these details from this dictionary we have to pass it to our machine learning model in order to make our prediction this is what happens here okay and if the label is zero we have to say the person is not diabetic and if the label is one we have to say uh the person is diabetic so that's why we use this infills condition okay and if you go to this file where we have like generated this model we have this zero and one where zero is non-diabetic and one is diabetic so that's why we have created a if statement like this okay and this is the input parameter so this is what the api will receive as the data and this is nothing but model input so what we are doing here is we are mentioning uh what is the format in which we will get the data so that is the purpose for mentioning this so the final result that you get will be a url okay and with that url we have to mention this endpoint so that will be our final api okay so i'll run this cell as bill and this is the code snippet in order to create our public url so we have seen that we did the same thing previously in spider where we have deployed this fast api uh in our local host so when you do that in localhost others cannot access it right so only you can save in order to rectify that we have like ah using this pi ng rock but this is more of a temporary solution okay in order to deploy this as a permanent uh url or link we have to go with options like kiroko or other hosting platforms but we will discuss about that in the next video but for now let's try to understand how this can be done google collaboratory itself okay so after running all these cells so run this last cell so this will give you uh your public url so this is the port in which your ap will be hosted in local and that is what we have seen in the previous video itself now let us try to get our public url yeah so this is running in this particular port in localhost and this is the public url okay so i'll copy this link so this is nothing but a proxy url for our localhost so you can run this in your own machine itself but i prefer doing that on google collapse you can also try this on google column now i've like copied this link now we can test our apa so we have seen in this previous video as well where we have implemented this uh you know apa so this is the local host in which uh we were building it on the previous video this is the end point for it and we are like passing these values so these are the values for a person who is not diabetic so if i open this diet is that csv file this is the data that we have passed for this values the outcome should be one and it will it would say that that person is not diabetic so we are going to do the same thing but in this case we are going to use a proxy url that ng rock has generated for us and the last thing we have to include here is the endpoint so the endpoint is nothing but what we have mentioned here so this should be in quotes and there should be a forward slash here so this is very important so i'll copy this and paste it here okay yeah now let's try to understand what we are doing here so i'm importing this json and request library so i'm mentioning this url is equal to this so this is where our ap is being hosted now or it is being deployed and i have to post these values to my apa okay so these are nothing but the same data format that we have mentioned here so all the case and and all the words should be same so we have pregnancy glucose blood pressure skin thickness insulin bmi diabetes pedigree function intake so based on these values our model should predict whether a person as you know diabetes or not so that's what i have mentioned here the only difference is we have mentioned all these values and we have also seen that bmi and diabetes pedigree function should be a floating point value okay so we are mentioning this url and we are posting these values to our url so all we are doing this is now this is in the form of a dictionary right and this dictionary should be converted to a json object because our apa will receive only json object okay so that's what we have done by using this post method so we are we have to like post these values to our apa and here i'm converting my dictionary into a json object using json.dumps and then i'm creating a variable called as response so response is equal to request so this is the request library that we have using and make sure that you have installed this json and request library i've also shown uh you know how you can install these libraries in your machine and uh yeah so request.post so there are like two main methods that we have in ap one is get and the another one is post so here we are using this post method and the parameters that we have to mention are the url to which we have to post the data and uh the json file uh which we have to post so js not the json object which we have to post to this ap okay so this will give you the end result for it for it so okay so response dot text so the final answer that we should get is uh the person is not diabetic because that is the data that we have copied here okay so let's try to run this and see and not only you anyone can use this url and then they can like write this code and see whether a person is diabetic or not okay so i will run this one okay so that is some error uh i can like go to this and see what is that particular error is okay pregnancy okay so i stop this uh deployment okay so you can just click this in order to interrupt this so the reason we are getting this error is so we have changes uh p to capital p right so we have we didn't make that changes so that's why we get this error so i'll this is capital p now i will run this again and i will run this deployment uh code snippet as well again this will generate a new url for me yeah so this is the public url that we have now i will copy this so each time you run this you are like url will change so i'll copy this link address and paste it here okay so i'll replace this one so our endpoint will remain the same okay now let's try to run this one okay again i'm getting an error so let's see four not four not formed so there is some error in which we are posting the data let me just quickly check it okay so the reason we get this error is i have like used two uh forward slashes here so that's the reason and now let's see whether it is working so i'll run this one getting this error again okay so the reason we are getting this error is so this p has not been changed yet so still i get this key error so i'll do one thing i'll stop this implementation and run all the cells from the start so that this change is being reflected so i will go to my runtime disconnect and delete my runtime so when i do this so all the you know files that i have uploaded and all the codes that i run will be resetting okay so now i'll upload my model file again which is diabetes model.sav okay now let's re-run all these cells so this is for installing so you have to install all these libraries again so that is a mandatory thing and then we can like run each of these steps so this is the same thing that we have done in the previous video so the only difference is uh like instead of using this local host we are using this public url so that like anyone can access it and you can access this url uh only when this last cell is running so if you stop this uh like execution of this particular cell this url won't work anymore so this is why i told you that this is a temporary solution but you can use this metal method in order to quickly check whether your api is working or not okay so there is a warning but i think it's it's not an issue so i'll run the cell student app is equal to fast ap and origins model input we are saving the model and let's see whether now it is working or not so this is my public url and i'll copy this link address and let me replace this link so make sure we are like replacing this forward slash as well okay now let's see whether it is working now yeah now it says the person is not diabetic right so it seems it is working well and here you will uh see that this uh response is 200 so 200 means the input has been stored correctly and this is just a basic warning that we get in scale and so the reason we get in scala is we are just passing these values right so we are having this pregnancies glucose and so on so while training we will like mention this feature names as well whereas while predicting it we will just pass this as a you know a list so here you can see so yeah so here you can see we are just passing this as a list so it won't contain this feature name so that's why we get this error but this is not like that much important for us we can also check for a person with diabetic aspects so i'll open the csv file now we are predicted for this so the outcome is zero so it says the person does not have diabetes and now let's check this for this first data point as well so i'll uh open this here and pregnancy is a six so i'll replace all these values with this first data point 6 148 and so on so glucose value is given as 148 and the blood pressure is given as uh 72 skin thickness is 35 insulin values being 0 and bmi is 33.6 yeah 33.6 and diabetes pedigree function is 0.627 [Music] okay and finally the age of the person is 50. so here the outcome should be 1 that means our api should return the result as the person is diabetic okay so let's see what is the result for this so i'll run this uh file again yeah so it says the person is diabetic now this is a public uh like apa url so anyone can like post this data to this and they can get this a result as we are getting okay but this mg rock should run so this uh cell should run so then only it can uh you know it can work okay so the moment you stop this uh execution so this if this particular url for our ap won't work so for uh overcoming this only we will like deploy our ap in hiroko but that is a bit of a work that has to go there so this will be a temporary solution that we can use in order to deploy our api in google collaboratory itself using this mg rock okay so i'll just give you a quick recap of what are all the things that we have done here so first we have installed all the libraries that we need and before that we need to get the model from this particular collab file which is our diabetes underscore model.sav okay so we have seen in the previous video as well and after importing all these libraries we are creating this fast api instance and as i told you this api will work so this api that we have used will work even if we don't include this origin okay so but when you are putting this url in some ui codes and all those things in that case you might not uh you might get a course error okay so that is why we are including this all origins which is mentioned by this asterisk symbol and we are mentioning the format in which the data should be passed and then we are loading the saved model and then we are creating the ap in this step and finally we are deploying this using this mg rock which gives us a proxy url that can use for the moment okay and uh here we have tested this apa so ins like instead of having this python file we will like put this url in some ui tool like react or other ui platforms and then we will like post this data from there so in real time how this will be is so there will be some input fields in a web browser and the user can give these values and once they give this value so this data will be posted to our url and in the back end we can predict whether a person is having diabetes or not using this api so this is how the entire thing works but for this time being we are checking our we are testing this api in this python uh file itself okay so i hope you have understood all things covered in this video so all the things that you have to do is uh save the file from this collaboratory and run all these uh cells in this uh second collab file and copy this link so this link will be changed and you have to replace the link that i have given here okay so that is very important so this link won't work when you are running it because i will close this collab file now so make sure you are replacing this link with uh the link that you get when you run this particular ng rock cell okay and once you do that so this will probably work okay so that's it from my side and i'll see you in the next upload thanks for watching
Info
Channel: Siddhardhan
Views: 18,269
Rating: undefined out of 5
Keywords: Deploy ML model as public API in Python, Deploy ML model as public API using FastAPI and ngrok in Google colab, API using FastAPI and ngrok in Google Colab, ML model as FastAPI, Deploy ML model using FastAPI, FastAPI for Machine Learning model, ML model deployment, FastAPI in Python for Machine Learning, Machine Learning Course, Machine Learning Training, Machine Learning Tutorial, ML model as public API
Id: EUWLdW_i0EQ
Channel Id: undefined
Length: 21min 1sec (1261 seconds)
Published: Wed Jun 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.