Spaces:
Runtime error
Runtime error
File size: 6,674 Bytes
31959a0 |
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 |
{
"cells": [
{
"cell_type": "raw",
"id": "3d577700",
"metadata": {},
"source": [
"---\n",
"title: NLP with Spacy\n",
"description: This application shows different tasks of Spacy\n",
"show-code: False\n",
"params:\n",
" sent:\n",
" input: text\n",
" label: Enter the text to analyze\n",
" value: Apple is looking at buying U.K. startup for $1 billion\n",
" task:\n",
" input: select\n",
" label: Choose the tasks to do\n",
" choices: [Dependency Parser, Named Entity Recognition, Sentiment Analysis]\n",
" value: Named Entity Recognition\n",
" multi: True\n",
" compact:\n",
" input: checkbox\n",
" label: Compact(If you chose Dependency Parser, check the below boxes accordingly)\n",
" value: False\n",
" collapse_punctuation:\n",
" input: checkbox\n",
" label: Collapse Punctuation\n",
" value: False\n",
" collapse_phrases:\n",
" input: checkbox\n",
" label: Collapse Phrases\n",
" value: False\n",
" entities:\n",
" input: select\n",
" label: If you chose Named Entity Recognition task then select the named entities you want to see\n",
" choices: [PERSON, ORG, DATE, PRODUCT, LOC, GPE, LANGUAGE, FAC, NORP, MONEY]\n",
" multi: True\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "62ddeb5f",
"metadata": {},
"source": [
"# <center>NLP with SpaCy 💥</center>\n",
"### <center>A web application built with Mercury and SpaCy</center>"
]
},
{
"cell_type": "markdown",
"id": "a76b4050",
"metadata": {},
"source": [
"This web application is built by converting jupyter notebook using [mercury](https://mljar.com/mercury/). Mercury is a framework that can convert your jupyter notebook into an interactive web application with almost no code. We can share jupyter notebooks easily by hosting them on web platforms. All you need to do is add YAML config to the first cell of your notebook and mercury takes care of the rest. We can deploy the apps made by mercury on popular platforms like Heroku, AWS, and Hugging Face Spaces.\n",
"\n",
"##### From the official [Github](https://github.com/mljar/mercury) page\n",
"> Mercury can convert your jupyter notebook into \n",
"> - interactive web app\n",
"> - interactive slides\n",
"> - data dashboard\n",
"> - beautiful report\n",
"\n",
"All you need to do is start by installing it \n",
"**pip install mljar-mercury**\n",
"\n",
"The main objective of this web application is to demonstrate the ability of Mercury to create NLP web apps from jupyter notebooks. [SpaCy](https://spacy.io/) is used to perform the NLP tasks. This app can do tasks like visualizing Dependency Parser, Named Entity Recognition, and Sentiment Analysis. Follow the below steps to run this application.\n",
"\n",
"1. Enter the text you want to analyse in the text box that is present in the side bar. \n",
"2. Choose the task you want to perform. \n",
"3. Choose the required parameters for each task.\n",
"4. Click 'Run' \n",
"\n",
"Wait for the execution of the notebook. You will see your output below."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "84845499",
"metadata": {},
"outputs": [],
"source": [
"sent = 'Notebook in watch mode. All changes to Notebook will be automatically visible in Mercury'\n",
"compact = False\n",
"collapse_punctuation = True\n",
"collapse_phrases = False\n",
"entities = 'PERSON'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c506125",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import spacy\n",
"from spacy import displacy\n",
"from spacytextblob.spacytextblob import SpacyTextBlob\n",
"from simple_colors import *"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "16e95d2f",
"metadata": {},
"outputs": [],
"source": [
"def tasks(task):\n",
" if 'Dependency Parser' in task:\n",
" options = {\"compact\":compact, \"collapse_punct\":collapse_punctuation, \"collapse_phrases\":collapse_phrases, \"color\": \"black\", \"bg\":\"linear-gradient(90deg, #aa9cfc, #fc9ce7)\",\"distance\":100}\n",
" print(red('Dependency Parser', ['bold', 'underlined']))\n",
" displacy.render(doc, style=\"dep\", jupyter =True, options=options)\n",
" if 'Named Entity Recognition' in task:\n",
" try:\n",
" options = {\"ents\": entities }\n",
" print(red('Named Entities', ['bold', 'underlined']))\n",
" if len(doc.ents)==0:\n",
" print('Spacy has not detected any entities in the doc object.')\n",
" else:\n",
" displacy.render(doc, style=\"ent\", jupyter=True, options=options)\n",
" except:\n",
" print(red('Named Entities', ['bold', 'underlined']))\n",
" print('No entity is chosen. Please select atleast one entity from the list on the sidebar.')\n",
" if 'Sentiment Analysis' in task:\n",
" print(red('Sentiment Analysis', ['bold', 'underlined']))\n",
" if doc._.blob.polarity > 0:\n",
" print('Positive:'+ ' Polarity score is '+str(doc._.blob.polarity))\n",
" elif doc._.blob.polarity < 0:\n",
" print('Negative:'+ ' Polarity score is '+str(doc._.blob.polarity))\n",
" else:\n",
" print('Neutral:'+ ' Polarity score is '+str(doc._.blob.polarity))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2840678",
"metadata": {},
"outputs": [],
"source": [
"nlp = spacy.load(\"en_core_web_sm\")\n",
"nlp.add_pipe('spacytextblob')\n",
"doc = nlp(sent)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "62adc4ef",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"tasks(task)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
} |