|
from typing import Dict, List, Any |
|
from transformers import NougatProcessor, VisionEncoderDecoderModel |
|
import torch |
|
|
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
self.processor = NougatProcessor.from_pretrained(path) |
|
self.model = VisionEncoderDecoderModel.from_pretrained(path) |
|
|
|
self.model.to(device) |
|
|
|
|
|
|
|
|
|
def __call__(self, data): |
|
|
|
inputs = data.pop("inputs", data) |
|
|
|
|
|
|
|
pixel_values = self.processor(inputs, return_tensors="pt").pixel_values |
|
print(type(pixel_values)) |
|
|
|
outputs = self.model.generate( |
|
pixel_values.to(device), |
|
min_length = 1, |
|
|
|
max_length=3584, |
|
|
|
|
|
|
|
|
|
|
|
bad_words_ids=[[self.processor.tokenizer.unk_token_id]], |
|
|
|
) |
|
|
|
prediction = self.processor.batch_decode(outputs, skip_special_tokens=True)[0] |
|
prediction = self.processor.post_process_generation(prediction, fix_markdown=False) |
|
|
|
return prediction |
|
|