Gabriel commited on
Commit
3720ff1
1 Parent(s): e8e725e

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +10 -5
handler.py CHANGED
@@ -1,20 +1,25 @@
1
  from typing import Dict, List, Any
2
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
 
3
 
4
-
5
- class PreTrainedPipeline():
6
  def __init__(self, path=""):
7
  self.processor = TrOCRProcessor.from_pretrained(path)
8
  self.model = VisionEncoderDecoderModel.from_pretrained(path)
 
 
 
9
 
10
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
11
- image = data.pop("inputs", data)
 
 
12
 
13
  # process image
14
- pixel_values = self.processor(images=image, return_tensors="pt").pixel_values
15
 
16
  # run prediction
17
- generated_ids = self.model.generate(pixel_values)
18
 
19
  # decode output
20
  prediction = generated_text = self.processor.batch_decode(generated_ids, skip_special_tokens=True)
 
1
  from typing import Dict, List, Any
2
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ import torch
4
 
5
+ class EndpointHandler():
 
6
  def __init__(self, path=""):
7
  self.processor = TrOCRProcessor.from_pretrained(path)
8
  self.model = VisionEncoderDecoderModel.from_pretrained(path)
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ self.model.to(device)
12
 
13
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
14
+ inputs = data.pop("inputs", data)
15
+ image_input = inputs.get('image')
16
+
17
 
18
  # process image
19
+ pixel_values = self.processor(images=image_input, return_tensors="pt").pixel_values
20
 
21
  # run prediction
22
+ generated_ids = self.model.generate(pixel_values.to(device))
23
 
24
  # decode output
25
  prediction = generated_text = self.processor.batch_decode(generated_ids, skip_special_tokens=True)