Views
No views yet
1from transformers import AutoConfig, AutoModelForTokenClassification, BertTokenizer
2from transformers import TokenClassificationPipeline
3import json
4
5model_path = "wilsontam/dstc9_ner"
6
7label_map = {
8"LABEL_0": "O",
9"LABEL_1": "B-hotel",
10"LABEL_2": "I-hotel",
11"LABEL_3": "B-restaurant",
12"LABEL_4": "I-restaurant",
13"LABEL_5": "B-attraction",
14"LABEL_6": "I-attraction",
15}
16
17config = AutoConfig.from_pretrained(
18 model_path,
19 num_labels=len(label_map),
20)
21model = AutoModelForTokenClassification.from_pretrained(
22 model_path,
23 from_tf=False,
24 config=config,
25)
26tokenizer = BertTokenizer.from_pretrained(
27 model_path,
28)
29
30# device=-1: cpu, device=0: gpu
31pipeline = TokenClassificationPipeline(model, tokenizer, device=-1)
32
33tokens = pipeline(["i want to book the hilton hotel near china town.", "can you reserve A & B restaurant for me?"])