mpd commited on
Commit
30f4ad4
1 Parent(s): 2a6b197

Create a pipeline file - I think this is required for the image pipeline widget to work

Browse files
Files changed (1) hide show
  1. pipeline.py +33 -0
pipeline.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.labels = config["labels"]
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
+ return [{"label": label, "score": preds[idx]} for idx, label in enumerate(self.labels)]