Update README.md
Browse files
README.md
CHANGED
@@ -29,14 +29,14 @@ This model is designed for integration into IT call center software systems, whe
|
|
29 |
```python
|
30 |
from __future__ import annotations
|
31 |
from transformers import RobertaConfig, RobertaModel, RobertaTokenizer, AutoModel, AutoTokenizer
|
|
|
32 |
|
33 |
# Add a custom regression head to RoBERTa
|
34 |
class SITCC(torch.nn.Module):
|
35 |
-
def __init__(self, model):
|
36 |
super(SITCC, self).__init__()
|
37 |
self.roberta = model
|
38 |
self.regressor = torch.nn.Linear(config.hidden_size, 1) # Outputs a single value
|
39 |
-
|
40 |
|
41 |
def forward(self, input_ids, attention_mask):
|
42 |
outputs = self.roberta(input_ids=input_ids, attention_mask=attention_mask)
|
@@ -44,30 +44,35 @@ class SITCC(torch.nn.Module):
|
|
44 |
logits = self.regressor(sequence_output)
|
45 |
return logits
|
46 |
|
|
|
47 |
def init_model() -> SITCC:
|
48 |
# Load the model from huggingface
|
49 |
model_name = "KameronB/sitcc-roberta"
|
50 |
tokenizer = AutoTokenizer.from_pretrained(model_name, from_tf=False)
|
51 |
-
config = RobertaConfig.from_pretrained(model_name)
|
52 |
-
|
53 |
# create the model based on the RoBERTa base model
|
54 |
-
model = SITCC(RobertaModel(config))
|
|
|
55 |
# fetch the statedict to apply the fine-tuned weights
|
56 |
state_dict = torch.hub.load_state_dict_from_url(f"https://huggingface.co/{model_name}/resolve/main/pytorch_model.bin")
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
model
|
|
|
62 |
|
|
|
63 |
|
64 |
def predict(sentences):
|
|
|
65 |
model.eval()
|
66 |
inputs = tokenizer(sentences, padding=True, truncation=True, max_length=512, return_tensors="pt")
|
67 |
input_ids = inputs['input_ids']
|
68 |
attention_mask = inputs['attention_mask']
|
69 |
-
|
70 |
with torch.no_grad():
|
71 |
outputs = model(input_ids, attention_mask)
|
|
|
72 |
return outputs
|
73 |
|
|
|
29 |
```python
|
30 |
from __future__ import annotations
|
31 |
from transformers import RobertaConfig, RobertaModel, RobertaTokenizer, AutoModel, AutoTokenizer
|
32 |
+
import torch
|
33 |
|
34 |
# Add a custom regression head to RoBERTa
|
35 |
class SITCC(torch.nn.Module):
|
36 |
+
def __init__(self, model, config):
|
37 |
super(SITCC, self).__init__()
|
38 |
self.roberta = model
|
39 |
self.regressor = torch.nn.Linear(config.hidden_size, 1) # Outputs a single value
|
|
|
40 |
|
41 |
def forward(self, input_ids, attention_mask):
|
42 |
outputs = self.roberta(input_ids=input_ids, attention_mask=attention_mask)
|
|
|
44 |
logits = self.regressor(sequence_output)
|
45 |
return logits
|
46 |
|
47 |
+
|
48 |
def init_model() -> SITCC:
|
49 |
# Load the model from huggingface
|
50 |
model_name = "KameronB/sitcc-roberta"
|
51 |
tokenizer = AutoTokenizer.from_pretrained(model_name, from_tf=False)
|
52 |
+
config = RobertaConfig.from_pretrained(model_name,)
|
53 |
+
|
54 |
# create the model based on the RoBERTa base model
|
55 |
+
model = SITCC(RobertaModel(config), config)
|
56 |
+
|
57 |
# fetch the statedict to apply the fine-tuned weights
|
58 |
state_dict = torch.hub.load_state_dict_from_url(f"https://huggingface.co/{model_name}/resolve/main/pytorch_model.bin")
|
59 |
+
# if running on cpu
|
60 |
+
# state_dict = torch.hub.load_state_dict_from_url(f"https://huggingface.co/{model_name}/resolve/main/pytorch_model.bin", map_location=torch.device('cpu'))
|
|
|
61 |
|
62 |
+
model.load_state_dict(state_dict)
|
63 |
+
return model, tokenizer
|
64 |
|
65 |
+
model, tokenizer = init_model()
|
66 |
|
67 |
def predict(sentences):
|
68 |
+
|
69 |
model.eval()
|
70 |
inputs = tokenizer(sentences, padding=True, truncation=True, max_length=512, return_tensors="pt")
|
71 |
input_ids = inputs['input_ids']
|
72 |
attention_mask = inputs['attention_mask']
|
73 |
+
|
74 |
with torch.no_grad():
|
75 |
outputs = model(input_ids, attention_mask)
|
76 |
+
|
77 |
return outputs
|
78 |
|