qhchina commited on
Commit
7b0e97f
1 Parent(s): d4916bc

Upload handler.py

Browse files
Files changed (1) hide show
  1. handler.py +20 -24
handler.py CHANGED
@@ -1,37 +1,33 @@
1
  import torch
2
  from transformers import BertTokenizer, BertForSequenceClassification
 
3
 
4
  class SentimentModel:
5
  def __init__(self, model_path):
6
  self.tokenizer = BertTokenizer.from_pretrained(model_path)
7
  self.model = BertForSequenceClassification.from_pretrained(model_path)
8
  self.model.eval()
 
 
9
 
10
- def predict(self, texts):
11
- max_length = 250
12
- batch_size = 64
13
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
- self.model.to(device)
15
-
16
- probabilities = []
17
  encodings = self.tokenizer(texts, truncation=True, padding=True, max_length=max_length, return_tensors="pt")
18
- input_ids = encodings['input_ids']
19
- attention_masks = encodings['attention_mask']
20
 
21
- dataset = torch.utils.data.TensorDataset(input_ids, attention_masks)
22
- dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size)
23
-
24
  with torch.no_grad():
25
- for batch in dataloader:
26
- input_ids_batch, attention_masks_batch = batch
27
- input_ids_batch = input_ids_batch.to(device)
28
- attention_masks_batch = attention_masks_batch.to(device)
29
-
30
- outputs = self.model(input_ids_batch, attention_mask=attention_masks_batch)
31
- logits = outputs.logits
32
-
33
- probs = torch.nn.functional.softmax(logits, dim=-1)
34
- strong_probs = probs[:, 1].tolist()
35
- probabilities.extend(strong_probs)
36
 
37
- return probabilities
 
 
 
 
1
  import torch
2
  from transformers import BertTokenizer, BertForSequenceClassification
3
+ from typing import List
4
 
5
  class SentimentModel:
6
  def __init__(self, model_path):
7
  self.tokenizer = BertTokenizer.from_pretrained(model_path)
8
  self.model = BertForSequenceClassification.from_pretrained(model_path)
9
  self.model.eval()
10
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ self.model.to(self.device)
12
 
13
+ def predict(self, texts: List[str]) -> List[float]:
14
+ max_length = 128
 
 
 
 
 
15
  encodings = self.tokenizer(texts, truncation=True, padding=True, max_length=max_length, return_tensors="pt")
16
+ input_ids = encodings['input_ids'].to(self.device)
17
+ attention_masks = encodings['attention_mask'].to(self.device)
18
 
 
 
 
19
  with torch.no_grad():
20
+ outputs = self.model(input_ids, attention_mask=attention_masks)
21
+ logits = outputs.logits
22
+ probs = torch.nn.functional.softmax(logits, dim=-1)
23
+ strong_probs = probs[:, 1].tolist() # Probability of the "strong" class (class 1)
24
+
25
+ return strong_probs
26
+
27
+ # Initialize the model
28
+ model = SentimentModel(".")
 
 
29
 
30
+ def infer(inputs):
31
+ texts = [inputs[i]['data'] for i in range(len(inputs))]
32
+ probabilities = model.predict(texts)
33
+ return [{"sentiment intensity": prob} for prob in probabilities]