|
from typing import List, Dict |
|
import torch |
|
from transformers import AutoConfig, AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
self.config = AutoConfig.from_pretrained(path) |
|
|
|
|
|
self.model = AutoModelForSequenceClassification.from_pretrained(path) |
|
self.model.eval() |
|
|
|
|
|
self.tokenizer = AutoTokenizer.from_pretrained(path) |
|
|
|
|
|
self.max_length = 512 |
|
|
|
def __call__(self, data: List[Dict[str, str]]) -> List[Dict[str, float]]: |
|
""" |
|
处理文本推理请求 |
|
""" |
|
try: |
|
|
|
texts = [] |
|
for item in data: |
|
|
|
if isinstance(item, dict) and "inputs" in item: |
|
texts.append(item["inputs"]) |
|
elif isinstance(item, str): |
|
texts.append(item) |
|
else: |
|
raise ValueError(f"Unexpected input format: {item}") |
|
|
|
|
|
encoded_inputs = self.tokenizer( |
|
texts, |
|
padding=True, |
|
truncation=True, |
|
max_length=self.max_length, |
|
return_tensors="pt" |
|
) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = self.model(**encoded_inputs) |
|
logits = outputs.logits |
|
probabilities = torch.softmax(logits, dim=-1) |
|
|
|
|
|
results = [] |
|
for probs in probabilities: |
|
label_id = int(torch.argmax(probs).item()) |
|
confidence = float(probs[label_id].item()) |
|
results.append({ |
|
"label": str(label_id), |
|
"score": confidence |
|
}) |
|
|
|
return results |
|
|
|
except Exception as e: |
|
|
|
print(f"Error in prediction: {str(e)}") |
|
return [{"error": str(e)}] |
|
|
|
def preprocess(self, text: str) -> Dict[str, torch.Tensor]: |
|
""" |
|
预处理方法 |
|
""" |
|
try: |
|
encoded = self.tokenizer( |
|
text, |
|
padding=True, |
|
truncation=True, |
|
max_length=self.max_length, |
|
return_tensors="pt" |
|
) |
|
return encoded |
|
except Exception as e: |
|
print(f"Error in preprocessing: {str(e)}") |
|
raise e |
|
|
|
def postprocess(self, model_outputs) -> Dict: |
|
""" |
|
后处理方法 |
|
""" |
|
try: |
|
logits = model_outputs.logits |
|
probabilities = torch.softmax(logits, dim=-1) |
|
label_id = int(torch.argmax(probabilities[0]).item()) |
|
confidence = float(probabilities[0][label_id].item()) |
|
|
|
return { |
|
"label": str(label_id), |
|
"score": confidence |
|
} |
|
except Exception as e: |
|
print(f"Error in postprocessing: {str(e)}") |
|
raise e |