Example #1
0
def update_app(id):
    app_dir = common.get_app_dir(id)
    if not os.path.exists(app_dir):
        return response_404(id)
    json_data = common.load_json(request.data, app_dir)
    nlp_c = train_textcat.main(json_data,
                               output_dir=common.get_category_dir(app_dir))
    nlp_e = train_new_entity_type.main(
        json_data, output_dir=common.get_entity_dir(app_dir))
    id_models[id] = (nlp_c, nlp_e)
    return id
def update_app(id, data, files, is_create, is_force):
    app_dir = common.get_app_dir(id)
    if not os.path.exists(app_dir):
        if is_create:
            pathlib.Path(app_dir).mkdir(parents=True, exist_ok=True)
        else:
            return response_404(id)
    json_data = common.load_json(data, files, app_dir)
    if not is_force and common.compare_json(json_data, app_dir):
        return id
    common.remove_json(app_dir)
    nlp_c = train_textcat.main(json_data,
                               output_dir=common.get_category_dir(app_dir))
    nlp_e = train_new_entity_type.main(
        json_data, output_dir=common.get_entity_dir(app_dir))
    id_models[id] = (nlp_c, nlp_e)
    common.save_json(json_data, app_dir)
    return id
Example #3
0
def query_app(id, query):
    if not id in id_models:
        app_dir = common.get_app_dir(id)
        if not os.path.exists(app_dir):
            return response_404(id)
        c_dir = common.get_category_dir(app_dir)
        e_dir = common.get_entity_dir(app_dir)
        try:
            nlp_c = spacy.load(c_dir)
            nlp_e = spacy.load(e_dir)
            id_models[id] = (nlp_c, nlp_e)
        except:
            return response_403(id)

    nlp_c, nlp_e = id_models[id]
    doc_category = nlp_c(query)
    doc_entity = nlp_e(query)
    return {
        "cats": doc_category.cats,
        "ents": [(ent.label_, ent.text) for ent in doc_entity.ents],
    }