monaal commited on
Commit
c47ca17
1 Parent(s): 2247841

Update app.py

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