And welcome back that, of course, in this section will talk about something or form a project that
actually is quite popular and in demand.
And that's how do we actually deploy our computer vision models and one of the many ways to deploy,
actually, I'm going to record a whole section on production deployment later on.
However, the easiest way right now is to create a rest API that allows you to send images to that API
access point, and that API then returns its class or whatever you wanted to do with that image.
You can actually turn the entire image if you were doing something like low light enhancement or neural
old style transfer, or we can just give you an object detector overlay, or it can give you the class
of the image.
So let's take a look at how we can use Flask, which is a Python micro web framework for micro sites
that we we're going to use that to create, firstly, an API.
So let's create the API civil opening notebook 73 here, and we'll begin.
So before I begin, actually, I just wanted to mention a bit of what flask of Flask is a web framework
development library.
It's a very, very good one.
I strongly preferred the jungle.
Even though Django is better, Django makes things a lot more complicated than it needs to be, in my
opinion.
And Flask is actually quite easy to get up and running.
It's quite easy to understand how it's how it's structured, how the templates work, how everything
kind of goes together.
And it actually has a lot of advanced features.
So Flask is pretty much capable of almost everything Django is, except maybe some more advanced functionality
and management tools.
So that's it for Flask.
And now we're also going to need to install and grow and grow up allows us to access those local ports
here on this call out machine on the internet.
So which is what we're going to be doing, exposing our API?
So let's get started.
So clearly does my repo here with the flask up, then we install and grok here pip, install, flash
and grok.
Next, we need to actually download the grok system for Linux, so we don't want that file here and
then we need to install it.
This takes about 10 11 seconds to install and know what I need you to do is actually put a link in the
notebook for you guys to sign up for and grok accounts.
Can you see which gets better?
But Google is quite quick.
And then navigate to getting to your token and you just put exclamation mark and grok or token.
And this saves your configuration.
You took into a configuration file here.
I've already done it, which is why I've run this number before and I've had my could my token here,
so you guys can't access it.
So in the meantime, let's just put a link here.
This is the signup link.
So once we do that now, we never get back into the directory and we're ready to get started here.
So Flask is quite easy to use, so all you have to do is just import your libraries that you're going
to be using.
So we're using are you adjusting the torch vision for the model because we're going to be loading a
pre-trained image that pie PyTorch model?
We import pills and image processing processing tools.
We import different things on flask to request that you solidify.
And we also use flash and drop to run with ink.
So we declared up here optical flask on the school underscore name, underscore underscore.
Then we have run away and grow the app that we named him.
Next, we need to load our pre-trained model, which is what we do in these three lines here, and we
set the model to evaluation mode.
And now we have a helper function that allows us to take an image that we are uploading from an outside
the client and basically transform that that image to running these pre-processing functions so that
then we can now send it to our we use it with another get prediction function.
So that takes the image that we get in bytes, transforms the image into a tensor and then passes that
tensor to the model.
Then and then it returns to Class Hill.
So that's quite simple.
And now all you need to do in Flask Flask basically runs is to arrest API takes requests.
So if we send a post request to it with the image and it sends it to slash predicted ABC, the local
slash predict, you can send a file to our server.
And once it receives a file, it executes this function here and it returns a class.
So let's see this in action.
So let's run this block of code.
It takes maybe about five to 10 seconds to actually quicker.
So it's running here on this link.
That's so API link and you can see we just need to go to to visit the safe site because it doesn't have
an SSL certificate.
And this happens within.
You have to refresh and then we go.
So no starts running is just this is the it's not an actual webpage yet.
It's an API.
So now to accessto API, what we need to do, we need to open a new cloud notebook to open this one.
That's our client.
And let's download test image here of a bird.
And let's get our previous and Gerlach link here pasted into their.
Keeping this stash predict and let's see if we get to class, and there we go.
So we've got the image here.
So this image that you've downloaded want to visualize the image quickly?
We can see it here.
This cute little bird.
So we send this picture out to the EPA and returns here.
And so, no, you can actually post this from any machine across the world using this link, using any
requests or any any sort of like the EPA tool interfacing tool we can do to any language you want.
Or you can use something like postman to test these APIs and you get to know we just don't need this
line here, but we get the class in return.
So we get a class I.D. and the image, the class name lorikeets, a bid, by the way, in case you did
not know, I wasn't sure it was until I googled it.
We can actually google it to prove it.
And you can see it's a type of parrot.
Very pretty one.
It's not exactly the same kind of bird, but it's very close as you can see.
So we know our model is working well.
So that's it for this lesson.
In the next lesson, we're going to use Flask and Grok to create an actual website that allows you to
access it, and you can accept some anywhere in the world and allows you to upload a picture and returns
to class back to you.
So let's stop there and we'll begin that flask web app section.
No, thank you.