Spaces:
Runtime error
Runtime error
Create mercury-spacy.ipynb
Browse files- mercury-spacy.ipynb +187 -0
mercury-spacy.ipynb
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "raw",
|
5 |
+
"id": "3d577700",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"---\n",
|
9 |
+
"title: NLP with Spacy\n",
|
10 |
+
"description: This application shows different tasks of Spacy\n",
|
11 |
+
"show-code: False\n",
|
12 |
+
"params:\n",
|
13 |
+
" sent:\n",
|
14 |
+
" input: text\n",
|
15 |
+
" label: Enter the text to analyze\n",
|
16 |
+
" value: Apple is looking at buying U.K. startup for $1 billion\n",
|
17 |
+
" task:\n",
|
18 |
+
" input: select\n",
|
19 |
+
" label: Choose the tasks to do\n",
|
20 |
+
" choices: [Dependency Parser, Named Entity Recognition, Sentiment Analysis]\n",
|
21 |
+
" value: Named Entity Recognition\n",
|
22 |
+
" multi: True\n",
|
23 |
+
" compact:\n",
|
24 |
+
" input: checkbox\n",
|
25 |
+
" label: Compact(If you chose Dependency Parser, check the below boxes accordingly)\n",
|
26 |
+
" value: False\n",
|
27 |
+
" collapse_punctuation:\n",
|
28 |
+
" input: checkbox\n",
|
29 |
+
" label: Collapse Punctuation\n",
|
30 |
+
" value: False\n",
|
31 |
+
" collapse_phrases:\n",
|
32 |
+
" input: checkbox\n",
|
33 |
+
" label: Collapse Phrases\n",
|
34 |
+
" value: False\n",
|
35 |
+
" entities:\n",
|
36 |
+
" input: select\n",
|
37 |
+
" label: If you chose Named Entity Recognition task then select the named entities you want to see\n",
|
38 |
+
" choices: [PERSON, ORG, DATE, PRODUCT, LOC, GPE, LANGUAGE, FAC, NORP, MONEY]\n",
|
39 |
+
" multi: True\n",
|
40 |
+
"---"
|
41 |
+
]
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"cell_type": "markdown",
|
45 |
+
"id": "62ddeb5f",
|
46 |
+
"metadata": {},
|
47 |
+
"source": [
|
48 |
+
"# <center>NLP with SpaCy 💥</center>\n",
|
49 |
+
"### <center>A web application built with Mercury and SpaCy</center>"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "markdown",
|
54 |
+
"id": "a76b4050",
|
55 |
+
"metadata": {},
|
56 |
+
"source": [
|
57 |
+
"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",
|
58 |
+
"\n",
|
59 |
+
"##### From the official [Github](https://github.com/mljar/mercury) page\n",
|
60 |
+
"> Mercury can convert your jupyter notebook into \n",
|
61 |
+
"> - interactive web app\n",
|
62 |
+
"> - interactive slides\n",
|
63 |
+
"> - data dashboard\n",
|
64 |
+
"> - beautiful report\n",
|
65 |
+
"\n",
|
66 |
+
"All you need to do is start by installing it \n",
|
67 |
+
"**pip install mljar-mercury**\n",
|
68 |
+
"\n",
|
69 |
+
"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",
|
70 |
+
"\n",
|
71 |
+
"1. Enter the text you want to analyse in the text box that is present in the side bar. \n",
|
72 |
+
"2. Choose the task you want to perform. \n",
|
73 |
+
"3. Choose the required parameters for each task.\n",
|
74 |
+
"4. Click 'Run' \n",
|
75 |
+
"\n",
|
76 |
+
"Wait for the execution of the notebook. You will see your output below."
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": 4,
|
82 |
+
"id": "84845499",
|
83 |
+
"metadata": {},
|
84 |
+
"outputs": [],
|
85 |
+
"source": [
|
86 |
+
"sent = 'Notebook in watch mode. All changes to Notebook will be automatically visible in Mercury'\n",
|
87 |
+
"compact = False\n",
|
88 |
+
"collapse_punctuation = True\n",
|
89 |
+
"collapse_phrases = False\n",
|
90 |
+
"entities = 'PERSON'"
|
91 |
+
]
|
92 |
+
},
|
93 |
+
{
|
94 |
+
"cell_type": "code",
|
95 |
+
"execution_count": null,
|
96 |
+
"id": "7c506125",
|
97 |
+
"metadata": {
|
98 |
+
"scrolled": true
|
99 |
+
},
|
100 |
+
"outputs": [],
|
101 |
+
"source": [
|
102 |
+
"import spacy\n",
|
103 |
+
"from spacy import displacy\n",
|
104 |
+
"from spacytextblob.spacytextblob import SpacyTextBlob\n",
|
105 |
+
"from simple_colors import *"
|
106 |
+
]
|
107 |
+
},
|
108 |
+
{
|
109 |
+
"cell_type": "code",
|
110 |
+
"execution_count": 6,
|
111 |
+
"id": "16e95d2f",
|
112 |
+
"metadata": {},
|
113 |
+
"outputs": [],
|
114 |
+
"source": [
|
115 |
+
"def tasks(task):\n",
|
116 |
+
" if 'Dependency Parser' in task:\n",
|
117 |
+
" options = {\"compact\":compact, \"collapse_punct\":collapse_punctuation, \"collapse_phrases\":collapse_phrases, \"color\": \"black\", \"bg\":\"linear-gradient(90deg, #aa9cfc, #fc9ce7)\",\"distance\":100}\n",
|
118 |
+
" print(red('Dependency Parser', ['bold', 'underlined']))\n",
|
119 |
+
" displacy.render(doc, style=\"dep\", jupyter =True, options=options)\n",
|
120 |
+
" if 'Named Entity Recognition' in task:\n",
|
121 |
+
" try:\n",
|
122 |
+
" options = {\"ents\": entities }\n",
|
123 |
+
" print(red('Named Entities', ['bold', 'underlined']))\n",
|
124 |
+
" if len(doc.ents)==0:\n",
|
125 |
+
" print('Spacy has not detected any entities in the doc object.')\n",
|
126 |
+
" else:\n",
|
127 |
+
" displacy.render(doc, style=\"ent\", jupyter=True, options=options)\n",
|
128 |
+
" except:\n",
|
129 |
+
" print(red('Named Entities', ['bold', 'underlined']))\n",
|
130 |
+
" print('No entity is chosen. Please select atleast one entity from the list on the sidebar.')\n",
|
131 |
+
" if 'Sentiment Analysis' in task:\n",
|
132 |
+
" print(red('Sentiment Analysis', ['bold', 'underlined']))\n",
|
133 |
+
" if doc._.blob.polarity > 0:\n",
|
134 |
+
" print('Positive:'+ ' Polarity score is '+str(doc._.blob.polarity))\n",
|
135 |
+
" elif doc._.blob.polarity < 0:\n",
|
136 |
+
" print('Negative:'+ ' Polarity score is '+str(doc._.blob.polarity))\n",
|
137 |
+
" else:\n",
|
138 |
+
" print('Neutral:'+ ' Polarity score is '+str(doc._.blob.polarity))"
|
139 |
+
]
|
140 |
+
},
|
141 |
+
{
|
142 |
+
"cell_type": "code",
|
143 |
+
"execution_count": null,
|
144 |
+
"id": "d2840678",
|
145 |
+
"metadata": {},
|
146 |
+
"outputs": [],
|
147 |
+
"source": [
|
148 |
+
"nlp = spacy.load(\"en_core_web_sm\")\n",
|
149 |
+
"nlp.add_pipe('spacytextblob')\n",
|
150 |
+
"doc = nlp(sent)"
|
151 |
+
]
|
152 |
+
},
|
153 |
+
{
|
154 |
+
"cell_type": "code",
|
155 |
+
"execution_count": null,
|
156 |
+
"id": "62adc4ef",
|
157 |
+
"metadata": {
|
158 |
+
"scrolled": true
|
159 |
+
},
|
160 |
+
"outputs": [],
|
161 |
+
"source": [
|
162 |
+
"tasks(task)"
|
163 |
+
]
|
164 |
+
}
|
165 |
+
],
|
166 |
+
"metadata": {
|
167 |
+
"kernelspec": {
|
168 |
+
"display_name": "Python 3 (ipykernel)",
|
169 |
+
"language": "python",
|
170 |
+
"name": "python3"
|
171 |
+
},
|
172 |
+
"language_info": {
|
173 |
+
"codemirror_mode": {
|
174 |
+
"name": "ipython",
|
175 |
+
"version": 3
|
176 |
+
},
|
177 |
+
"file_extension": ".py",
|
178 |
+
"mimetype": "text/x-python",
|
179 |
+
"name": "python",
|
180 |
+
"nbconvert_exporter": "python",
|
181 |
+
"pygments_lexer": "ipython3",
|
182 |
+
"version": "3.9.12"
|
183 |
+
}
|
184 |
+
},
|
185 |
+
"nbformat": 4,
|
186 |
+
"nbformat_minor": 5
|
187 |
+
}
|