Views
No views yet
distilbert-base-uncased model. The source text data comes from the GoEmotions dataset, which was processed and is available at psyrishi/MoodPulse..pkl files using joblib. The filename includes the model's F1-score on the validation set, which was used for initial selection.huggingface_hub library. You will also need a Transformer model (like distilbert-base-uncased) to generate the embeddings for your input text.1import joblib
2import torch
3from transformers import AutoTokenizer, AutoModel
4from huggingface_hub import hf_hub_download
5
6# --- 1. Load the Embedding Model and Tokenizer ---
7device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
9embedding_model = AutoModel.from_pretrained("distilbert-base-uncased").to(device)
10embedding_model.eval()
11
12# --- 2. Download the Champion Classifier from the Hub ---
13REPO_ID = "psyrishi/affectivelens-emotion-models"
14FILENAME = "LightGBM_MicroF1_0.6240.pkl"
15
16print(f"Downloading model '{FILENAME}' from '{REPO_ID}'...")
17model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
18
19# --- 3. Load the Classifier ---
20classifier = joblib.load(model_path)
21print("Successfully loaded the champion classifier.")
22
23# --- 4. Create a Prediction Function ---
24def predict_emotion(text: str):
25 # Tokenize the input text
26 inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=512)
27 inputs = {k: v.to(device) for k, v in inputs.items()}
28
29 # Get the embedding from the Transformer model
30 with torch.no_grad():
31 outputs = embedding_model(**inputs)
32 embedding = outputs.last_hidden_state[:, 0, :].cpu().numpy()
33
34 # Use the classifier to predict
35 prediction_index = classifier.predict(embedding)[0]
36 emotion_labels = ['negative', 'neutral', 'positive']
37
38 return emotion_labels[prediction_index]
39
40# --- 5. Make a Prediction ---
41my_text = "This was an amazing experience, I am so happy!"
42predicted_emotion = predict_emotion(my_text)
43print(f"\nText: '{my_text}'")
44print(f"--> Predicted Emotion: {predicted_emotion}")RandomOversampling.@inproceedings{demszky2020goemotions,
title={GoEmotions: A Dataset of Fine-Grained Emotions},
author={Demszky, Dorottya and Movshovitz-Attias, Dana and Ko, Jeongwoo and Cowen, Alan and Nemade, Gaurav and Ravi, Sujith},
booktitle={Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics (ACL)},
year={2020}
}