使用huggingface pipeline实现抽取式问答question-answering

1:

from huggingface_hub.hf_api import HfFolder
HfFolder.save_token('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')
from transformers import pipeline
qa_model = pipeline("question-answering", "timpal0l/mdeberta-v3-base-squad2")
question = "Where do I live?"
context = "My name is Tim and I live in Sweden."
print(qa_model(question = question, context = context))

2:

from huggingface_hub.hf_api import HfFolder
HfFolder.save_token('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')


from transformers import AutoModelForQuestionAnswering,AutoTokenizer,pipeline
context = """
普希金从那里学习人民的语言,吸取了许多有益的养料,这一切对普希金后来的创作产生了很大的影响。这两年里,普希金创作了不少优秀的作品,如《囚徒》、《致大海》、《致凯恩》和《假如生活欺骗了你》等几十首抒情诗,叙事诗《努林伯爵》,历史剧《鲍里斯·戈都诺夫》,以及《叶甫盖尼·奥涅金》前六章。
"""
mode_name = 'liam168/qa-roberta-base-chinese-extractive'
model = AutoModelForQuestionAnswering.from_pretrained(mode_name)
tokenizer = AutoTokenizer.from_pretrained(mode_name)
QA = pipeline('question-answering', model=model, tokenizer=tokenizer)
QA_input = {'question': "著名诗歌《假如生活欺骗了你》的作者是?",'context': context}
print(QA(QA_input))


你可能感兴趣的:(huggingface,java,服务器,前端)