davanstrien HF staff commited on
Commit
91e182f
·
1 Parent(s): eb6e629

Create pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +65 -0
pipeline.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ import os
3
+
4
+ import json
5
+ import numpy as np
6
+
7
+ from fastai.learner import load_learner
8
+
9
+ class PreTrainedPipeline():
10
+
11
+ def __init__(self, path=""):
12
+
13
+ # IMPLEMENT_THIS
14
+
15
+ # Preload all the elements you are going to need at inference.
16
+
17
+ # For instance your model, processors, tokenizer that might be needed.
18
+
19
+ # This function is only called once, so do all the heavy processing I/O here"""
20
+
21
+ self.model = load_learner(os.path.join(path, "20211115-model.pkl"))
22
+
23
+ with open(os.path.join(path, "config.json")) as config:
24
+
25
+ config = json.load(config)
26
+
27
+ self.id2label = config["id2label"]
28
+
29
+ def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]:
30
+
31
+ """
32
+
33
+ Args:
34
+
35
+ inputs (:obj:`PIL.Image`):
36
+
37
+ The raw image representation as PIL.
38
+
39
+ No transformation made whatsoever from the input. Make all necessary transformations here.
40
+
41
+ Return:
42
+
43
+ A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
44
+
45
+ It is preferred if the returned list is in decreasing `score` order
46
+
47
+ """
48
+
49
+ # IMPLEMENT_THIS
50
+
51
+ # FastAI expects a np array, not a PIL Image.
52
+
53
+ _, _, preds = self.model.predict(np.array(inputs))
54
+
55
+ preds = preds.tolist()
56
+
57
+ labels = [
58
+
59
+ {"label": str(self.id2label["0"]), "score": preds[0]},
60
+
61
+ {"label": str(self.id2label["1"]), "score": preds[1]},
62
+
63
+ ]
64
+
65
+ return labels