@@ -0,0 +1,2207 @@
+from flask import Flask%0Afrom flask import request, abort, jsonify%0Aimport numpy as np%0Aimport gensim%0Aimport re%0Aimport string%0A%0Aapp = Flask(__name__)%0A%0Amodel = None%0AmaxSeqLength = 70%0AnumFeatures = 300%0A%0A%0Adef get_tokens(s):%0A    return re.sub('%5B%7B%7D%5D'.format(string.punctuation), '', s).lower().split()%0A%0Adef get_sequence_matrix(tokens, maxSeqLength, numFeatures, model):%0A    matrix = %5B%5D%0A    for i in range(maxSeqLength):%0A        if i %3C len(tokens) and tokens%5Bi%5D in model:%0A            matrix.append(model%5Btokens%5Bi%5D%5D)%0A        else:%0A            pass%0A    matrix = np.array(matrix)%0A    if matrix.shape%5B0%5D == 0:%0A        matrix = np.zeros((maxSeqLength, numFeatures))%0A    elif matrix.shape%5B0%5D %3C maxSeqLength:%0A        matrix = np.concatenate((matrix, np.zeros((maxSeqLength - matrix.shape%5B0%5D, numFeatures))))%0A    return matrix%0A%0A@app.route(%22/init%22, methods=%5B'POST'%5D)%0Adef init():%0A    global model%0A    try:%0A        model_path = request.form%5B'filepath'%5D%0A        is_binary = bool(request.form%5B'is_binary'%5D)%0A        model = gensim.models.KeyedVectors.load_word2vec_format(model_path, binary=is_binary)%0A    except PermissionError:%0A        abort(400, 'Could not load vectors - invalid path (%25s)' %25 model_path)%0A    except Exception as e:%0A        abort(500, 'Internal server error: %25s' %25 str(e))%0A    return 'OK'%0A%0A@app.route(%22/check/%3Cstring:word%3E%22, methods=%5B'GET'%5D)%0Adef check(word):%0A    if model is None:%0A        abort(500, 'Model is not initialized.')%0A    if word not in model:%0A        abort(500, 'Word was not found.')%0A    return jsonify(%7Bword: model%5Bword%5D.tolist()%7D)%0A%0A@app.route(%22/transform%22, methods=%5B'POST'%5D)%0Adef transform():%0A    if model is None:%0A        abort(500, 'Model is not initialized.')%0A    global maxSeqLength%0A    global numFeatures%0A    global model%0A    response = %7B%7D%0A    for item in request.data%5B'texts'%5D:%0A        try:%0A            id = item%5B'id'%5D%0A            vector_sequence = get_sequence_matrix(get_tokens(item%5B'text'%5D), maxSeqLength, numFeatures, model).tolist()%0A            response%5Bid%5D = vector_sequence%0A        except KeyError as e:%0A            abort(400, 'Wrong JSON format, key %25s' %25 e)%0A        except Exception as e:%0A            abort(500, 'Internal server error: %25s' %25 str(e))%0A        return jsonify(response)%0A

Add server for embedding transformation