classla-api/app.py
2022-01-25 15:37:59 +01:00

128 lines
3.5 KiB
Python

from flask import Flask, request
import classla
import gc
import torch
classla.download('sl')
classla.download('sl', type='standard_jos')
classla.download('sl', type='nonstandard')
classla.download('hr')
classla.download('hr', type='nonstandard')
classla.download('sr')
classla.download('sr', type='nonstandard')
classla.download('mk')
classla.download('bg')
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"
})
nlp_hr_standard_UD = classla.Pipeline('hr')
nlp_hr_nonstandard_UD = classla.Pipeline('hr', type='nonstandard')
nlp_sr_standard_UD = classla.Pipeline('sr')
nlp_sr_nonstandard_UD = classla.Pipeline('sr', type='nonstandard')
nlp_bg_standard_UD = classla.Pipeline('bg')
nlp_mk_standard_UD = classla.Pipeline('mk')
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'API is running...'
@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)
result = nlp(input_json['text']).to_conll()
del(nlp)
gc.collect()
torch.cuda.empty_cache()
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()
@app.route('/hr-standard-ud', methods=["POST"])
def hr_standard_ud():
input_json = request.get_json(force=True)
doc = nlp_hr_standard_UD(input_json['text'])
return doc.to_conll()
@app.route('/hr-nonstandard-ud', methods=["POST"])
def hr_nonstandard_ud():
input_json = request.get_json(force=True)
doc = nlp_hr_nonstandard_UD(input_json['text'])
return doc.to_conll()
@app.route('/sr-standard-ud', methods=["POST"])
def sr_standard_ud():
input_json = request.get_json(force=True)
doc = nlp_sr_standard_UD(input_json['text'])
return doc.to_conll()
@app.route('/sr-nonstandard-ud', methods=["POST"])
def sr_nonstandard_ud():
input_json = request.get_json(force=True)
doc = nlp_sr_nonstandard_UD(input_json['text'])
return doc.to_conll()
@app.route('/bg-standard-ud', methods=["POST"])
def bg_standard_ud():
input_json = request.get_json(force=True)
doc = nlp_bg_standard_UD(input_json['text'])
return doc.to_conll()
@app.route('/mk-standard-ud', methods=["POST"])
def mk_standard_ud():
input_json = request.get_json(force=True)
doc = nlp_mk_standard_UD(input_json['text'])
return doc.to_conll()
if __name__ == '__main__':
app.run(host="0.0.0.0")