language: tr
土耳其语问答BERT模型
使用方法
from transformers import pipeline
nlp = pipeline('question-answering', model='lserinol/bert-turkish-question-answering', tokenizer='lserinol/bert-turkish-question-answering')
nlp({
'question': "安卡拉有多少个区?",
'context': r"""土耳其的首都是安卡拉。全国最大的行政区划是省,共有81个省。这些省又划分为区,总共有973个区。"""
})
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch
tokenizer = AutoTokenizer.from_pretrained("lserinol/bert-turkish-question-answering")
model = AutoModelForQuestionAnswering.from_pretrained("lserinol/bert-turkish-question-answering")
text = r"""
安卡拉被宣布为首都后(1923年10月13日),城市迅速发展,成为土耳其人口第二多的省份。
土耳其共和国成立初期,该省经济以农业和畜牧业为主,至今仍有半数土地用于农业。
经济活动主要基于贸易和工业。农业和畜牧业的比重正在逐渐下降。安卡拉及周边地区的公共和私营部门投资,
吸引了大量来自其他省份的人口迁移。自共和国成立以来,其人口增长速度是全国平均水平的两倍。
约四分之三的人口从事服务行业,如公务员、交通、通讯和贸易等工作,四分之一从事工业,
2%从事农业。工业主要集中在纺织、食品和建筑领域。如今,投资最多的是国防、金属和汽车行业。
作为土耳其拥有最多大学的省份,安卡拉拥有大学文凭的人口比例是全国平均水平的两倍。
这些受过教育的人口为技术密集型投资提供了所需的劳动力。通过高速公路、铁路和航空,
可以从安卡拉到达土耳其其他城市。安卡拉作为首都,也是土耳其大国民议会(TBMM)的所在地。
"""
questions = [
"安卡拉在哪一年成为首都?",
"安卡拉什么时候成为首都?",
"如何从安卡拉前往其他城市?",
"TBMM是什么的缩写?"
]
for question in questions:
inputs = tokenizer(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
)
answer_end = torch.argmax(answer_end_scores) + 1
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
print(f"问题: {question}")
print(f"答案: {answer}\n")