Lin0He commited on
Commit
8cd2c8e
·
1 Parent(s): e4545c7

Upload handler.py

Browse files
Files changed (1) hide show
  1. handler.py +99 -0
handler.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ # upload model
3
+ import torch
4
+ from transformers import GPT2LMHeadModel,GPT2Tokenizer, GPT2Config
5
+
6
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
7
+ model = torch.load('text_summary_4sets_2_550.pth', map_location=torch.device('mps'))
8
+
9
+ model.push_to_hub(repo_name="text-summary-gpt2-short", repo_id="Lin0He/text-summary-gpt2-short")
10
+ tokenizer.push_to_hub(repo_name="text-summary-gpt2-short", repo_id="Lin0He/text-summary-gpt2-short")
11
+ '''
12
+ import torch
13
+ from transformers import pipeline, AutoModel, AutoTokenizer
14
+
15
+ def topk(probs, n=9):
16
+ # The scores are initially softmaxed to convert to probabilities
17
+ probs = torch.softmax(probs, dim= -1)
18
+
19
+ # PyTorch has its own topk method, which we use here
20
+ tokensProb, topIx = torch.topk(probs, k=n)
21
+
22
+ # The new selection pool (9 choices) is normalized
23
+ tokensProb = tokensProb / torch.sum(tokensProb)
24
+
25
+ # Send to CPU for numpy handling
26
+ tokensProb = tokensProb.cpu().detach().numpy()
27
+
28
+ # Make a random choice from the pool based on the new prob distribution
29
+ choice = np.random.choice(n, 1, p = tokensProb)#[np.argmax(tokensProb)]#
30
+ tokenId = topIx[choice][0]
31
+
32
+ return int(tokenId)
33
+
34
+ def model_infer(model, tokenizer, review, max_length=30):
35
+ # Preprocess the init token (task designator)
36
+ review_encoded = tokenizer.encode(review)
37
+ result = review_encoded
38
+ initial_input = torch.tensor(review_encoded).unsqueeze(0).to(device)
39
+
40
+ with torch.set_grad_enabled(False):
41
+ # Feed the init token to the model
42
+ output = model(initial_input)
43
+
44
+ # Flatten the logits at the final time step
45
+ logits = output.logits[0,-1]
46
+
47
+ # Make a top-k choice and append to the result
48
+ #choices = [topk(logits) for i in range(5)]
49
+ choices = topk(logits)
50
+ result.append(choices)
51
+
52
+ # For max_length times:
53
+ for _ in range(max_length):
54
+ # Feed the current sequence to the model and make a choice
55
+ input = torch.tensor(result).unsqueeze(0).to(device)
56
+ output = model(input)
57
+ logits = output.logits[0,-1]
58
+ res_id = topk(logits)
59
+
60
+ # If the chosen token is EOS, return the result
61
+ if res_id == tokenizer.eos_token_id:
62
+ return tokenizer.decode(result)
63
+ else: # Append to the sequence
64
+ result.append(res_id)
65
+
66
+ # IF no EOS is generated, return after the max_len
67
+ return tokenizer.decode(result)
68
+
69
+ def predict(text):
70
+ result_text = []
71
+ for i in range(6):
72
+ summary = model_infer(model, tokenizer, input+"TL;DR").strip()
73
+ result_text.append(summary[len(input)+5:])
74
+ return sorted(result_text, key=len)[3]
75
+ #print("summary:", sorted(result_text, key=len)[3])
76
+
77
+
78
+ class EndpointHandler:
79
+ def __init__(self, path=""):
80
+ # load model and tokenizer from path
81
+ tokenizer = AutoTokenizer.from_pretrained("Lin0He/text-summary-gpt2-short")
82
+ model = AutoModel.from_pretrained("Lin0He/text-summary-gpt2-short")
83
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
84
+
85
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
86
+ # process input
87
+ inputs = data.pop("inputs", data)
88
+ parameters = data.pop("parameters", None)
89
+
90
+ result = predict(inputs)
91
+ return result
92
+
93
+
94
+
95
+ '''
96
+ predictor = pipeline("summarization", model = model, tokenizer = tokenizer)
97
+ result = predictor("Input text for prediction")
98
+ print(result)
99
+ '''