From f082471107ff6bf9364918d0fc7d69fdc985e578 Mon Sep 17 00:00:00 2001 From: Mathieu B Date: Mon, 26 Jul 2021 19:52:20 +0200 Subject: [PATCH] Sentence processing can now be used with the REST API --- ia/process.py | 12 ++++-------- main.py | 15 ++++++++++----- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ia/process.py b/ia/process.py index bd2de07..590f208 100644 --- a/ia/process.py +++ b/ia/process.py @@ -2,11 +2,12 @@ import os import torch from unidecode import unidecode - import get_path_file from ia.model import NeuralNet from ia.nltk_utils import bag_of_words, tokenize +print("Loading, might take a few seconds...") + path = os.path.dirname(get_path_file.__file__) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') @@ -53,11 +54,6 @@ def get_tag_for_sentence(input_sentence): probs = torch.softmax(output, dim=1) prob = probs[0][predicted.item()] if prob.item() > 0.75 and len(sentence) > 2: - return "MATCHING INTENT : " + tag + " (" + str(prob.item()) + ")" - # return intents.intents.get_matching_intent_for_tag(tag).get('tag') + return tag else: - return 'dont_understand' - - -if __name__ == '__main__': - print(get_tag_for_sentence("Hey, est il")) \ No newline at end of file + return 'dont_understand' \ No newline at end of file diff --git a/main.py b/main.py index 2f496dc..d0961e4 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import flask from flask import Flask, request, jsonify, Response +import ia.process from utils import config_utils, flask_utils app = Flask(__name__) @@ -10,14 +11,18 @@ app = Flask(__name__) def process_request(): data = flask_utils.get_data_in_request(request) - if 'sentence' not in data: - flask.abort(Response('You must provide a \'sentence\' parameter!')) + if 'sentence' not in data or not data['sentence']: + flask.abort(Response('You must provide a \'sentence\' parameter (not empty aswell)!')) - print(data) - return jsonify(data) + sentence = data['sentence'] + tag_for_request = ia.process.get_tag_for_sentence(sentence) + + print("SENTENCE : " + sentence + " /// TAG : " + tag_for_request) + + return jsonify(tag_for_request) if __name__ == '__main__': # start the flask server app.config['JSON_AS_ASCII'] = False - app.run(port=config_utils.get_in_config("PORT"), debug=False, host='0.0.0.0', threaded=True) + app.run(port=config_utils.get_in_config("PORT"), debug=False, host='0.0.0.0', threaded=True) \ No newline at end of file