{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: transformers in e:\\coding\\anaconda\\lib\\site-packages (4.15.0)\n", "Requirement already satisfied: filelock in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (3.0.12)\n", "Requirement already satisfied: pyyaml>=5.1 in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (5.3)\n", "Requirement already satisfied: requests in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (2.22.0)\n", "Requirement already satisfied: tqdm>=4.27 in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (4.42.1)\n", "Requirement already satisfied: tokenizers<0.11,>=0.10.1 in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (0.10.3)\n", "Requirement already satisfied: packaging>=20.0 in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (20.1)\n", "Requirement already satisfied: importlib-metadata; python_version < \"3.8\" in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (3.3.0)\n", "Requirement already satisfied: sacremoses in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (0.0.46)\n", "Requirement already satisfied: numpy>=1.17 in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (1.19.2)\n", "Requirement already satisfied: regex!=2019.12.17 in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (2021.11.10)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.1.0 in e:\\coding\\anaconda\\lib\\site-packages (from transformers) (0.2.1)\n", "Requirement already satisfied: certifi>=2017.4.17 in e:\\coding\\anaconda\\lib\\site-packages (from requests->transformers) (2021.10.8)\n", "Requirement already satisfied: idna<2.9,>=2.5 in e:\\coding\\anaconda\\lib\\site-packages (from requests->transformers) (2.8)\n", "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in e:\\coding\\anaconda\\lib\\site-packages (from requests->transformers) (3.0.4)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in e:\\coding\\anaconda\\lib\\site-packages (from requests->transformers) (1.25.8)\n", "Requirement already satisfied: pyparsing>=2.0.2 in e:\\coding\\anaconda\\lib\\site-packages (from packaging>=20.0->transformers) (2.4.6)\n", "Requirement already satisfied: six in e:\\coding\\anaconda\\lib\\site-packages (from packaging>=20.0->transformers) (1.14.0)\n", "Requirement already satisfied: zipp>=0.5 in e:\\coding\\anaconda\\lib\\site-packages (from importlib-metadata; python_version < \"3.8\"->transformers) (2.2.0)\n", "Requirement already satisfied: typing-extensions>=3.6.4; python_version < \"3.8\" in e:\\coding\\anaconda\\lib\\site-packages (from importlib-metadata; python_version < \"3.8\"->transformers) (3.7.4.3)\n", "Requirement already satisfied: joblib in e:\\coding\\anaconda\\lib\\site-packages (from sacremoses->transformers) (0.14.1)\n", "Requirement already satisfied: click in e:\\coding\\anaconda\\lib\\site-packages (from sacremoses->transformers) (7.0)\n" ] } ], "source": [ "!pip install transformers" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from transformers import pipeline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For text summarization" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "No model was supplied, defaulted to sshleifer/distilbart-cnn-12-6 (https://huggingface.co/sshleifer/distilbart-cnn-12-6)\n" ] } ], "source": [ "summarizer = pipeline('summarization')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "text = \"\"\" New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.\n", "... A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.\n", "... Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared \"I do\" five more times, sometimes only within two weeks of each other.\n", "... In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her \"first and only\" marriage.\n", "... Barrientos, now 39, is facing two criminal counts of \"offering a false instrument for filing in the first degree,\" referring to her false statements on the\n", "... 2010 marriage license application, according to court documents.\n", "... Prosecutors said the marriages were part of an immigration scam.\n", "... On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.\n", "... After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective\n", "... Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.\n", "... All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.\n", "... Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.\n", "... Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.\n", "... The case was referred to the Bronx District Attorney\\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\\'s\n", "... Investigation Division. Seven of the men are from so-called \"red-flagged\" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.\n", "... Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.\n", "... If convicted, Barrientos faces up to four years in prison. Her next court appearance is scheduled for May 18.\n", "... \"\"\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'summary_text': ' Liana Barrientos, 39, is charged with two counts of \"offering a false instrument for filing in the first degree\" In total, she has been married 10 times, with nine of her marriages occurring between 1999 and 2002 . At one time, she was married to eight men at once, prosecutors say .'}]\n" ] } ], "source": [ "print(summarizer(text, max_length=150, min_length=50, do_sample=False))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For Context based question answering" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline\n", "\n", "model_name = \"deepset/roberta-base-squad2\"\n", "\n", "nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)\n", "QA_input = {\n", " 'question': 'Why is model conversion important?',\n", " 'context': 'The option to convert models between FARM and transformers gives freedom to the user and let people easily switch between frameworks.'\n", "}\n", "res = nlp(QA_input)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'score': 0.21171443164348602, 'start': 59, 'end': 84, 'answer': 'gives freedom to the user'}\n" ] } ], "source": [ "print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using gradio" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: gradio in e:\\coding\\anaconda\\lib\\site-packages (2.6.4)\n", "Requirement already satisfied: requests in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (2.22.0)\n", "Requirement already satisfied: markdown2 in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (2.4.2)\n", "Requirement already satisfied: Flask>=1.1.1 in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (1.1.1)\n", "Requirement already satisfied: matplotlib in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (3.1.3)\n", "Requirement already satisfied: flask-cachebuster in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (1.0.0)\n", "Requirement already satisfied: numpy in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (1.19.2)\n", "Requirement already satisfied: paramiko in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (2.7.1)\n", "Requirement already satisfied: pydub in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (0.25.1)\n", "Requirement already satisfied: Flask-Cors>=3.0.8 in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (3.0.10)\n", "Requirement already satisfied: pillow in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (7.0.0)\n", "Requirement already satisfied: pandas in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (1.0.1)\n", "Requirement already satisfied: pycryptodome in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (3.12.0)\n", "Requirement already satisfied: analytics-python in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (1.4.0)\n", "Requirement already satisfied: ffmpy in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (0.3.0)\n", "Requirement already satisfied: Flask-Login in e:\\coding\\anaconda\\lib\\site-packages (from gradio) (0.5.0)\n", "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in e:\\coding\\anaconda\\lib\\site-packages (from requests->gradio) (3.0.4)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in e:\\coding\\anaconda\\lib\\site-packages (from requests->gradio) (1.25.8)\n", "Requirement already satisfied: idna<2.9,>=2.5 in e:\\coding\\anaconda\\lib\\site-packages (from requests->gradio) (2.8)\n", "Requirement already satisfied: certifi>=2017.4.17 in e:\\coding\\anaconda\\lib\\site-packages (from requests->gradio) (2021.10.8)\n", "Requirement already satisfied: Werkzeug>=0.15 in e:\\coding\\anaconda\\lib\\site-packages (from Flask>=1.1.1->gradio) (1.0.0)\n", "Requirement already satisfied: itsdangerous>=0.24 in e:\\coding\\anaconda\\lib\\site-packages (from Flask>=1.1.1->gradio) (1.1.0)\n", "Requirement already satisfied: Jinja2>=2.10.1 in e:\\coding\\anaconda\\lib\\site-packages (from Flask>=1.1.1->gradio) (2.11.1)\n", "Requirement already satisfied: click>=5.1 in e:\\coding\\anaconda\\lib\\site-packages (from Flask>=1.1.1->gradio) (7.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in e:\\coding\\anaconda\\lib\\site-packages (from matplotlib->gradio) (1.1.0)\n", "Requirement already satisfied: python-dateutil>=2.1 in e:\\coding\\anaconda\\lib\\site-packages (from matplotlib->gradio) (2.8.1)\n", "Requirement already satisfied: cycler>=0.10 in e:\\coding\\anaconda\\lib\\site-packages (from matplotlib->gradio) (0.10.0)\n", "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in e:\\coding\\anaconda\\lib\\site-packages (from matplotlib->gradio) (2.4.6)\n", "Requirement already satisfied: bcrypt>=3.1.3 in e:\\coding\\anaconda\\lib\\site-packages (from paramiko->gradio) (3.1.7)\n", "Requirement already satisfied: cryptography>=2.5 in e:\\coding\\anaconda\\lib\\site-packages (from paramiko->gradio) (2.8)\n", "Requirement already satisfied: pynacl>=1.0.1 in e:\\coding\\anaconda\\lib\\site-packages (from paramiko->gradio) (1.3.0)\n", "Requirement already satisfied: Six in e:\\coding\\anaconda\\lib\\site-packages (from Flask-Cors>=3.0.8->gradio) (1.14.0)\n", "Requirement already satisfied: pytz>=2017.2 in e:\\coding\\anaconda\\lib\\site-packages (from pandas->gradio) (2019.3)\n", "Requirement already satisfied: backoff==1.10.0 in e:\\coding\\anaconda\\lib\\site-packages (from analytics-python->gradio) (1.10.0)\n", "Requirement already satisfied: monotonic>=1.5 in e:\\coding\\anaconda\\lib\\site-packages (from analytics-python->gradio) (1.6)\n", "Requirement already satisfied: MarkupSafe>=0.23 in e:\\coding\\anaconda\\lib\\site-packages (from Jinja2>=2.10.1->Flask>=1.1.1->gradio) (1.1.1)\n", "Requirement already satisfied: setuptools in e:\\coding\\anaconda\\lib\\site-packages (from kiwisolver>=1.0.1->matplotlib->gradio) (45.2.0.post20200210)\n", "Requirement already satisfied: cffi>=1.1 in e:\\coding\\anaconda\\lib\\site-packages (from bcrypt>=3.1.3->paramiko->gradio) (1.14.0)\n", "Requirement already satisfied: pycparser in e:\\coding\\anaconda\\lib\\site-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko->gradio) (2.19)\n" ] } ], "source": [ "!pip install gradio" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "\n", "examples = [\n", " [ 'Question-Answer',\n", " '',\n", " 'The option to convert models between FARM and transformers gives freedom to the user and let people easily switch between frameworks.',\n", " 'Why is model conversion important?'\n", " ],\n", " [ 'Question-Answer',\n", " '',\n", " \"The Amazon rainforest is a moist broadleaf forest that covers most of the Amazon basin of South America\", \n", " \"Which continent is the Amazon rainforest in?\"\n", " ],\n", " [ 'Question-Answer',\n", " '',\n", " 'I am a Programmer.',\n", " 'Who am I?' \n", " ]\n", " ]\n", "\n", "def summarize_text(text):\n", " summary = summarizer(text, max_length=130, min_length=30, do_sample=False)\n", " summary = summary[0]['summary_text']\n", " return summary\n", "\n", "def question_answer(context, question):\n", " QA_input = {\n", " 'context': context,\n", " 'question': question\n", " }\n", " res = nlp(QA_input)\n", " return (res['answer'])\n", "\n", "def home_func(model_choice, summ_text, qa_context, qa_question):\n", " if model_choice==\"Text Summarizer\":\n", " if summ_text == \"\":\n", " return \"Input correct text to be summarized\"\n", " return summarize_text(summ_text)\n", " elif model_choice==\"Question-Answer\":\n", " if qa_context == \"\" or qa_question == \"\":\n", " return \"Choose correct Context and associated questions\"\n", " \n", " return question_answer(qa_context, qa_question) \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check of above function" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "South America\n", " Liana Barrientos, 39, is charged with two counts of \"offering a false instrument for filing in the first degree\" In total, she has been married 10 times, with nine of her marriages occurring between 1999 and 2002 . At one time, she was married to eight men at once, prosecutors say .\n" ] } ], "source": [ "print(home_func(\"Question-Answer\", \"\", examples[1][2], examples[1][3]))\n", "print(home_func(\"Text Summarizer\", text, \"\", \"\"))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "iface = gr.Interface(fn = home_func, \n", " inputs = [gr.inputs.Dropdown([\"Text Summarizer\", \"Question-Answer\"], type=\"value\"), \n", " gr.inputs.Textbox(lines=5, placeholder=\"Enter your text here...\", label=\"Text to be summarized\"),\n", " gr.inputs.Textbox(lines=5, placeholder=\"Choose from examples\", label=\"Context\"),\n", " gr.inputs.Textbox(lines=5, placeholder=\"Choose from examples\", label=\"Question\")],\n", " outputs=\"text\",\n", " examples=examples)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861/\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(, 'http://127.0.0.1:7861/', None)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iface.launch()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }