diff --git a/Chapter12/BERT_classification/BERT_classifier_with_transfer_learning.ipynb b/Chapter12/BERT_classification/BERT_classifier_with_transfer_learning.ipynb new file mode 100644 index 0000000..7d42c6e --- /dev/null +++ b/Chapter12/BERT_classification/BERT_classifier_with_transfer_learning.ipynb @@ -0,0 +1,111 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "BERT_classifier_with_transfer_learning.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "b1ej6UMvmS85" + }, + "source": [ + "# **Problem: Create a BERT model to answer questions and summarize.**\n", + "\n", + "Python program which creates a BERT model to answer questions and summarize.\n", + "\n", + "Run all the cells. After executing the last cell, you will get the answer and the summarized text.\n", + "\n", + "**Notes:**\n", + "\n", + "Following things are needed to be checked before running the program.\n", + " 1. transformers module is needed to run this program in a notebook. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oWQQaoyfoFHv" + }, + "source": [ + "# **Import Modules**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Q1KG9DzAoLLn" + }, + "source": [ + "# Import the transformers pipelines\n", + "from transformers import pipeline\n", + "\n", + "summarizer = pipeline(\"summarization\")\n", + "nlp = pipeline(\"question-answering\")" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YVKxFe7voS4w" + }, + "source": [ + "# **Sample text and question**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kiyQ9sE3orDi" + }, + "source": [ + "# Open and read the article\n", + "context = r\"The four largest cities in the Netherlands are Amsterdam, Rotterdam, The Hague and Utrecht.[17] Amsterdam is the country's most populous city and nominal capital,[18] while The Hague holds the seat of the States General, Cabinet and Supreme Court.[19] The Port of Rotterdam is the busiest seaport in Europe, and the busiest in any country outside East Asia and Southeast Asia, behind only China and Singapore.\"\n", + "\n", + "# Question to ask\n", + "question = \"What is the capital of the Netherlands?\"" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZhgLLPw9oucv" + }, + "source": [ + "# **Get Answer and Summarized text**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Gi5a_V7voxab" + }, + "source": [ + "# Get the answer for the question\n", + "result = nlp(question=question, context=context)\n", + "print('Answer :', result['answer'])\n", + "\n", + "# Get the summarized text\n", + "print(summarizer(context, max_length=51, min_length=30, do_sample=False))" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Chapter12/BERT_classification/BERT_classifier_with_transfer_learning.py b/Chapter12/BERT_classification/BERT_classifier_with_transfer_learning.py index 41719e0..21aea38 100644 --- a/Chapter12/BERT_classification/BERT_classifier_with_transfer_learning.py +++ b/Chapter12/BERT_classification/BERT_classifier_with_transfer_learning.py @@ -1,3 +1,37 @@ -# TODO: Create a BERT model to answer questions and summarize. -# Example is here: https://colab.research.google.com/drive/1GtNcdhO0OgC0IOHrlBZ2xTspTaJGdk0j?usp=sharing -# TODO: Code should be well commented. \ No newline at end of file +'''Copyright (c) 2021 AIClub + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without restriction, including without +limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE.''' + +# Python program which creates a BERT model to answer questions and summarize. + +# Import the transformers pipelines +from transformers import pipeline + +summarizer = pipeline("summarization") +nlp = pipeline("question-answering") + +# Open and read the article +context = r"The four largest cities in the Netherlands are Amsterdam, Rotterdam, The Hague and Utrecht.[17] Amsterdam is the country's most populous city and nominal capital,[18] while The Hague holds the seat of the States General, Cabinet and Supreme Court.[19] The Port of Rotterdam is the busiest seaport in Europe, and the busiest in any country outside East Asia and Southeast Asia, behind only China and Singapore." + +# Question to ask +question = "What is the capital of the Netherlands?" + +# Get the answer for the question +result = nlp(question=question, context=context) +print('Answer :', result['answer']) + +# Get the summarized text +print(summarizer(context, max_length=51, min_length=30, do_sample=False)) \ No newline at end of file diff --git a/Chapter12/BERT_classification/README.md b/Chapter12/BERT_classification/README.md index e69de29..151b4d1 100644 --- a/Chapter12/BERT_classification/README.md +++ b/Chapter12/BERT_classification/README.md @@ -0,0 +1,11 @@ +What it does : + + 1. This python program creates a BERT model to answer questions and summarizes it. + +Dependancies : + + 1. transformers module is needed to be installed in the local machine to run this program. + + + + \ No newline at end of file