classla-api/app.py

128 lines
3.5 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
2022-01-25 14:37:59 +00:00
import torch
2022-01-14 08:42:34 +00:00
classla.download('sl')
classla.download('sl', type='standard_jos')
classla.download('sl', type='nonstandard')
2022-01-25 14:37:59 +00:00
classla.download('hr')
classla.download('hr', type='nonstandard')
classla.download('sr')
classla.download('sr', type='nonstandard')
classla.download('mk')
classla.download('bg')
2022-01-14 08:42:34 +00:00
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"
})
2022-01-25 14:37:59 +00:00
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')
2022-01-14 08:42:34 +00:00
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)
result = nlp(input_json['text']).to_conll()
del(nlp)
gc.collect()
2022-01-25 14:37:59 +00:00
torch.cuda.empty_cache()
2022-01-14 08:42:34 +00:00
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()
2022-01-25 14:37:59 +00:00
@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()
2022-01-14 08:42:34 +00:00
if __name__ == '__main__':
2022-01-14 08:43:50 +00:00
app.run(host="0.0.0.0")