|
from transformers import T5Tokenizer, T5ForConditionalGeneration |
|
import torch |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
self.tokenizer = T5Tokenizer.from_pretrained(path) |
|
self.model = T5ForConditionalGeneration.from_pretrained(path) |
|
|
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
self.model.to(self.device) |
|
self.model.eval() |
|
|
|
def __call__(self, data): |
|
""" |
|
Expected input JSON format: |
|
{ |
|
"inputs": "Instruction: Generate the correct Frappe query for the given question...", |
|
"parameters": { |
|
"max_new_tokens": 128, |
|
"temperature": 0.3, |
|
"do_sample": false |
|
} |
|
} |
|
""" |
|
|
|
inputs = data.get("inputs", "") |
|
params = data.get("parameters", {}) |
|
|
|
|
|
encoded_input = self.tokenizer( |
|
inputs, |
|
return_tensors="pt", |
|
padding=True, |
|
truncation=True, |
|
max_length=512 |
|
).to(self.device) |
|
|
|
|
|
gen_kwargs = { |
|
"max_new_tokens": params.get("max_new_tokens", 128), |
|
"temperature": params.get("temperature", 0.3), |
|
"do_sample": params.get("do_sample", False), |
|
"num_beams": params.get("num_beams", 1), |
|
} |
|
|
|
|
|
with torch.no_grad(): |
|
generated_ids = self.model.generate(**encoded_input, **gen_kwargs) |
|
|
|
|
|
output = self.tokenizer.decode(generated_ids[0], skip_special_tokens=False) |
|
output=output.replace("<pad>","") |
|
output=output.replace("</s>","") |
|
output_text = output.replace("[BT]","`") |
|
|
|
|
|
|
|
return [{"generated_text": output_text}] |
|
|