You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.1 KiB
108 lines
3.1 KiB
from datetime import timedelta
|
|
|
|
import torch
|
|
from flask import Flask, request, jsonify, session
|
|
import classla
|
|
import gc
|
|
|
|
classla.download('sl')
|
|
classla.download('sl', type='standard_jos')
|
|
classla.download('sl', type='nonstandard')
|
|
|
|
print(f'USE GPU = {torch.cuda.is_available()}')
|
|
|
|
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.config['SESSION_TYPE'] = 'filesystem'
|
|
app.config['SECRET_KEY'] = "78df924389h138g01308ghj0d913"
|
|
# app.config['SESSION_PERMANENT']= False
|
|
|
|
|
|
# @app.before_request
|
|
# def make_session_permanent():
|
|
# session.permanent = True
|
|
# app.permanent_session_lifetime = timedelta(seconds=20)
|
|
# # app.permanent_session_lifetime = timedelta(minutes=1)
|
|
|
|
@app.route('/')
|
|
def hello_world():
|
|
return 'This is my first API call!'
|
|
|
|
# @app.route('/run', methods=['GET', "POST"])
|
|
# def run():
|
|
# if 'settings' not in session:
|
|
# return jsonify({'status': 'ERROR', 'details': 'No settings uploaded!'})
|
|
#
|
|
# input_json = request.get_json(force=True)
|
|
# doc = session['nlp'](input_json['text'])
|
|
#
|
|
# return doc.to_conll()
|
|
#
|
|
# # return 'NOT IN SESSION!\n'
|
|
|
|
# @app.route('/upload-settings', methods=['POST'])
|
|
# def upload_settings():
|
|
# input_json = request.get_json(force=True)
|
|
# try:
|
|
# settings = input_json['settings']
|
|
# session['nlp_id'] = classla.Pipeline(**settings)
|
|
# except:
|
|
# return jsonify({'status': 'ERROR', 'details': 'Incorrect settings!'})
|
|
#
|
|
# return jsonify({'status': 'OK'})
|
|
|
|
@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__':
|
|
app.run(host="0.0.0.0", debug=True)
|