|
2 | 2 |
|
3 | 3 | # spaCy REST services |
4 | 4 |
|
5 | | -This repository provides REST microservices for Explosion AI's [interactive demos](https://demos.explosion.ai) and visualisers. All requests and responses are JSON-encoded as `text/string`, so all requests require the header `Content-Type: text/string`. |
6 | | - |
7 | | -## [displaCy server](displacy) |
8 | | - |
9 | | -A simple [Falcon](https://falconframework.org/) app for exposing a spaCy dependency parser and spaCy named entity recognition model as a REST microservice, formatted for the [displaCy.js](https://github.com/explosion/displacy) and [displaCy ENT](https://github.com/explosion/displacy-ent) visualiser. For more info on the rendering on the front-end that consumes the data produced by this service, see [this blog post](https://explosion.ai/blog/displacy-js-nlp-visualizer). |
10 | | - |
11 | | -The service exposes two endpoints that accept POST requests, and two endpoints that accept GET requests to describe the available models and schemas. |
12 | | - |
13 | | ---- |
14 | | - |
15 | | -### `POST` `/dep/` |
16 | | - |
17 | | -Example request: |
18 | | - |
19 | | -```json |
20 | | -{ |
21 | | - "text": "They ate the pizza with anchovies", |
22 | | - "model":"en", |
23 | | - "collapse_punctuation": 0, |
24 | | - "collapse_phrases": 1 |
25 | | -} |
26 | | -``` |
27 | | - |
28 | | -| Name | Type | Description | |
29 | | -| --- | --- | --- | |
30 | | -| `text` | string | text to be parsed | |
31 | | -| `model` | string | identifier string for a model installed on the server | |
32 | | -| `collapse_punctuation` | boolean | Merge punctuation onto the preceding token? | |
33 | | -| `collapse_phrases` | boolean | Merge noun chunks and named entities into single tokens? | |
34 | | - |
35 | | - |
36 | | -Example request using the Python [Requests library](http://docs.python-requests.org/en/master/): |
37 | | - |
38 | | -```python |
39 | | -import json |
40 | | -import requests |
41 | | - |
42 | | -url = "http://localhost:8000/dep" |
43 | | -message_text = "They ate the pizza with anchovies" |
44 | | -headers = {'content-type': 'application/json'} |
45 | | -d = {'text': message_text, 'model': 'en'} |
46 | | - |
47 | | -response = requests.post(url, data=json.dumps(d), headers=headers) |
48 | | -r = response.json() |
49 | | -``` |
50 | | - |
51 | | -Example response: |
52 | | - |
53 | | -```json |
54 | | -{ |
55 | | - "arcs": [ |
56 | | - { "dir": "left", "start": 0, "end": 1, "label": "nsubj" }, |
57 | | - { "dir": "right", "start": 1, "end": 2, "label": "dobj" }, |
58 | | - { "dir": "right", "start": 1, "end": 3, "label": "prep" }, |
59 | | - { "dir": "right", "start": 3, "end": 4, "label": "pobj" }, |
60 | | - { "dir": "left", "start": 2, "end": 3, "label": "prep" } |
61 | | - ], |
62 | | - "words": [ |
63 | | - { "tag": "PRP", "text": "They" }, |
64 | | - { "tag": "VBD", "text": "ate" }, |
65 | | - { "tag": "NN", "text": "the pizza" }, |
66 | | - { "tag": "IN", "text": "with" }, |
67 | | - { "tag": "NNS", "text": "anchovies" } |
68 | | - ] |
69 | | -} |
70 | | -``` |
71 | | - |
72 | | -| Name | Type | Description | |
73 | | -| --- | --- | --- | |
74 | | -| `arcs` | array | data to generate the arrows | |
75 | | -| `dir` | string | direction of arrow (`"left"` or `"right"`) | |
76 | | -| `start` | integer | offset of word the arrow starts **on** | |
77 | | -| `end` | integer | offset of word the arrow ends **on** | |
78 | | -| `label` | string | dependency label | |
79 | | -| `words` | array | data to generate the words | |
80 | | -| `tag` | string | part-of-speech tag | |
81 | | -| `text` | string | token | |
82 | | - |
83 | | ---- |
84 | | - |
85 | | -Curl command: |
86 | | - |
87 | | -``` |
88 | | -curl -s localhost:8000/dep -d '{"text":"Pastafarians are smarter than people with Coca Cola bottles.", "model":"en"}' |
89 | | -``` |
90 | | - |
91 | | -```json |
92 | | -{ |
93 | | - "arcs": [ |
94 | | - { |
95 | | - "dir": "left", |
96 | | - "end": 1, |
97 | | - "label": "nsubj", |
98 | | - "start": 0 |
99 | | - }, |
100 | | - { |
101 | | - "dir": "right", |
102 | | - "end": 2, |
103 | | - "label": "acomp", |
104 | | - "start": 1 |
105 | | - }, |
106 | | - { |
107 | | - "dir": "right", |
108 | | - "end": 3, |
109 | | - "label": "prep", |
110 | | - "start": 2 |
111 | | - }, |
112 | | - { |
113 | | - "dir": "right", |
114 | | - "end": 4, |
115 | | - "label": "pobj", |
116 | | - "start": 3 |
117 | | - }, |
118 | | - { |
119 | | - "dir": "right", |
120 | | - "end": 5, |
121 | | - "label": "prep", |
122 | | - "start": 4 |
123 | | - }, |
124 | | - { |
125 | | - "dir": "right", |
126 | | - "end": 6, |
127 | | - "label": "pobj", |
128 | | - "start": 5 |
129 | | - } |
130 | | - ], |
131 | | - "words": [ |
132 | | - { |
133 | | - "tag": "NNPS", |
134 | | - "text": "Pastafarians" |
135 | | - }, |
136 | | - { |
137 | | - "tag": "VBP", |
138 | | - "text": "are" |
139 | | - }, |
140 | | - { |
141 | | - "tag": "JJR", |
142 | | - "text": "smarter" |
143 | | - }, |
144 | | - { |
145 | | - "tag": "IN", |
146 | | - "text": "than" |
147 | | - }, |
148 | | - { |
149 | | - "tag": "NNS", |
150 | | - "text": "people" |
151 | | - }, |
152 | | - { |
153 | | - "tag": "IN", |
154 | | - "text": "with" |
155 | | - }, |
156 | | - { |
157 | | - "tag": "NNS", |
158 | | - "text": "Coca Cola bottles." |
159 | | - } |
160 | | - ] |
161 | | -} |
162 | | -``` |
163 | | - |
164 | | - |
165 | | -### `POST` `/ent/` |
166 | | - |
167 | | -Example request: |
168 | | - |
169 | | -```json |
170 | | -{ |
171 | | - "text": "When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously.", |
172 | | - "model": "en" |
173 | | -} |
174 | | -``` |
175 | | - |
176 | | -| Name | Type | Description | |
177 | | -| --- | --- | --- | |
178 | | -| `text` | string | text to be parsed | |
179 | | -| `model` | string | identifier string for a model installed on the server | |
180 | | - |
181 | | -Example request using the Python [Requests library](http://docs.python-requests.org/en/master/): |
182 | | - |
183 | | -```python |
184 | | -import json |
185 | | -import requests |
186 | | - |
187 | | -url = "http://localhost:8000/ent" |
188 | | -message_text = "When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously." |
189 | | -headers = {'content-type': 'application/json'} |
190 | | -d = {'text': message_text, 'model': 'en'} |
191 | | - |
192 | | -response = requests.post(url, data=json.dumps(d), headers=headers) |
193 | | -r = response.json() |
194 | | -``` |
195 | | - |
196 | | -Example response: |
197 | | - |
198 | | -```json |
199 | | -[ |
200 | | - { "end": 20, "start": 5, "type": "PERSON" }, |
201 | | - { "end": 67, "start": 61, "type": "ORG" }, |
202 | | - { "end": 75, "start": 71, "type": "DATE" } |
203 | | -] |
204 | | -``` |
205 | | - |
206 | | -| Name | Type | Description | |
207 | | -| --- | --- | --- | |
208 | | -| `end` | integer | character offset the entity ends **after** | |
209 | | -| `start` | integer | character offset the entity starts **on** | |
210 | | -| `type` | string | entity type | |
211 | | - |
212 | | - |
213 | | - |
214 | | -``` |
215 | | -curl -s localhost:8000/ent -d '{"text":"Pastafarians are smarter than people with Coca Cola bottles.", "model":"en"}' |
216 | | -``` |
217 | | - |
218 | | -```json |
219 | | -[ |
220 | | - { |
221 | | - "end": 12, |
222 | | - "start": 0, |
223 | | - "type": "NORP" |
224 | | - }, |
225 | | - { |
226 | | - "end": 51, |
227 | | - "start": 42, |
228 | | - "type": "ORG" |
229 | | - } |
230 | | -] |
231 | | -``` |
232 | | - |
233 | | - |
234 | | ---- |
235 | | - |
236 | | -### `GET` `/models` |
237 | | - |
238 | | -List the names of models installed on the server. |
239 | | - |
240 | | -Example request: |
241 | | - |
242 | | -``` |
243 | | -GET /models |
244 | | -``` |
245 | | - |
246 | | -Example response: |
247 | | - |
248 | | -```json |
249 | | -["en", "de"] |
250 | | -``` |
251 | | - |
252 | | ---- |
253 | | - |
254 | | -### `GET` `/{model}/schema/` |
255 | | - |
256 | | -Example request: |
257 | | - |
258 | | -``` |
259 | | -GET /en/schema |
260 | | -``` |
261 | | - |
262 | | -| Name | Type | Description | |
263 | | -| --- | --- | --- | |
264 | | -| `model` | string | identifier string for a model installed on the server | |
265 | | - |
266 | | -Example response: |
267 | | - |
268 | | -```json |
269 | | -{ |
270 | | - "dep_types": ["ROOT", "nsubj"], |
271 | | - "ent_types": ["PERSON", "LOC", "ORG"], |
272 | | - "pos_types": ["NN", "VBZ", "SP"] |
273 | | -} |
274 | | -``` |
275 | | - |
276 | | ---- |
277 | | - |
278 | | -## [sense2vec server](sense2vec) |
279 | | - |
280 | | -A simple [Falcon](https://falconframework.org/) app for exposing a sense2vec model as a REST microservice, as used in the [sense2vec demo](https://github.com/explosion/sense2vec-demo) |
281 | | - |
282 | | -The service exposes a single endpoint over GET. |
283 | | - |
284 | | ---- |
285 | | - |
286 | | -### `GET` `/{word|POS}` |
287 | | - |
288 | | -Example query: |
289 | | - |
290 | | -``` |
291 | | -GET /natural_language_processing%7CNOUN |
292 | | -``` |
293 | | - |
294 | | -Example response: |
295 | | - |
296 | | -```json |
297 | | -[ |
298 | | - { |
299 | | - "score": 0.1, |
300 | | - "key": "computational_linguistics|NOUN", |
301 | | - "text": "computational linguistics", |
302 | | - "count": 20, |
303 | | - "head": "linguistics" |
304 | | - } |
305 | | -] |
306 | | -``` |
307 | | - |
308 | | -| Name | Type | Description | |
309 | | -| --- | --- | --- | |
310 | | -| `score` | float | similarity to query | |
311 | | -| `key` | string | identifier string | |
312 | | -| `text` | string | human-readable token | |
313 | | -| `count` | integer | absolute frequency in training corpus | |
314 | | -| `head` | string | head word in text | |
| 5 | +This repository includes REST microservices for various spaCy-related tasks. The |
| 6 | +services power our [interactive demos](https://explosion.ai/demos) and can be |
| 7 | +used as examples of exposing spaCy's capabilities as a microservice. All APIs |
| 8 | +are built with [`hug`](https://github.com/timothycrosley/hug) and |
| 9 | +**require Python 3**. The following services are available – for more details, |
| 10 | +see the API docs in the respective directories. |
| 11 | + |
| 12 | +| Service | Description | Example | |
| 13 | +| --- | --- | -- | |
| 14 | +| [`displacy`](displacy) | Serve a spaCy model and extract dependencies and named entities. | [🖼](https://explosion.ai/demos/displacy), [🖼](https://explosion.ai/demos/displacy-ent)| |
| 15 | +| [`sense2vec`](sense2vec) | Serve a [sense2vec](https://github.com/explosion/sense2vec) model with automatic sense detection. | [🖼](https://explosion.ai/demos/sense2vec) |
| 16 | +| [`matcher`](matcher) | Run a match pattern over a text and return the matches and tokens as JSON. | [🖼](https://explosion.ai/demos/matcher) | |
0 commit comments