基于sciBert的端到端封闭域问答系统

Idmeneo-cdQa的Python项目详细描述


封闭域问答

Build StatuscodecovPyPI VersionPyPI DownloadsBinderColabContributor CovenantPRs WelcomeGitHub

一个端到端的封闭域问答系统。建在拥抱面图书馆之上。在

详细的cdQA

如果您对理解系统如何工作及其实现感兴趣,我们编写了一个article on Medium,并给出了一个高级解释。在

我们还在由Feedly组织的9 NLP早餐会上做了一个报告。你可以去看看here。在

目录

安装

有pip

pip install cdqa

来源

^{pr2}$

硬件要求

已经用以下方法进行了实验:

  • CPUAWS EC2t2.medium深度学习AMI(Ubuntu)22.0版
  • GPUAWS EC2p3.2xlarge深度学习AMI(Ubuntu)版本22.0+Tesla V100 16GB。在

入门

准备数据

手动

要使用cdQA,您需要使用以下列创建pandas数据帧:

titleparagraphs
The Article Title[Paragraph 1 of Article, ... , Paragraph N of Article]

带转换器

cdqa转换器的目标是使从原始文档数据库创建此数据帧变得容易。例如,pdf_converter可以从包含.pdf文件的目录创建cdqa数据帧:

fromcdqa.utils.convertersimportpdf_converterdf=pdf_converter(directory_path='path_to_pdf_folder')

您需要安装Java OpenJDK才能使用此转换器。我们目前有以下转换器:

  • pdf格式
  • 降价

我们计划在未来改进和增加更多的转换器。敬请期待!在

下载预先训练的模型和数据

您可以从GitHub releases手动下载模型和数据,也可以使用我们的下载功能:

fromcdqa.utils.downloadimportdownload_squad,download_model,download_bnpp_datadirectory='path-to-directory'# Downloading datadownload_squad(dir=directory)download_bnpp_data(dir=directory)# Downloading pre-trained BERT fine-tuned on SQuAD 1.1download_model('bert-squad_1.1',dir=directory)# Downloading pre-trained DistilBERT fine-tuned on SQuAD 1.1download_model('distilbert-squad_1.1',dir=directory)

训练模型

使用预先培训过的读者在你的语料库中安装管道:

importpandasaspdfromastimportliteral_evalfromcdqa.pipelineimportQAPipelinedf=pd.read_csv('your-custom-corpus-here.csv',converters={'paragraphs':literal_eval})cdqa_pipeline=QAPipeline(reader='bert_qa.joblib')# use 'distilbert_qa.joblib' for DistilBERT instead of BERTcdqa_pipeline.fit_retriever(df=df)

如果您想在自定义班组(如带注释的数据集)上微调读卡器:

cdqa_pipeline=QAPipeline(reader='bert_qa.joblib')# use 'distilbert_qa.joblib' for DistilBERT instead of BERTcdqa_pipeline.fit_reader('path-to-custom-squad-like-dataset.json')

微调后保存读卡器模型:

cdqa_pipeline.dump_reader('path-to-save-bert-reader.joblib')

做出预测

要获得给定输入查询的最佳预测:

cdqa_pipeline.predict(query='your question')

要获得N个最佳预测:

cdqa_pipeline.predict(query='your question',n_predictions=N)

也有可能改变检索器分数的权重 与最终排名得分计算中的读者得分(默认值为0.35,在1.1班-公开赛的开发集上显示为最佳权重)

cdqa_pipeline.predict(query='your question',retriever_score_weight=0.35)

评估模型

为了评估自定义数据集上的模型,需要对其进行注释。注释过程可分为3个步骤:

  1. 将pandas数据帧转换为一个具有SQuAD格式的json文件:

    fromcdqa.utils.convertersimportdf2squadjson_data=df2squad(df=df,squad_version='v1.1',output_dir='.',filename='dataset-name')
  2. 使用注释器添加基本真理问答对:

    请参考我们的^{},这是一个基于web的注释器,用于使用SQuAD格式的封闭域问答数据集。在

  3. 评估管道对象:

    fromcdqa.utils.evaluationimportevaluate_pipelineevaluate_pipeline(cdqa_pipeline,'path-to-annotated-dataset.json')
  4. 评估读者:

    fromcdqa.utils.evaluationimportevaluate_readerevaluate_reader(cdqa_pipeline,'path-to-annotated-dataset.json')

