Upload handler.py
Browse files- handler.py +13 -15
handler.py
CHANGED
|
@@ -1,20 +1,18 @@
|
|
| 1 |
from typing import Dict, List, Any
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def preprocess(inputs: Dict[str, Any]) -> List[str]:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# The entrypoint for the Inference API
|
| 19 |
-
def handle(inputs: Dict[str, Any], context: Dict[str, Any] = None) -> Any:
|
| 20 |
-
return inference(inputs)
|
|
|
|
| 1 |
from typing import Dict, List, Any
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
class EndpointHandler:
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
# Load the model and tokenizer at startup
|
| 7 |
+
self.classifier = pipeline("text-classification", model=path if path else None)
|
| 8 |
|
| 9 |
+
def preprocess(self, inputs: Dict[str, Any]) -> List[str]:
|
| 10 |
+
# Hugging Face Inference API sends {"inputs": ...}
|
| 11 |
+
if isinstance(inputs["inputs"], list):
|
| 12 |
+
return inputs["inputs"]
|
| 13 |
+
return [inputs["inputs"]]
|
| 14 |
|
| 15 |
+
def __call__(self, inputs: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 16 |
+
texts = self.preprocess(inputs)
|
| 17 |
+
results = self.classifier(texts)
|
| 18 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|