classla-api/app.py

72 lines
2.0 KiB
Python
Raw Normal View History

2022-01-14 08:43:50 +00:00
from flask import Flask, request
2022-01-14 08:42:34 +00:00
import classla
import gc
classla.download('sl')
classla.download('sl', type='standard_jos')
classla.download('sl', type='nonstandard')
nlp_standard_UD = classla.Pipeline('sl', pos_use_lexicon=True)
nlp_standard_JOS = classla.Pipeline('sl', pos_use_lexicon=True, type='standard_jos')
nlp_nonstandard_UD = classla.Pipeline('sl', type='nonstandard')
nlp_nonstandard_JOS = classla.Pipeline('sl', processors={
"tokenize": "nonstandard",
"lemma": "nonstandard",
"pos": "nonstandard",
"depparse": "standard_jos",
"ner": "nonstandard"
})
app = Flask(__name__)
@app.route('/')
def hello_world():
2022-01-14 08:43:50 +00:00
return 'API is running...'
2022-01-14 08:42:34 +00:00
@app.route('/custom-settings', methods=["POST"])
def custom_settings():
input_json = request.get_json(force=True)
if 'settings' in input_json:
settings = input_json['settings']
nlp = classla.Pipeline(**settings)
# classla.Pipeline('sl', processors='tokenize,pos,lemma', pos_use_lexicon=True)
result = nlp(input_json['text']).to_conll()
del(nlp)
gc.collect()
else:
return f'ERROR `settings` were not given!'
return result
@app.route('/standard-ud', methods=["POST"])
def standard_ud():
input_json = request.get_json(force=True)
doc = nlp_standard_UD(input_json['text'])
return doc.to_conll()
@app.route('/standard-jos', methods=["POST"])
def standard_jos():
input_json = request.get_json(force=True)
doc = nlp_standard_JOS(input_json['text'])
return doc.to_conll()
@app.route('/nonstandard-ud', methods=["POST"])
def nonstandard_ud():
input_json = request.get_json(force=True)
doc = nlp_nonstandard_UD(input_json['text'])
return doc.to_conll()
@app.route('/nonstandard-jos', methods=["POST"])
def nonstandard_jos():
input_json = request.get_json(force=True)
doc = nlp_nonstandard_JOS(input_json['text'])
return doc.to_conll()
if __name__ == '__main__':
2022-01-14 08:43:50 +00:00
app.run(host="0.0.0.0")