"Add custom pipeline model logic"
Browse files
model.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Dict
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import BertForSequenceClassification, BertTokenizer
|
| 5 |
+
from sklearn.preprocessing import LabelEncoder
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
class CustomBertClassifier:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
# Load model and tokenizer
|
| 11 |
+
self.model = BertForSequenceClassification.from_pretrained(".")
|
| 12 |
+
self.tokenizer = BertTokenizer.from_pretrained(".")
|
| 13 |
+
self.model.eval()
|
| 14 |
+
|
| 15 |
+
# Load label classes
|
| 16 |
+
label_path = hf_hub_download(repo_id="JaySenpai/bert-model", filename="label_classes.npy")
|
| 17 |
+
self.le = LabelEncoder()
|
| 18 |
+
self.le.classes_ = np.load(label_path, allow_pickle=True)
|
| 19 |
+
|
| 20 |
+
def __call__(self, inputs: str) -> List[Dict]:
|
| 21 |
+
# Tokenize input
|
| 22 |
+
inputs = self.tokenizer(inputs, return_tensors="pt", truncation=True, padding=True)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = self.model(**inputs)
|
| 25 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 26 |
+
probs = probs[0].tolist()
|
| 27 |
+
|
| 28 |
+
# Map to labels
|
| 29 |
+
results = []
|
| 30 |
+
for i, prob in enumerate(probs):
|
| 31 |
+
results.append({
|
| 32 |
+
"label": self.le.classes_[i],
|
| 33 |
+
"score": round(prob, 4)
|
| 34 |
+
})
|
| 35 |
+
# Sort by score descending
|
| 36 |
+
results = sorted(results, key=lambda x: x["score"], reverse=True)
|
| 37 |
+
return results
|