1 Star 0 Fork 13

LCL/iii

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.py 2.18 KB
一键复制 编辑 原始数据 按行查看 历史
衣沾不足惜 提交于 2024-01-15 10:22 . test kafka
import torch
import gradio as gr
import torch.nn.functional as F
from transformers import BertTokenizer, GPT2LMHeadModel
tokenizer = BertTokenizer.from_pretrained("modelee/gpt2-chinese-poem")
model = GPT2LMHeadModel.from_pretrained("modelee/gpt2-chinese-poem")
model.eval()
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
assert logits.dim() == 1
top_k = min(top_k, logits.size(-1))
if top_k > 0:
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
logits[indices_to_remove] = filter_value
if top_p > 0.0:
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
sorted_indices_to_remove = cumulative_probs > top_p
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices[sorted_indices_to_remove]
logits[indices_to_remove] = filter_value
return logits
def generate(input_text):
input_ids = [tokenizer.cls_token_id]
input_ids.extend( tokenizer.encode(input_text, add_special_tokens=False) )
input_ids = torch.tensor( [input_ids] )
generated = []
for _ in range(100):
output = model(input_ids)
next_token_logits = output.logits[0, -1, :]
next_token_logits[tokenizer.convert_tokens_to_ids('[UNK]')] = -float('Inf')
filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=8, top_p=1)
next_token = torch.multinomial( F.softmax(filtered_logits, dim=-1), num_samples=1 )
if next_token == tokenizer.sep_token_id:
break
generated.append( next_token.item() )
input_ids = torch.cat((input_ids, next_token.unsqueeze(0)), dim=1)
return input_text + "".join( tokenizer.convert_ids_to_tokens(generated) )
examples = [["不堪翘首暮云中"], ["开源中国"], ["行到水穷处"], ["王师北定中原日"] ,["雪"], ["海上升明月"], ["十年磨一剑"]]
if __name__ == "__main__":
gr.Interface(fn=generate, inputs="text", outputs="text",examples=examples).queue(concurrency_count=1).launch()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lclgxx/iii.git
[email protected]:lclgxx/iii.git
lclgxx
iii
iii
master

搜索帮助