Когда я запускаю следующий код
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
text = r"""
As checked Dis is not yet on boarded to ARB portal, hence we cannot upload the invoices in portal
"""
questions = [
"Dis asked if it is possible to post the two invoice in ARB.I have not access so I wanted to check if you would be able to do it.",
]
for question in questions:
inputs = tokenizer.encode_plus(question, text, add_special_tokens=True, return_tensors="pt")
input_ids = inputs["input_ids"].tolist()[0]
text_tokens = tokenizer.convert_ids_to_tokens(input_ids)
answer_start_scores, answer_end_scores = model(**inputs)
answer_start = torch.argmax(
answer_start_scores
) # Get the most likely beginning of answer with the argmax of the score
answer_end = torch.argmax(answer_end_scores) + 1 # Get the most likely end of answer with the argmax of the score
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
print(f"Question: {question}")
print(f"Answer: {answer}\n")
Вот что я получаю:
Question: Dis asked if it is possible to post the two invoice in ARB.I have not access so I wanted to check if you would be able to do it.
Answer: dis is not yet on boarded to ARB portal
Как мне получить оценку за этот ответ? Оценка здесь очень похожа на то, что я получаю, когда запускаю конвейер вопрос-ответ.
Я должен использовать этот подход, поскольку конвейер вопрос-ответ при использовании выдает мне ключевую ошибку для приведенного ниже кода.
from transformers import pipeline
nlp = pipeline("question-answering")
context = r"""
As checked Dis is not yet on boarded to ARB portal, hence we cannot upload the invoices in portal.
"""
print(nlp(question="Dis asked if it is possible to post the two invoice in ARB?", context=context))
DistilBertForQuestionAnswering
, а неbert-large-uncased-whole-word-masking-finetuned-squad
. Вы можете сделать это случайно, указав параметр модели:nlp = pipeline("question-answering", model='bert-large-uncased-whole-word-masking-finetuned-squad')
. Что также дает результат для вашего примера. Вы также должны иметь в виду, что конвейер более сложен, чем ваш код, и охватывает больше случаев в пре- и постобработке. Поэтому будут еще примеры, которые дадут 1/2 - person cronoik   schedule 26.08.2020