from flask import Flask, request, jsonify
import time
import kgextraction as askbert
#from nltk import sent_tokenize
app = Flask(__name__)
askbert_args = {'input_text' : '', 'length':10, 'batch_size': 1, 'temperature':1, 'model_name':'117M',
            'seed':0, 'nsamples':10, 'cutoffs':"6 7 5", 'write_sfdp':False, 'random':False}
#world = askbert.World(askbert_args)
#import timeit
import kgextraction

@app.route('/', methods=['POST'])
def result():
    count = 1
    if request.method == 'POST':
        data = request.form
        #genclock = timeit.default_timer()
        #TODO be blocking and gather up all requests then batch?
        #if count % 16 != 0:
        #    states.append(data['states'])
        #else:
        entities = kgextraction.generate(data['state'], float(data['threshold']), bool(data['attribute']))
        #print('genclock', timeit.default_timer() - genclock)
        ov = {'entities': entities}
        return jsonify(ov), 201

if __name__ == '__main__':
    app.run(host='0.0.0.0')#debug=False, threaded=False, processes=8, port=6000)
    #app.run(debug = False, threaded=True, port=sys.argv[1])