Last active
January 16, 2021 04:50
-
-
Save arihantdaga/59a66356147cad14de50536109dcbcb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment