Views
No views yet
bert-base-uncased (Hugging Face Transformers)LSTM to capture sentence-level emotional flow| File | Description |
|---|---|
bert_lstm_sentiment_model.pt | Trained PyTorch model (BERT + LSTM + Classifier) |
label_encoder.pkl | sklearn.preprocessing.LabelEncoder used during training |
1#pip install torch transformers scikit-learn #if not already installed, remove the first comment
2
3import torch
4import pickle
5from transformers import BertTokenizer
6
7# Load model
8model = torch.load("bert_lstm_sentiment_model.pt", map_location=torch.device('cpu'))
9model.eval()
10
11# Load tokenizer and label encoder
12tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
13with open("label_encoder.pkl", "rb") as f:
14 label_encoder = pickle.load(f)
15
16# Sample input
17text = "I feel empty and overwhelmed today."
18
19# Tokenize
20inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
21with torch.no_grad():
22 outputs = model(**inputs)
23 predicted_class = torch.argmax(outputs.logits, dim=1).item()
24 emotion = label_encoder.inverse_transform([predicted_class])[0]
25
26print(f"Predicted Emotion: {emotion}")