File size: 1,414 Bytes
2bb0b26
 
 
 
 
 
 
a39e93b
2bb0b26
 
 
 
a39e93b
 
 
 
 
2bb0b26
 
 
5b47e63
2bb0b26
 
c10350f
2bb0b26
01920f9
 
2bb0b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7faefc8
2bb0b26
7faefc8
6e6846f
7faefc8
2bb0b26
 
 
a39e93b
5b47e63
 
2bb0b26
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# coding=utf-8
# author: xusong <xusong28@jd.com>
# time: 2022/8/23 17:08

"""


https://github.com/gradio-app/gradio/blob/299ba1bd1aed8040b3087c06c10fedf75901f91f/gradio/external.py#L484

interface = gr.Interface.load(
    "models/bert-base-uncased", api_key=None, alias="fill-mask"
)
## TODO:

1. json_output
2. 百分数换成小数
3.
"""

import gradio as gr
from info import article
from transformers import FillMaskPipeline
from transformers import BertTokenizer
from kplug.modeling_kplug import KplugForMaskedLM

tokenizer = BertTokenizer.from_pretrained("eson/kplug-base-encoder")
model = KplugForMaskedLM.from_pretrained("eson/kplug-base-encoder")

# fill mask
def fill_mask(text):
    fill_masker = FillMaskPipeline(model=model, tokenizer=tokenizer)
    outputs = fill_masker(text)
    return {i["token_str"]: i["score"] for i in outputs}


mlm_examples = [
    "这款连[MASK]裙真漂亮",
    "这是杨[MASK]同款包包,精选优质皮料制作",
    "美颜去痘洁面[MASK]",
]

mlm_iface = gr.Interface(
    fn=fill_mask,
    inputs=gr.Textbox(
        label="输入文本",
        value="这款连[MASK]裙真漂亮"),
    # outputs=gr.Label(
    outputs=gr.Label(
        label="填词",
    ),
    examples=mlm_examples,
    title="文本填词(Fill Mask)",
    description='基于KPLUG预训练语言模型',
    article=article
)

if __name__ == "__main__":
    mlm_iface.launch()