supermy commited on
Commit
60a408e
1 Parent(s): 82c3258

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ import gradio as gr
4
+ import torch.nn.functional as F
5
+
6
+ from transformers import BertTokenizer, GPT2LMHeadModel
7
+
8
+ tokenizer = BertTokenizer.from_pretrained("supermy/couplet-gpt2")
9
+ model = GPT2LMHeadModel.from_pretrained("supermy/couplet-gpt2")
10
+ model.eval()
11
+
12
+ def top_k_top_p_filtering( logits, top_k=0, top_p=0.0, filter_value=-float('Inf') ):
13
+ assert logits.dim() == 1
14
+ top_k = min( top_k, logits.size(-1) )
15
+ if top_k > 0:
16
+ indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
17
+ logits[indices_to_remove] = filter_value
18
+ if top_p > 0.0:
19
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True)
20
+ cumulative_probs = torch.cumsum( F.softmax(sorted_logits, dim=-1), dim=-1 )
21
+ sorted_indices_to_remove = cumulative_probs > top_p
22
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
23
+ sorted_indices_to_remove[..., 0] = 0
24
+ indices_to_remove = sorted_indices[sorted_indices_to_remove]
25
+ logits[indices_to_remove] = filter_value
26
+ return logits
27
+
28
+ def generate(input_text):
29
+ input_ids = [tokenizer.cls_token_id]
30
+ input_ids.extend( tokenizer.encode(input_text + "-", add_special_tokens=False) )
31
+ input_ids = torch.tensor( [input_ids] )
32
+
33
+ generated = []
34
+ for _ in range(100):
35
+ output = model(input_ids)
36
+
37
+ next_token_logits = output.logits[0, -1, :]
38
+ next_token_logits[ tokenizer.convert_tokens_to_ids('[UNK]') ] = -float('Inf')
39
+ filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=8, top_p=1)
40
+ next_token = torch.multinomial( F.softmax(filtered_logits, dim=-1), num_samples=1 )
41
+ if next_token == tokenizer.sep_token_id:
42
+ break
43
+ generated.append( next_token.item() )
44
+ input_ids = torch.cat( (input_ids, next_token.unsqueeze(0)), dim=1 )
45
+
46
+ return "".join( tokenizer.convert_ids_to_tokens(generated) )
47
+
48
+ if __name__ == "__main__":
49
+
50
+ gr.Interface(
51
+ fn=generate,
52
+ inputs="text",
53
+ outputs="text"
54
+ ).launch()