问答

创建日期:2024-06-21
更新日期:2025-04-20

问答(使用流水线API)

模型下载1:distilbert/distilbert-base-cased-distilled-squad · Hugging Face

模型下载2:distilbert/distilbert-base-cased-distilled-squad · HF Mirror

from transformers import pipeline

question_answerer = pipeline(
    "question-answering",
    model="distilbert/distilbert-base-cased-distilled-squad",
    framework="pt",
)
result = question_answerer(
    {
        "question": "What is the name of the repository ?",
        "context": "Pipeline has been included in the huggingface/transformers repository",
    }
)
print(result)

输出:

{'score': 0.30970099568367004, 'start': 34, 'end': 58, 'answer': 'huggingface/transformers'}

问答(使用PyTorch)

模型下载1:distilbert/distilbert-base-cased-distilled-squad · Hugging Face

模型下载2:distilbert/distilbert-base-cased-distilled-squad · HF Mirror

from transformers import DistilBertTokenizer, DistilBertForQuestionAnswering
import torch

path = "distilbert/distilbert-base-cased-distilled-squad"

tokenizer = DistilBertTokenizer.from_pretrained(path)
model = DistilBertForQuestionAnswering.from_pretrained(path)

inputs = tokenizer(
    "What is the name of the repository?",
    "Pipeline has been included in the huggingface/transformers repository",
    return_tensors="pt",
)

with torch.no_grad():
    outputs = model(**inputs)

answer_start_index = int(torch.argmax(outputs.start_logits, axis=-1)[0])
answer_end_index = int(torch.argmax(outputs.end_logits, axis=-1)[0])

predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
result = tokenizer.decode(predict_answer_tokens)

print(result)

输出:

huggingface / transformers

简介

一个来自三线小城市的程序员开发经验总结。