笔记本示例

我们在examples目录下准备了一些笔记本示例。在

您也可以使用BinderGoogle Colaboratory直接使用这些笔记本示例:

^{tb2}$

Binder和googlecolaboratory提供了临时环境,启动速度可能很慢,但是如果您想轻松开始使用cdQA,我们建议您使用它们。在

部署

手动

您可以通过执行以下操作来部署cdQAREST API:

exportdataset_path=path-to-dataset.csv
exportreader_path=path-to-reader-model

FLASK_APP=api.py flask run -h 0.0.0.0

现在可以请求测试API(这里使用HTTPie):

http localhost:5000/api query=='your question here'

如果您希望在cdQA系统之上提供一个用户界面,请执行以下操作为cdQA开发的web界面cdQA-ui的说明。在

贡献

阅读我们的Contributing Guidelines。在

参考文献

TypeTitleAuthorYear
:video_camera: VideoStanford CS224N: NLP with Deep Learning Lecture 10 – Question AnsweringChristopher Manning2019
:newspaper: PaperReading Wikipedia to Answer Open-Domain QuestionsDanqi Chen, Adam Fisch, Jason Weston, Antoine Bordes2017
:newspaper: PaperNeural Reading Comprehension and BeyondDanqi Chen2018
:newspaper: PaperBERT: Pre-training of Deep Bidirectional Transformers for Language UnderstandingJacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova2018
:newspaper: PaperContextual Word Representations: A Contextual IntroductionNoah A. Smith2019
:newspaper: PaperEnd-to-End Open-Domain Question Answering with BERTseriniWei Yang, Yuqing Xie, Aileen Lin, Xingyu Li, Luchen Tan, Kun Xiong, Ming Li, Jimmy Lin2019
:newspaper: PaperData Augmentation for BERT Fine-Tuning in Open-Domain Question AnsweringWei Yang, Yuqing Xie, Luchen Tan, Kun Xiong, Ming Li, Jimmy Lin2019
:newspaper: PaperPassage Re-ranking with BERTRodrigo Nogueira, Kyunghyun Cho2019
:newspaper: PaperMRQA: Machine Reading for Question AnsweringJonathan Berant, Percy Liang, Luke Zettlemoyer2019
:newspaper: PaperUnsupervised Question Answering by Cloze TranslationPatrick Lewis, Ludovic Denoyer, Sebastian Riedel2019
:computer: FrameworkScikit-learn: Machine Learning in PythonPedregosa et al.2011
:computer: FrameworkPyTorchAdam Paszke, Sam Gross, Soumith Chintala, Gregory Chanan2016
:computer: FrameworkTransformers: State-of-the-art Natural Language Processing for TensorFlow 2.0 and PyTorch.Hugging Face2018

许可证

Apache-2.0

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在Android上使用BroadcastReceiver从Java报警执行本机代码   解析模板[]时发生java错误,模板可能不存在,或者任何已配置的模板解析程序都无法访问该模板   从命令行创建Java包的正确方法是什么?(与Maven的麻烦)   带有日期参数的java时区和MessageFormat   Java返回一个新数组,其中包含数组参数元素的平方   带有可选右括号的java年正则表达式   writeCharacteristic Android上的java空指针异常   在ScheduledThreadPoolExecutor中重写泛型方法的java问题   java如何在从两个不同的组合框中选择两个元素时生成通知?   java试图在正则表达式中捕获多个组,同时跳过其他组   Jasper报告中的java动态图像大小   JavaFX(带FXML)MVC:模型使用控制器   我们可以用java回滚文件mkdir吗?   java Android,无效双:“空白”字段使程序崩溃   java JSoup解析结构不良的HTML   java Jackson:(反)序列化程序和StdConverter之间有什么区别?   Crudepository(spring boot)中自定义方法的java问题