Hello everyone. Now that we have everything prepared and file organized, it's time to dig into flask code and create the back end for our application, the flask code is a bit longer, so we are going to split it into two videos. So in this as well in the next one, we are going to implement the flask back end for our application. Okay, to start the flask application, we need to create a separate Python file called app.pi. I've named it by convention, but you can name it whatever you want. And I've also imported dependencies that we are going to need.
Let's go through them. At the very beginning, we have same libraries that we had in our notebook. So we have iOS library, which is used to locate files on the disk for our neural networks and session creation. We are using tensor flow and for vectorization and matrix operation we are using NumPy. The big goal here is to load the train Set vectors and training image paths. This config import is the file that I've used to organize all my hyper parameters for the project.
You don't have to do that, but it is easier for later reuse for different placements in the project. From model dot p y, we are going to import our network class image search model and from inference dot p y, we import to the simple inference function. Lastly, we have imports for the flask application. I imported flask which is the main clause in the flask library, and it is used to create a new class applications. Then we have request, it is used to get parameters from the post request and send the get request. Render templates is used to render the HTML pages when a user enters a specific URL.
Sent from directory is a very useful method used to expose uploaded files to our application. In other words, we are using that to locate a newly uploaded file. The first thing to set up for the app is the root directory, we want the current directory to be the route as well. To do that, we define the variable called app root at brute equals two, oh s dot path dot their name and provide eyes dot path dot x path of the file. Let's explain this slide. Always dot path dot dir name takes only the directory of the path that is given to it.
And the ies dot path dot apps path gets the absolute path of the file provided. This underscore underscore file underscore underscore is the built in Python variable, which reference the current py file. The next step is to define The model type model equals image search model. And it takes arguments value from the config file. The first one is learning rate equals CFG dot learning rate. If you take a look at the config file, there is learning rate.
Then we have image size, the value of the variable is taken from the config file as well. Lastly, we have the number of classes arguments, also taken from the config file. And, and we are done with this step. The next one is to define the session, right session equals TF dot session and run all global variables. The model is defined and initialize the open session, but it is random. In order to use it, we need to load the checkpoint and use all those pre trained weights.
Define saver equals to tf dot train dot saver. Now, we are going to use the table to load the model right saver dot load and provide two parameters. First one is the current session and the second one is right screen saver slash and go to the saver folder and there is the newest checkpoint, or the one that I provided for you. So in my case it is model ebooks five. The next thing to load is previously saved train set vectors. We opened hemming train sets vectors dot pickle and open it in read binary mode as F to be sure let's check if the file exists in a root directory.
And yeah, as you can see it exists here. Set train vectors equals to pickle dot load of F copy the training vectors loading code and paste it below. We'll modify this code a bit to load the training Sat tests change the file name into the file where you saved all paths. For. For me it is trained image pickle dot pickle. Now we are going to now that we are done with the whole setting part let's define the app itself.
Type app equals flask is takes pythons built in named variable as first argument to define the name of the app. Secondly, we'll provide the static URL path was to slash static. This static URL path is here to define where in the apps root folder the app should look for static files. Let's define our first flask function. This function will help us to render homepage of the app. When you defined function in the flask application, you need to use decorators if you haven't used them before.
They are there to provide Add more functions to the method function they are connected to. In Python, they start with ADD symbol, and we write them above a function name. In flask, we are using this to define the location of the app connected to our function right at app dot route, and provide just slash in quotes. This route indicates route to the place that the user should visit and type to execute the particular function connected to that decorator. This single slash means just the domain or IP in our homepage. define the function below.
Normally, I will call it just index because it is index dot HTML, and no arguments stated inside the body of the function just right return render templates and in quotes, index dot HTML. Now that the flask application will serve For templates, another word for HTML files inside the templates folder. Okay, I'll cut the implementation here. If you have any questions or comments, please post them in the comment section. Otherwise, I see you in the next tutorial.