Add some usage code
Browse files
README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
```python
|
| 7 |
+
class HelpdeskReviewModel(nn.Module):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super(HelpdeskReviewModel, self).__init__()
|
| 10 |
+
self.bert = RobertaModel.from_pretrained('roberta-base')
|
| 11 |
+
self.drop = nn.Dropout(p=0.2)
|
| 12 |
+
|
| 13 |
+
# Fully connected layers
|
| 14 |
+
self.fc1 = nn.Linear(self.bert.config.hidden_size, 512) # First fully connected layer
|
| 15 |
+
self.fc2 = nn.Linear(512, 256) # Second fully connected layer
|
| 16 |
+
self.fc3 = nn.Linear(256, 128) # Third fully connected layer
|
| 17 |
+
|
| 18 |
+
# Activation function
|
| 19 |
+
self.relu = nn.ReLU()
|
| 20 |
+
|
| 21 |
+
self.output = nn.Linear(128, 4) # 4 outputs
|
| 22 |
+
self.sigmoid = nn.Sigmoid()
|
| 23 |
+
|
| 24 |
+
state_dict = torch.hub.load_state_dict_from_url(f"https://huggingface.co/KameronB/SITCCSA/resolve/main/pytorch_model.bin")
|
| 25 |
+
# if running on cpu
|
| 26 |
+
# state_dict = torch.hub.load_state_dict_from_url(f"https://huggingface.co/KameronB/SITCCSA/resolve/main/pytorch_model.bin", map_location=torch.device('cpu'))
|
| 27 |
+
|
| 28 |
+
self.load_state_dict(state_dict)
|
| 29 |
+
|
| 30 |
+
def forward(self, input_ids, attention_mask):
|
| 31 |
+
_, pooled_output = self.bert(
|
| 32 |
+
input_ids=input_ids,
|
| 33 |
+
attention_mask=attention_mask,
|
| 34 |
+
return_dict=False
|
| 35 |
+
)
|
| 36 |
+
output = self.drop(pooled_output)
|
| 37 |
+
|
| 38 |
+
# apply new hidden layers
|
| 39 |
+
output = self.relu(self.fc1(output))
|
| 40 |
+
output = self.relu(self.fc2(output))
|
| 41 |
+
output = self.relu(self.fc3(output))
|
| 42 |
+
|
| 43 |
+
return self.sigmoid(self.output(output))
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
```python
|
| 47 |
+
def make_prediction(model, tokenizer, text, max_length=512):
|
| 48 |
+
# Tokenize the input text and convert to tensor
|
| 49 |
+
inputs = tokenizer.encode_plus(
|
| 50 |
+
text,
|
| 51 |
+
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
|
| 52 |
+
max_length=max_length, # Pad & truncate all sentences.
|
| 53 |
+
padding='max_length',
|
| 54 |
+
truncation=True,
|
| 55 |
+
return_tensors="pt" # Return PyTorch tensors.
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Move tensors to the same device as model
|
| 59 |
+
input_ids = inputs['input_ids']
|
| 60 |
+
attention_mask = inputs['attention_mask']
|
| 61 |
+
if torch.cuda.is_available():
|
| 62 |
+
input_ids = input_ids.cuda()
|
| 63 |
+
attention_mask = attention_mask.cuda()
|
| 64 |
+
model = model.cuda()
|
| 65 |
+
|
| 66 |
+
# Make prediction
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
outputs = model(input_ids, attention_mask=attention_mask)
|
| 69 |
+
|
| 70 |
+
# Return probabilities
|
| 71 |
+
return outputs.cpu().numpy()[0] # Return to CPU and convert to numpy array if not running on CPU
|
| 72 |
+
|
| 73 |
+
# Example usage of the function
|
| 74 |
+
texts = [
|
| 75 |
+
"This Agent is TERRIBLE!: The agent I spoke to on the phone did not seem to have any idea of what he was doing.",
|
| 76 |
+
"Excellent work!: The tech that installed my software was amazing! Thank you!",
|
| 77 |
+
"Good Work: The person who anwsered the phone did a pretty good job. It took a bit longer than I would have liked, but they got the job done.",
|
| 78 |
+
"Bad Computer: My Computer is a piece of junk!!!",
|
| 79 |
+
"Poor Service: I sent David and email and it took him over 30 seconds to respond. The service is so slow that I missed the solar eclipse.",
|
| 80 |
+
"Very Slow: The technician was very slow.",
|
| 81 |
+
"Thank you!: Stanley did a great job installing my software!",
|
| 82 |
+
"You need better training: These agents need better training, they cant even seem to do simple troubleshooting.",
|
| 83 |
+
"The technician threatened my life: The technician threatened my life!"
|
| 84 |
+
]
|
| 85 |
+
for text in texts:
|
| 86 |
+
probabilities = make_prediction(model, tokenizer, text)
|
| 87 |
+
print(probabilities)
|
| 88 |
+
```
|