Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
from transformers import AutoTokenizer, GPT2LMHeadModel
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("supermy/jinyong")
|
9 |
+
model = GPT2LMHeadModel.from_pretrained("supermy/jinyong")
|
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(title, context, max_len):
|
29 |
+
|
30 |
+
# input_ids.extend( tokenizer.encode(input_text + "-", add_special_tokens=False) )
|
31 |
+
|
32 |
+
title_ids = tokenizer.encode(title, add_special_tokens=False)
|
33 |
+
context_ids = tokenizer.encode(context, add_special_tokens=False)
|
34 |
+
input_ids = title_ids + [sep_id] + context_ids
|
35 |
+
print(input_ids)
|
36 |
+
cur_len = len(input_ids)
|
37 |
+
input_len = cur_len
|
38 |
+
last_token_id = input_ids[-1]
|
39 |
+
input_ids = torch.tensor([input_ids], dtype=torch.long)
|
40 |
+
|
41 |
+
# input_ids = [tokenizer.cls_token_id]
|
42 |
+
# input_ids.extend( tokenizer.encode(title + "-" +context, add_special_tokens=False) )
|
43 |
+
# input_ids = torch.tensor( [input_ids] )
|
44 |
+
|
45 |
+
print(input_ids)
|
46 |
+
|
47 |
+
while True:
|
48 |
+
outputs = model( input_ids=input_ids[:, -200:] )
|
49 |
+
logits = outputs.logits
|
50 |
+
next_token_logits = logits[0, -1, :]
|
51 |
+
next_token_logits = next_token_logits / 1
|
52 |
+
next_token_logits[unk_id] = -float('Inf')
|
53 |
+
filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=0, top_p=0.85)
|
54 |
+
next_token_id = torch.multinomial( F.softmax(filtered_logits, dim=-1), num_samples=1 )
|
55 |
+
input_ids = torch.cat( ( input_ids, next_token_id.unsqueeze(0) ), dim=1 )
|
56 |
+
cur_len += 1
|
57 |
+
word = tokenizer.convert_ids_to_tokens( next_token_id.item() )
|
58 |
+
if cur_len >= ( input_len + max_len ) and last_token_id == 8 and next_token_id == 3:
|
59 |
+
break
|
60 |
+
if cur_len >= ( input_len + max_len ) and word in [".", "。", "!", "!", "?", "?", ",", ","]:
|
61 |
+
break
|
62 |
+
if next_token_id == eod_id:
|
63 |
+
break
|
64 |
+
result = tokenizer.decode( input_ids.squeeze(0) )
|
65 |
+
return result
|
66 |
+
|
67 |
+
if __name__ == '__main__':
|
68 |
+
eod_id = tokenizer.convert_tokens_to_ids("<eod>")
|
69 |
+
sep_id = tokenizer.sep_token_id
|
70 |
+
unk_id = tokenizer.unk_token_id
|
71 |
+
|
72 |
+
|
73 |
+
gr.Interface(
|
74 |
+
fn=generate,
|
75 |
+
inputs=[
|
76 |
+
gr.Textbox(lines=1, placeholder="输入文本标题:射雕英雄传", value="射雕英雄传",label="文本标题"),
|
77 |
+
gr.Textbox(lines=7, placeholder="输入文本内容:郭靖练功偶遇高人,见道长那轻功,转头便拜其为师", value="郭靖练功偶遇高人,见道长那轻功,转头便拜其为师。",label="初始文本"),
|
78 |
+
"number"
|
79 |
+
],
|
80 |
+
outputs=gr.Textbox(lines=15, placeholder="AI生成的文本显示在这里。",label="生成的文本")
|
81 |
+
).launch()
|