Now, this is good if I just wanted to run it on all my notes once and tag them. But if I want to keep tagging them automatically when I write new notes then I have to deploy this model and make an API.
So far trying the model on google colab was easy, but if I want to run the same model on my laptop(Which is new and has GPU bdw 😛) or on a server, I need to have a proper Environment setup. I need the right python version, the right PyTorch, and other dependencies. And trust me, pip install isn’t always very easy. We would very often run into dependency issues, especially when you are on windows(Aww. :O). Saving the day — Docker.
I am sure almost all of us know about docker already and i am absolutely in love with it now ❤. But If you are not using it already, i would highly recommend that you do give it a try. Its simple, its fast, and it saves a lot of time. That being said for the whale, lets dive into what to do next.
First of all create a flask server and register a simple route that accepts the text to be tagged and sends detected mood in the response.
At this point this is how my project structure looks like —
Now create a docker file and add a bunch of stuff we are going to need. A common recipe of a Dockerfile involves these steps —
a. Choosing a Base Image
b. Installing additional packages we need
c. Copy things from the work directory into docker
d. Install project dependencies (npm install/pip install etc)
e. Run the application and usually expose a port.
This is how our docker file looks like.
Simple!! Isn’t it ?
Now just build this image and run it.
# Building Docker Image, Giving it name mood_tagging
docker build -t mood_tagging .# Run the image we just built. And map the port which we exposed in # our Dockerfile.
docker run -d -p 5005:5005 mood_tagging
Tada!!
Check it with curl.
curl --location --request POST 'localhost:5005/get_emotion' --header 'Content-Type: application/json' --data-raw '{"text": "Wohoo! Am i jumping on my bed happy to see this working ? Ohh yeah i am"}'# <pad> joy
That’s it. Now you have a running application server with Docker on your machine. It’s time to deploy it on the server. There are many ways to do it. Especially now we live in a world where DevOps is sort of another programming language in itself. But we’ll follow the simplest instructions at the moment without throwing in one more stack or dev ops tool.
You can simply clone your repository on the server and run the last step there. (Build and run commands). Of course, I am Assuming you have docker installed there.
You can find everything put together in this Github repo. This was a basic recipe, but now you can add more ingredients and flavors of your choice into this, to make it more delicious 😋.