| import torch |
| import numpy as np |
| from typing import Dict, List, Any |
| from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer |
|
|
| device = 'cuda' if torch.cuda.is_available() else 'cpu' |
|
|
| def topk(probs, n=9): |
| |
| probs = torch.softmax(probs, dim= -1) |
| |
| tokensProb, topIx = torch.topk(probs, k=n) |
| |
| tokensProb = tokensProb / torch.sum(tokensProb) |
| |
| tokensProb = tokensProb.cpu().detach().numpy() |
| |
| choice = np.random.choice(n, 1, p = tokensProb) |
| tokenId = topIx[choice][0] |
| return int(tokenId) |
|
|
| def model_infer(model, tokenizer, review, max_length=10): |
| result_text = [] |
| for i in range(6): |
| |
| |
| review_encoded = tokenizer.encode(review) |
| result = review_encoded |
| initial_input = torch.tensor(review_encoded).unsqueeze(0).to(device) |
| |
| with torch.set_grad_enabled(False): |
| |
| output = model(initial_input) |
| |
| |
| logits = output.logits[0,-1] |
| |
| |
| |
| choices = topk(logits) |
| result.append(choices) |
| |
| |
| for _ in range(max_length): |
| |
| input = torch.tensor(result).unsqueeze(0).to(device) |
| output = model(input) |
| logits = output.logits[0,-1] |
| res_id = topk(logits) |
| |
| |
| if res_id == tokenizer.eos_token_id: |
| result_text.append(tokenizer.decode(result)[len(review):]) |
| break |
| else: |
| result.append(res_id) |
|
|
| |
| |
| return sorted(result_text, key=len)[4] |
|
|
| class PreTrainedPipeline(): |
| def __init__(self, path=""): |
| |
| self.tokenizer = AutoTokenizer.from_pretrained("Lin0He/text-summary-gpt2-short") |
| self.model = AutoModelForCausalLM.from_pretrained("Lin0He/text-summary-gpt2-short") |
|
|
| def __call__(self, data) -> Dict[str,str]: |
| |
| inputs = data |
| |
| prediction = model_infer(self.model, self.tokenizer, inputs+"TL;DR") |
| return {"text": prediction[len(inputs):]} |
|
|