| import json |
| from transformers import AutoModelForSequenceClassification, AutoTokenizer |
| import torch |
| from typing import Dict, List, Any |
|
|
| class EndpointHandler(): |
| def __init__(self, path=""): |
| """ |
| Initializes the model and tokenizer. |
| |
| Args: |
| path (str): Path to the directory containing the model files. |
| """ |
| |
| self.tokenizer = AutoTokenizer.from_pretrained(path) |
| self.model = AutoModelForSequenceClassification.from_pretrained(path) |
| |
| |
| if torch.cuda.is_available(): |
| self.device = torch.device("cuda") |
| else: |
| self.device = torch.device("cpu") |
| self.model.to(self.device) |
| self.model.eval() |
| print("Model and tokenizer loaded successfully.") |
|
|
|
|
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
| """ |
| Processes inference requests. |
| |
| Args: |
| data (Dict[str, Any]): A dictionary containing the input data. |
| Expected key: "inputs" (string or list of strings). |
| Optional key: "parameters" (dictionary). |
| |
| Returns: |
| List[Dict[str, Any]]: A list of dictionaries, where each dictionary |
| contains the prediction results for one input string |
| (e.g., [{"label": "AI", "score": 0.98}]). |
| """ |
| |
| inputs = data.pop("inputs", None) |
| parameters = data.pop("parameters", {}) |
|
|
| if inputs is None: |
| raise ValueError("Missing 'inputs' key in request data") |
|
|
| |
| |
| |
| tokenized_inputs = self.tokenizer( |
| inputs, |
| return_tensors="pt", |
| padding=True, |
| truncation=True, |
| max_length=self.tokenizer.model_max_length |
| ).to(self.device) |
|
|
| |
| with torch.no_grad(): |
| outputs = self.model(**tokenized_inputs) |
|
|
| |
| logits = outputs.logits |
| probabilities = torch.softmax(logits, dim=-1) |
| |
| results = [] |
| |
| for i in range(probabilities.shape[0]): |
| scores = probabilities[i].tolist() |
| predictions = [] |
| for j, score in enumerate(scores): |
| |
| label = self.model.config.id2label[j] |
| predictions.append({"label": label, "score": score}) |
| |
| |
| |
| results.append(predictions) |
|
|
| |
| if isinstance(inputs, str): |
| |
| |
| |
| |
| |
| pass |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| return results |