File size: 895 Bytes
101e5d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# coding=utf-8
# author: xusong <xusong28@jd.com>
# time: 2021/9/17 17:43

import torch
from transformers import BertTokenizer
from modeling_kplug import KplugForMaskedLM

model_dir = "../../models/hugging_face/kplug/"
tokenizer = BertTokenizer.from_pretrained(model_dir)
model = KplugForMaskedLM.from_pretrained(model_dir)

input_ids = torch.tensor(tokenizer.encode("这款连衣裙真漂亮", add_special_tokens=True)).unsqueeze(0)
outputs = model(input_ids)

# fill mask
from transformers import FillMaskPipeline
fill_masker = FillMaskPipeline(model=model, tokenizer=tokenizer)
outputs = fill_masker(f"这款连[MASK]裙真漂亮")
print(outputs)

from transformers import pipeline
nlp_fill = pipeline('fill-mask', topk=10)


def from_url():
    MODEL_PATH = "http://storage.jd.com/language-models/kplug/huggingface/pytorch_model.bin"
    model = KplugForMaskedLM.from_pretrained(MODEL_PATH)