Update README.md
Browse files
README.md
CHANGED
|
@@ -34,26 +34,94 @@ The model can predict the following emotions in text:
|
|
| 34 |
Here is an example of how to run inference with the model:
|
| 35 |
|
| 36 |
```python
|
| 37 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 38 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# Load the model and tokenizer
|
| 41 |
-
model = AutoModelForSequenceClassification.from_pretrained("pharci/MiniLM-L12-Affect")
|
| 42 |
-
tokenizer = AutoTokenizer.from_pretrained("pharci/MiniLM-L12-Affect")
|
| 43 |
-
|
| 44 |
-
# Emotion prediction function
|
| 45 |
def predict_emotions(text):
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with torch.no_grad():
|
| 48 |
outputs = model(**inputs)
|
| 49 |
-
|
|
|
|
| 50 |
return predictions
|
| 51 |
|
| 52 |
-
# Example
|
| 53 |
-
test_text = "
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
## Deployment
|
| 59 |
|
|
|
|
| 34 |
Here is an example of how to run inference with the model:
|
| 35 |
|
| 36 |
```python
|
|
|
|
| 37 |
import torch
|
| 38 |
+
from torch import nn
|
| 39 |
+
from transformers import AutoTokenizer, AutoModel
|
| 40 |
+
import safetensors.torch
|
| 41 |
+
import pandas as pd
|
| 42 |
+
|
| 43 |
+
# Custom model class for emotion classification using MiniLM
|
| 44 |
+
class MiniLMEmotionClassifier(nn.Module):
|
| 45 |
+
def __init__(self, model_name):
|
| 46 |
+
super(MiniLMEmotionClassifier, self).__init__()
|
| 47 |
+
self.base_model = AutoModel.from_pretrained(model_name, ignore_mismatched_sizes=True) # Load the MiniLM model
|
| 48 |
+
self.dropout = nn.Dropout(0.1) # Dropout for regularization
|
| 49 |
+
self.fc = nn.Linear(384, 6) # Output layer for 6 emotion categories
|
| 50 |
+
|
| 51 |
+
def forward(self, input_ids, attention_mask=None, labels=None):
|
| 52 |
+
outputs = self.base_model(input_ids=input_ids, attention_mask=attention_mask)
|
| 53 |
+
pooled_output = outputs.last_hidden_state[:, 0, :] # Extract [CLS] token representation
|
| 54 |
+
pooled_output = self.dropout(pooled_output)
|
| 55 |
+
logits = self.fc(pooled_output) # Compute predictions
|
| 56 |
+
|
| 57 |
+
loss = None
|
| 58 |
+
if labels is not None:
|
| 59 |
+
# Use MSE loss for regression-style emotion prediction
|
| 60 |
+
loss_fct = nn.MSELoss()
|
| 61 |
+
loss = loss_fct(logits, labels.view_as(logits))
|
| 62 |
+
|
| 63 |
+
return {"loss": loss, "logits": logits} if loss is not None else {"logits": logits}
|
| 64 |
+
|
| 65 |
+
# Path to the safetensors model file
|
| 66 |
+
model_path = 'MiniLM-L12-Affect/model.safetensors'
|
| 67 |
+
|
| 68 |
+
# Load model weights from the safetensors file
|
| 69 |
+
with open(model_path, 'rb') as f:
|
| 70 |
+
model_data = f.read()
|
| 71 |
+
model_state_dict = safetensors.torch.load(model_data)
|
| 72 |
+
|
| 73 |
+
# Initialize the MiniLM model
|
| 74 |
+
model_name = "./MiniLM-L12-Affect"
|
| 75 |
+
model = MiniLMEmotionClassifier(model_name)
|
| 76 |
+
|
| 77 |
+
# Load pre-trained weights into the model
|
| 78 |
+
model.load_state_dict(model_state_dict, strict = False)
|
| 79 |
+
|
| 80 |
+
# Load the tokenizer
|
| 81 |
+
tokenizer = AutoTokenizer.from_pretrained("./MiniLM-L12-Affect", ignore_mismatched_sizes=True)
|
| 82 |
+
|
| 83 |
+
# Move model to GPU if available
|
| 84 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 85 |
+
model.to(device)
|
| 86 |
+
model.eval()
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def predict_emotions(text):
|
| 89 |
+
"""Tokenizes input text and predicts emotion scores."""
|
| 90 |
+
inputs = tokenizer(
|
| 91 |
+
text,
|
| 92 |
+
padding="max_length",
|
| 93 |
+
truncation=True,
|
| 94 |
+
max_length=128,
|
| 95 |
+
return_tensors="pt"
|
| 96 |
+
)
|
| 97 |
+
# Remove 'token_type_ids' if present
|
| 98 |
+
inputs.pop('token_type_ids', None)
|
| 99 |
+
inputs = {key: value.to(device) for key, value in inputs.items()}
|
| 100 |
+
|
| 101 |
with torch.no_grad():
|
| 102 |
outputs = model(**inputs)
|
| 103 |
+
|
| 104 |
+
predictions = outputs["logits"].cpu().numpy()[0]
|
| 105 |
return predictions
|
| 106 |
|
| 107 |
+
# Example inference
|
| 108 |
+
test_text = "This is horribly amazing ! you're a genius"
|
| 109 |
+
emotions = predict_emotions(test_text)
|
| 110 |
+
|
| 111 |
+
# Emotion categories
|
| 112 |
+
categories = ["Joy", "Anger", "Fear", "Sadness", "Surprise", "Disgust"]
|
| 113 |
+
|
| 114 |
+
# Display the results
|
| 115 |
+
print(f"Text: {test_text}")
|
| 116 |
+
emotion_df = pd.DataFrame(emotions.reshape(1, -1), columns=categories)
|
| 117 |
+
print(emotion_df)
|
| 118 |
```
|
| 119 |
+
**Result**
|
| 120 |
+
| | Joy | Anger | Fear | Sadness | Surprise | Disgust |
|
| 121 |
+
|-----|---------|--------|---------|---------|----------|---------|
|
| 122 |
+
| 0 | 0.844805 | 0.02971 | 0.008245 | -0.007872 | 0.668609 | 0.001267 |
|
| 123 |
+
|
| 124 |
+
|
| 125 |
|
| 126 |
## Deployment
|
| 127 |
|