Upload inference.py with huggingface_hub
Browse files- inference.py +33 -0
inference.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import abc
|
2 |
+
|
3 |
+
from .artifact import Artifact
|
4 |
+
|
5 |
+
|
6 |
+
class InferenceEngine(abc.ABC, Artifact):
|
7 |
+
"""Abstract base class for inference."""
|
8 |
+
|
9 |
+
@abc.abstractmethod
|
10 |
+
def infer(self, dataset):
|
11 |
+
"""Perform inference on the input dataset."""
|
12 |
+
pass
|
13 |
+
|
14 |
+
|
15 |
+
class HFPipelineBasedInferenceEngine(Artifact):
|
16 |
+
"""Abstract base class for inference."""
|
17 |
+
|
18 |
+
model_name: str
|
19 |
+
max_new_tokens: int
|
20 |
+
|
21 |
+
def prepare(self):
|
22 |
+
from transformers import pipeline
|
23 |
+
|
24 |
+
self.model = pipeline(model=self.model_name)
|
25 |
+
|
26 |
+
def infer(self, dataset):
|
27 |
+
return [
|
28 |
+
output["generated_text"]
|
29 |
+
for output in self.model(
|
30 |
+
[instance["source"] for instance in dataset],
|
31 |
+
max_new_tokens=self.max_new_tokens,
|
32 |
+
)
|
33 |
+
]
|