Views
No views yet
1from transformers import AutoModelForQuestionAnswering, AutoTokenizer
2question = 'アレクサンダー・グラハム・ベルは、どこで生まれたの?'
3context = 'アレクサンダー・グラハム・ベルは、スコットランド生まれの科学者、発明家、工学者である。世界初の>実用的電話の発明で知られている。'
4model = AutoModelForQuestionAnswering.from_pretrained(
5 'ybelkada/japanese-roberta-question-answering')
6tokenizer = AutoTokenizer.from_pretrained(
7 'ybelkada/japanese-roberta-question-answering')
8inputs = tokenizer(
9 question, context, add_special_tokens=True, return_tensors="pt")
10input_ids = inputs["input_ids"].tolist()[0]
11outputs = model(**inputs)
12answer_start_scores = outputs.start_logits
13answer_end_scores = outputs.end_logits
14# Get the most likely beginning of answer with the argmax of the score.
15answer_start = torch.argmax(answer_start_scores)
16# Get the most likely end of answer with the argmax of the score.
17# 1 is added to `answer_end` because the index pointed by score is inclusive.
18answer_end = torch.argmax(answer_end_scores) + 1
19answer = tokenizer.convert_tokens_to_string(
20 tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
21# answer = 'スコットランド'Pipeline and I was able to reproduce the error, needs a further investigation