Views
No views yet
1from transformers import BertTokenizerFast, BertForQuestionAnswering, pipeline
2model_name = "NchuNLP/Chinese-Question-Answering"
3tokenizer = BertTokenizerFast.from_pretrained(model_name)
4model = BertForQuestionAnswering.from_pretrained(model_name)
5
6# a) Get predictions
7nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
8QA_input = {
9 'question': '中興大學在哪裡?',
10 'context': '國立中興大學(簡稱興大、NCHU),是位於臺中的一所高等教育機構。中興大學以農業科學、農業經濟學、獸醫、生命科學、轉譯醫學、生醫工程、生物科技、綠色科技等研究領域見長 。近年中興大學與臺中榮民總醫院、彰化師範大學、中國醫藥大學等機構合作,聚焦於癌症醫學、免疫醫學及醫學工程三項領域,將實驗室成果逐步應用到臨床上,未來「衛生福利部南投醫院中興院區」將改為「國立中興大學醫學院附設醫院」。興大也與臺中市政府合作,簽訂合作意向書,共同推動數位文化、智慧城市等面相帶動區域發展。'
11}
12res = nlp(QA_input)
13
14{'score': 1.0, 'start': 21, 'end': 23, 'answer': '臺中'}
15
16# b) Inside the Question answering pipeline
17
18inputs = tokenizer(query, text, return_tensors="pt",padding=True, truncation=True, max_length=512, stride=256)
19outputs = model(**inputs)
20
21sequence_ids = inputs.sequence_ids()
22# Mask everything apart from the tokens of the context
23mask = [i != 1 for i in sequence_ids]
24# Unmask the [CLS] token
25mask[0] = False
26mask = torch.tensor(mask)[None]
27
28start_logits[mask] = -10000
29end_logits[mask] = -10000
30
31start_probabilities = torch.nn.functional.softmax(start_logits, dim=-1)[0]
32end_probabilities = torch.nn.functional.softmax(end_logits, dim=-1)[0]
33
34scores = start_probabilities[:, None] * end_probabilities[None, :]
35
36max_index = scores.argmax().item()
37start_index = max_index // scores.shape[1]
38end_index = max_index % scores.shape[1]
39
40
41inputs_with_offsets = tokenizer(query, text, return_offsets_mapping=True)
42offsets = inputs_with_offsets["offset_mapping"]
43
44start_char, _ = offsets[start_index]
45_, end_char = offsets[end_index]
46answer = text[start_char:end_char]
47
48result = {
49 "answer": answer,
50 "start": start_char,
51 "end": end_char,
52 "score": scores[start_index, end_index],
53}
54print(result)
55