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

Delete handler.py

Browse files
Files changed (1) hide show
  1. handler.py +0 -33
handler.py DELETED
@@ -1,33 +0,0 @@
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]