I had a lot of notes in my diary which I had written on different days and I wanted to tag my mood on each day. Before starting I had a notion that there must be plenty of pre-trained AI Models for mood tagging. After all, everyone is working on AI these days. But after doing a few google searches I realized that it’s not as easy as finding a solution to other programming problems. Most google search results showed papers and journals, but no actual code.
The first problem was to find and decide which model to use?
I posted for help on Kaggle and luckily someone pointed me to this project on Github. And it has a model named mrm8488/t5-base-finetuned-emotion. It can categorize a text into 6 emotions (sadness 😢joy 😃love 🥰anger 😡fear 😱surprise 😯). Well, Enough for now.
The second step was to test the Model and its accuracy on my dataset. I think Google Colab is the ultimate choice for quickly testing your AI models.
The actual code is very small
# Bunch of setting up Steps
pip install transformers[sentencepiece]
pip install sentencepiece
import os
# Load the Pretrained Model
from transformers import AutoTokenizer, AutoModelWithLMHead, PreTrainedModel
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion", use_fast=False)
model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-emotion")
# Get emotion
def get_emotion(text):
input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
output = model.generate(input_ids=input_ids, max_length=2)
dec = [tokenizer.decode(ids) for ids in output]
label = dec[0]
return label
get_emotion(
"Dear Diary, Today's special is the tibbetian namkeen tea. Its so so yum. Really good. Te weather here is like Rajasthan today, so chilled. In the room its so cozy. I think i can just close my eyes and imagine, that its all foggy out there and my house is made of wood and i am wearing a woolen cap and in sometime i will go out to the market all the way down the hill, streets are empty, its very cold, its almost afternoon but we cant see where the sun is. Smoke coming out of chimneys from houses.... I am grateful for our cerebral cortex Happy day today.."
) # Output: 'joy'
In Line 7–9 We are loading the model and the tokenizer. Since this is an NLP model, we need a tokenizer. In short, a tokenizer just maps all the words into numbers. In simple terms (“I am doing good” -> [1 23 35 49]).
In line 13 we tokenize our input text, because that’s what our model takes as input.
In lines 14–15 we predict the output for the given text. Very simple. Done.
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 —
Image for post
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 😋.
I also work on an app called theopendiaries.com. If you like writing diary or if you want to read unfiltered life stories of people around the world, do check it.
Top comments (0)