|
''' |
|
# upload model |
|
import torch |
|
from transformers import GPT2LMHeadModel,GPT2Tokenizer, GPT2Config |
|
|
|
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") |
|
model = torch.load('text_summary_4sets_2_550.pth', map_location=torch.device('mps')) |
|
|
|
model.push_to_hub(repo_name="text-summary-gpt2-short", repo_id="Lin0He/text-summary-gpt2-short") |
|
tokenizer.push_to_hub(repo_name="text-summary-gpt2-short", repo_id="Lin0He/text-summary-gpt2-short") |
|
''' |
|
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=300): |
|
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: |
|
return tokenizer.decode(result) |
|
else: |
|
result.append(res_id) |
|
|
|
|
|
result_text.append(tokenizer.decode(result)) |
|
return sorted(result_text, key=len)[3] |
|
|
|
class EndpointHandler(): |
|
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, Any]) -> Dict[str, Any]: |
|
|
|
inputs = data.pop("inputs", data) |
|
|
|
prediction = model_infer( self.model, self.tokenizer, inputs+"TL;DR") |
|
return prediction |
|
|