-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_hug.py
53 lines (41 loc) · 1.48 KB
/
app_hug.py
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
# coding: utf8
from __future__ import unicode_literals
import hug
from hug_middleware_cors import CORSMiddleware
import spacy
from pathlib import Path
output_dir=Path('/appl/training/model')
MODELS = {
'ca': spacy.load('ca')
}
def get_model_desc(nlp, model_name):
"""Get human-readable model name, language name and version."""
lang_cls = spacy.util.get_lang_class(nlp.lang)
lang_name = lang_cls.__name__
model_version = nlp.meta['version']
return '{} - {} (v{})'.format(lang_name, model_name, model_version)
@hug.get('/models')
def models():
return {name: get_model_desc(nlp, name) for name, nlp in MODELS.items()}
@hug.post('/dep')
def dep(text: str, model: str, collapse_punctuation: bool=False,
collapse_phrases: bool=False):
"""Get dependencies for displaCy visualizer."""
nlp = MODELS[model]
doc = nlp(text)
if collapse_phrases:
for np in list(doc.noun_chunks):
np.merge(tag=np.root.tag_, lemma=np.root.lemma_,
ent_type=np.root.ent_type_)
options = {'collapse_punct': collapse_punctuation}
return spacy.displacy.parse_deps(doc, options)
@hug.post('/ent')
def ent(text: str, model: str):
"""Get entities for displaCy ENT visualizer."""
nlp = MODELS[model]
doc = nlp(text)
return [{'start': ent.start_char, 'end': ent.end_char, 'label': ent.label_}
for ent in doc.ents]
if __name__ == '__main__':
app = hug.API(__name__)
app.http.add_middleware(CORSMiddleware(app))