Spaces:
Running
Running
File size: 18,886 Bytes
fd68a4d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
{
"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",
" <iframe\n",
" width=\"900\"\n",
" height=\"500\"\n",
" src=\"http://127.0.0.1:7861/\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1464937bcc8>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(<Flask 'gradio.networking'>, '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
}
|