Transformers(3) - 質疑応答

今回は、Transformersを使って質疑応答を行います。

(Transformersのインストールは完了している想定となります。)

質疑応答

質疑応答では、コンテキスト質問からコンテキスト内に含まれる応答を抽出します。

ソースとしては、タスク名に‘question-answering’を指定したパイプラインを作成し、コンテキストと質問を渡して応答を推論します。

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
12
13
from transformers import pipeline

# 質疑応答のパイプラインの準備
nlp = pipeline('question-answering')

# コンテキスト
context = 'Huggingface Transformers is a deep learning framework provided by Huggingface that specializes in natural language processing. It supports both TensorFlow and PyTorch. You can use deep learning to solve natural language processing tasks such as text classification, question answering, and summary.'

# 質問
question = 'What is a natural language processing task?'

# 推論
print(nlp(question=question, context=context))

実行結果は下記の通りです。

[実行結果]

1
{'score': 0.6564476490020752, 'start': 245, 'end': 297, 'answer': 'text classification, question answering, and summary'}

スコア65で、テキスト分類、質疑応答、要約という結果になりました。

正しい応答になっていると思います。


次回は、要約を行います。