espejelomar commited on
Commit
18c8be0
1 Parent(s): b166d76

Upload pipeline.py

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