hello all today we'll be discussing about how we can deploy a machine learning model by using a flask this is one of the video that everybody most of my subscriber will requesting for that I'm really sorry that I've just put up you know it just got this little bit delayed because I was busy but today I'm going to deploy I'm going to show you how you can deploy a machine learning model by using flask it is a very simple technique all together so make sure that the most important components that you require in this particular session is basically that you should have one model building file that basically means that one py file will be responsible for creating your model and this also involves feature engineering all the data pre-processing and the type of model that I have basically taken is a very simple one because the main agenda of this particular session is to show you how you can deploy the model the second thing is that you need to have one index dot HTML file so that this will basically act as your front-end web app so that any request that you give it to your model which will be in the form of API which we are going to host through flask it will interact with that get the output from that particular API and then there are some stylings also I've included it I have I'm going to put down this whole code in the github and the description will be given in the video in the description box about the URL of this button edit hub length so you can refer it from there then I also have one more py file one is called as apt of ty5 in this what I will be doing is that I will be writing my flask for where and I will be creating the API is like Hawaii yes URL look like like our localhost address slash reading okay and another API is basically slash reddit at Moscone epi so we'll discuss first about model dot dy where we'll be seeing that how the model is actually created so for this I have taken a data set which is called as hiring dot CSV data set I'll just tell you I'll just tell you about this particular dataset what this data set is all about so let us just go and see about the detects that altogether now here you have some fields like experience test score interview score and based on all these parameters you have to provide a salary to the technical guide was comfort this particular interview here you can see that you have something like experience field s coroner interviews underscores for field and this is basically your independent features salary is your dependent feature in experience you have the number of years of experience that you basically have in terms of spread so one feature engineering work that I'll directly show you is that first of all you need to replace man with zero because experience if it is none that basically means the person is having zero experience and all those strings you have to basically convert it into integers now in the tests code you can see that there is also Nance then we will try to fix this by finding the out the mean of all this particular test score and introduced for also mean of if there is no nan in the interview score so you did not do that one more point I want to note on to make you note that that test score usually ranges between 0 to 10 similar interview score also used usually raises between 0 to 10 and based on that you will be able to predict the actual salary that should be given to the candidate now this is a small data set case I just wanted to show you how the main deployment is done not just taken of small data set applied a large linear regression to this and try to show okay so let us go go ahead with the code as said that for the experience I am going to fill the Hemi value with 0 so here it is then for the test score what I am doing and trying to find out the mean wherever I have had the value I am going to replace with the mean and also taking at is in place is equal to true now in the X I have all my independent features let me just execute this 3 line I am also going to convert that first experience filled from string to integer so for that I'm making a function which is say at a scan mode to end here are all my words and I am going to just apply that by using a lambda function in my experiences this is all feature engineering guys don't worry about it you can download this particular code from the github link that I provided in the description here you can see that these are all my independent features in Ian's test run will score score interview score now you can see that this is basically being replaced the Manuel is basically replaced with a mean of this and instead of man here I have zero zeros so that's perfectly fine then my wire which is my dependent feature I'll just use I lock feature and take the minus one feature that is this is my future with respective and after this what he can do is that you can apply a linear regression you already know how to apply a linear regression I guess so then I will do a set after doing a fit my model will be ready after my model will be ready has just used picol dot dub this pickle is basically coming from the pickle library so this pickle helps you to create a pre-compile format model name which will just be like a file which will have a extension like dot detail so here you can see that I am having that particular model dot decal so finally if you want to test it what you have to do is that model because dot load and you can test it but anyhow your model dot tkl file is ready now you make sure you execute this and just put the create your model dot eql fine so this is the one thing that you require now let us move ahead and try to see how we can create our flask environment with where we will be creating our api is where we will be reading this pkl file and then we will give the input to the peak alpha and then finally from the peak alpha will be still get output so for that I have created a file called as AB dot t1 so in this r dot toi I have three import statement one is numpy will be used under to use people and i am going to import use flasks if I am using floss basically to host my model okay so from the flask can be using fast Jason if I had render underscore template render and also template will actually help us to you know redirect to the first home page that we basically have initially then in that particular home page will try to put some inputs and then as soon as we submit the submit button we will get the hope now so far as usual always make sure you write this particular first line of code because you have to initialize the flask app then right pickle dot load open this particular pickle file in in a read mode okay bytes mode so once you read it your model is ready now okay then you have two functions that are created one is defined home basically this is basically the root node where it should root API URL well should go it will directly rewrite you to the index dot HTML file which is minus five which is my first file which will be just like my home page and what I do is that I create my another API that is slash predict and always use this app dot route route because in flask you have to use this in order to create any number of you are eyes with respect to the API okay and the method that you are going to use is post after this what you do is that now you know that during predict what will happen we will be providing three inputs because in my variable my independent feature has three inputs so I will be providing these three inputs as soon as I provide these three inputs this will be you know reddit since this is a post request I will be reading it from request or form not values so each and every values will be available over here I will convert that into an array and finally I will just do model or predict or final features but finally I will just output I will just get the output and not round it off then after this I will return that render on this foot template in index dot HTML I will say prediction underscore test should be replaced with this particular value now where should this value get replaced one let us go to the index dot HTML here you have a placeholder called as prediction on this foot test okay and this predict this particular placeholder will get replaced with the value that are you want to pass it over here that is employ salary should be so-and-so dollars it and this output will get basically replaced with this particular this is one API that I have created I'll also create one direct API where I will just directly a sort pass on the hard-coded JSON so that our naming it has predict telesco yeah so this JSON also we should get we should collect it from somewhere right so for that I have created one request dot d by five Here I am giving the URL and I am just saying that request or post URL and I am just giving the JSON values that experience is two years as for is this this and I'm just printing it are gorgeous as soon as this is you know this is running the command prompt what will happen is that you'll go and in this particular URL gave out this particular JSON values and in this app dot API this will run it over here and you will get down put it so always remember whenever you are creating a flask whenever you are creating a flask kind of a PR deployment there will be one main function okay and then with respect to that you always have to import this flask request JSON file in a template they are various ways to create root directory that is slash which is my default or home page then you know you can create any number of API is based on that you can just write a logical now the main thing is that how do you run this all right how do you run this all now make sure that you have your file explorer like this okay this is your static you have a CSS over here this is your template you have index over here I have app dot T why my CSV file model K model dot a PK you need not run model dot GUI again because I have to take you inside all you have to go is that go to a command prompt okay and just write Python Python and just see which is your model I mean the flask API file this is app dot P wise it's so as soon as you write AB dot E Y in just executed and it's a warning do not use the deployment server but it is fine so that will run in your local machine now just go and copy this and open the browser over here and just go and hit it now this is your home page that you can see away it is very very much simple I just use three fields it is very very simple to see okay just write your years of experience like I'll say I am eight years of experience my test code should be between zero to ten so my test score is nine and my interview score is 8 now it will go and directly predict yourself that you salary should be seventy four thousand six fifty dollars and that is how you have you can do one more way is that I will go and directly and predict underscore API and remember there from the hard code Jason I have already given it but you cannot run it directly so what he can do is that close this okay that is my predictor then what you do is that you can just write Python you can just this particular application a pest control see stop Python and you can just read it request dot e one take some time to execute okay you're getting some error but it is fine what you can do is that okay this cannot be running like a flask API because this is just for the test analysis don't worry about this request dot T why it is just like you can see to your console itself main thing is you just try to run this app for T 1 let us verify once again let me tell you suppose somebody is 12 years of experience is not done well in desk or if he is getting six and into V score is 5 you can do the predict oops I'll just run it once again after t why because my you know I close that app dot py flask API so it is just giving me some error saying that the site cannot be reached but now to be able to he what is the error this now here it is now what I'll do is that I'll just say that the person is 13 years of experience - that person sim test score is 5 into be score is that right now you can see that before I had written eight years of spinosad road test score at nine interviews for as nine give me the output of somewhere around seventy five thousand now it is less than that even though the experience was high so this is a basic example guys you can apply any model use the generate template you will be able to get it that's it it's very simple you can also design your own HTML you if you want but make sure that each and every field is properly handled into it and the type of input that you are giving to the model you make sure that it is properly back so I hope you like this particular video guys I'm extremely sorry for the delay of this particular video of deployment part I'll be uploading more about deployment times so you better model together so don't worry about that please do subscribe the channel if you're not subscribed and I'll see you all in the next video have a great day and never give up keep on learning god bless your thank you