Skip to content

Commit

Permalink
Add CLI options to specify models, host and port
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed Oct 27, 2018
1 parent 76a2fd3 commit 80b66b3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,27 @@ environment.
pip install -r requirements.txt
```

If you like, you can install more [models](https://spacy.io/models) and add them
to the list of models in [`api/server.py`](api/server.py). You can then run the
REST API. By default, this will serve the API via `localhost:8080`:
You can then run the REST API. By default, this will serve the API via
`0.0.0.0:8080`:

```bash
python api/server.py
```

If you like, you can install more [models](https://spacy.io/models) and specify
a comma-separated list of models to load as the first argument when you run
the server. All models need to be installed in the same environment.

```bash
python api/server.py en_core_web_sm,de_core_news_sm
```

| Argument | Type | Description | Default |
| --- | --- | --- | --- |
| `models` | positional (str) | Comma-separated list of models to load and make available. | `en_core_web_sm` |
| `--host`, `-ho` | option (str) | Host to serve the API. | `0.0.0.0` |
| `--port`, `-p` | option (int) | Port to server the API. | `8080` |

## 🎛 API

### `spacy.load`
Expand Down
31 changes: 24 additions & 7 deletions api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@

import hug
from hug_middleware_cors import CORSMiddleware
import waitress
import spacy
import plac


MODELS = {
'en_core_web_sm': spacy.load('en_core_web_sm')
}
MODELS = {}


@plac.annotations(
models=("Comma-separated list of spaCy models", "positional", None, str),
host=("Host to serve API", "option", "ho", str),
port=("Port to serve API", "option", "p", int)
)
def main(models=None, host='0.0.0.0', port=8080):
if not models:
models = ['en_core_web_sm']
else:
models = [m.strip() for m in models.split(',')]
for model in models:
print("Loading model '{}'...".format(model))
MODELS[model] = spacy.load(model)
# Serving Hug API
app = hug.API(__name__)
app.http.add_middleware(CORSMiddleware(app))
waitress.serve(__hug_wsgi__, port=port)



def doc2json(doc: spacy.tokens.Doc, model: str):
Expand Down Expand Up @@ -98,7 +118,4 @@ def similarity(model: str, text1: str, text2: str):


if __name__ == '__main__':
import waitress
app = hug.API(__name__)
app.http.add_middleware(CORSMiddleware(app))
waitress.serve(__hug_wsgi__, port=8080)
plac.call(main)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ hug>=2.4.0,<3.0.0
hug-middleware-cors>=1.0.0,<2.0.0
spacy>=2.0.10,<2.1.0
waitress>=1.0.2,<2.0.0
plac>=0.9.6,<1.0.0

https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.0.0/en_core_web_sm-2.0.0.tar.gz#egg=en_core_web_sm==2.0.0

0 comments on commit 80b66b3

Please sign in to comment.