florentgbelidji HF staff commited on
Commit
a583978
1 Parent(s): 4ec14ae

Create pretrained pipeline

Browse files
Files changed (1) hide show
  1. pipeline.py +41 -0
pipeline.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from PIL import Image
3
+ import requests
4
+ import torch
5
+ from torchvision import transforms
6
+ from torchvision.transforms.functional import InterpolationMode
7
+
8
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+
10
+ from transformers import pipeline, AutoTokenizer
11
+
12
+
13
+ class PreTrainedPipeline():
14
+ def __init__(self, path=""):
15
+ # load the optimized model
16
+ model = ORTModelForSequenceClassification.from_pretrained(path)
17
+ tokenizer = AutoTokenizer.from_pretrained(path)
18
+ # create inference pipeline
19
+ self.pipeline = pipeline("text-classification", model=model, tokenizer=tokenizer)
20
+
21
+
22
+ def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
23
+ """
24
+ Args:
25
+ data (:obj:):
26
+ includes the input data and the parameters for the inference.
27
+ Return:
28
+ A :obj:`list`:. The object returned should be a list of one list like [[{"label": 0.9939950108528137}]] containing :
29
+ - "label": A string representing what the label/class is. There can be multiple labels.
30
+ - "score": A score between 0 and 1 describing how confident the model is for this label/class.
31
+ """
32
+ inputs = data.pop("inputs", data)
33
+ parameters = data.pop("parameters", None)
34
+
35
+ # pass inputs with all kwargs in data
36
+ if parameters is not None:
37
+ prediction = self.pipeline(inputs, **parameters)
38
+ else:
39
+ prediction = self.pipeline(inputs)
40
+ # postprocess the prediction
41
+ return prediction