Hello, everyone, and welcome to this new and exciting session in which we are going to deploy our
API to the cloud.
Now, in the previous session we had looked at how to build this API using fast API where we could simply
pass in an image like this and then obtain the output where we we get the emotion that is happy and
we also get the time it takes to produce this output so we could execute that.
And you see we have our result right here.
That said, the platform which I'll be using to deploy our API is Heroku, and Heroku is a platform
as a service that is a pass that enables developers to build, run and operate applications entirely
in the cloud.
So first things first, you could go ahead and sign up.
So you click here, sign up and if you've already signed up, you could go ahead and log in.
So once we've signed up and logged in, we could get back to the code here.
We'll stop this, let's clear that out.
And then now what we'll do is we'll do a pip freeze.
So let's do pip freeze.
And you see that we have this output, which is basically all the different packages we have in our
virtual environment.
So now let's write out or put out all these packages right here in a requirements.txt file.
So we'll do PIP freeze and then we'll say, want to pass?
Send this to our requirements dot txt file.
Now note that we are in this directory.
Emotions detection does this.
So when we do this here you find this requirements.
Let's take this one off.
Did this previously um, move to trash?
Okay, so you find that now we're left with service and we have this requirements dot txt that said,
let's open up this requirements.
You see all the different packages which we installed already.
Now we'll get into OpenCV We have.
OpenCV Well that should be Python.
Let's search for OpenCV.
Okay, so it's OpenCV Python.
And now what we'll do is we'll modify this.
And the reason why we modifying this is because this version of OpenCV right here is one which you could
use even with a desktop application.
But what we need for the cloud is one which is headless, that is one which doesn't contain all the
functions.
It's fairly functions like those of visualization.
Given that what we're doing with OpenCV here is essentially, um, maybe resizing and that's basically
it.
So, uh, we don't need this OpenCV python.
We instead we're going to make use of this OpenCV python headless.
So that's it.
We are going to take this version four point, 2.00.3232.
Okay.
So with that done, we save this file to save.
Well, once we save that as administrator, the next thing we do is we are going to create this proc
file.
So in here we have, well, new file.
We'll call this proc file.
There we go.
See?
Recognizes that already.
And then what we'll do is we're going to paste out essentially what we run.
Um, each time we want to launch our server.
So we have web and that's it.
So here we'll specify number of workers to two and that's it.
So here we have Gunicorn service main app, number of worker set, worker class set, and that's fine.
So let's save this too.
And then we create another file runtime dot txt.
Now this runtime dot txt is essentially going to take this python version.
So we'll copy this and we paste that out here.
So here we have Python and this should be fine.
Now also make sure you include this hyphen before this python version with our proc file already the
requirements dot txt file ready and the runtime dot txt file ready.
We're going to get into the Heroku platform where we'll get right here and click on create new app.
So we want to create a new app and then we're going to give this app a name.
So while this loads, there we go, we're going to call this emotions detection.
Now, we could add or add this to a pipeline or deploy this directly.
So we want to deploy this directly.
We have three options using the Heroku git using or by connecting to GitHub or using the Heroku cli.
So we want to use Heroku git.
And so we're going to follow this simple instructions right here.
It's not very long now.
First and first Heroku login.
So let's get back here.
We have Heroku login.
Now you find that this will work and now.
But you should note that let's let's stop this.
You should note that if you don't have Heroku initially installed, this wouldn't work.
So the first thing you want to do is do a sudo snap install heroku, sudo snap, install heroku before
doing the heroku login.
Now given that I've done that already, I will just do a local login and then that should be fine.
So right here we have heroku login and press.
Any key to open up the browser, press any key and it should be able to login very easily.
There we go.
You click on this link right here, and if you click on that link, you should be able to log in.
So you can see here we have logged in.
So that is it.
We're now logged in.
The next thing we want to do is, um, from here we want to do now is get into our project directory.
So once we get into our project directory, let's zoom this.
We get into our project directory and then from there we do a git init.
Now if you're not familiar with git, you could take a crash course on git.
So you get familiar with all these terms.
So let's get back here and do, let's clear this.
Um clear.
And then we do a git init.
There we go.
See we in this emotions detection directory.
Now once we've done that, you see initialize empty repository in our directory.
Now the next thing we want to do is, um, have this here.
Let's copy this.
And then, um, paste this out here.
So we have this Heroku git remote and then we specify the project name.
Notice how this project name is automatically generated because this is what we put in.
Um, let's get back here.
Run this.
And that should be fine.
So that's what we have now.
Command field.
We told that to.
Well, we command field git remote, fertile, unsafe repository to add an exception for this directory
called git config and that's it.
So what we'll do is now we're going to copy this and then paste out right here.
So there we go.
Let's now get back and run this and this should work.
So that is it.
So set git remote Heroku to this and that's fine.
Now the next step we want to do is deploy the application.
So we have git add, git commit and git push.
So let's go ahead and do git add.
We want to add up all our files.
So pick the dot and then we have let's copy this.
Git commit, um, git commit, make it better, paste it out.
Well let's say our first, first, um, commit of our emotions detection app or well, API.
So this is a string which you could obviously modify.
Now we run that and then now you see we have all this, um, created and then now let's go ahead and
do the git push Heroku master.
So here we have git push Heroku master and this should work.
Fine.
What we get here is this push rejected to emotional detection neural learn.
So let's scroll up and try to understand why we have this problem.
So we told your request at runtime is not available for this stack.
So open this up and then, um, let's try to check out on this different Python versions available for
the stack here.
You could see we have this, um, 3.9.15, which is on all supported stacks.
So let's get back here and then um, a level of the runtime.
Let's, let's take all this off, paste that now save that, save that and then we'll do, um, git
add, um, git commit.
Now what we'll say here is we have um, updated runtime dot txt file with Python version 3.9.15, run
that and then now we're ready to do git push Heroku master once more.
We still get another error right here where we told that um.
No matching distribution found for OpenCV Python headless.
So now we're going to take off this version and then we do the git push one more time.
Once they get pushed down, you see that we now have this release and here is our link.
So let's click on this.
We open up and there we go.
See, we have Hello World.
So this is what we have online now.
And we could go, um, docs and we check out on our swagger UI.
So let's reduce this and let's try this out.
See, we're trying this out online.
We've deployed this on the cloud.
Let's execute and see what we get.
Okay?
So it even takes less time as compared to we.
The PC is taking like 0.23 seconds.
That's about 230 milliseconds.
Let's reduce this so we can see that.
Um, okay, so that's it.
So we see that our model is working, we're able to obtain our outputs.
And then now what we could do is we could actually from here, let's copy this here and then try this
out on Postman.
So there we go.
We have this enter the URL and then we select post request.
Um, well, here we have the body, here we have form data.
Then our key, our key here is going to be image M and then the value.
Given that it's a file, we will now select the file.
There we go.
We have our file selected, click on send and we should get our expected output.
You see here we have happy and time elapsed 230 milliseconds.
So that's what we expect.
You could see from here we have this different timing.
We have the status and we have the time it took for all the different events right here.
And so that's it for the section in which we've deployed our API to the cloud.
And in the next section we are going to carry out load testing on our deployed API.