Sentence processing can now be used with the REST API

This commit is contained in:
Mathieu B 2021-07-26 19:52:20 +02:00
parent 06b8be8026
commit f082471107
2 changed files with 14 additions and 13 deletions

View File

@ -2,11 +2,12 @@ import os
import torch import torch
from unidecode import unidecode from unidecode import unidecode
import get_path_file import get_path_file
from ia.model import NeuralNet from ia.model import NeuralNet
from ia.nltk_utils import bag_of_words, tokenize from ia.nltk_utils import bag_of_words, tokenize
print("Loading, might take a few seconds...")
path = os.path.dirname(get_path_file.__file__) path = os.path.dirname(get_path_file.__file__)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 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) probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()] prob = probs[0][predicted.item()]
if prob.item() > 0.75 and len(sentence) > 2: if prob.item() > 0.75 and len(sentence) > 2:
return "MATCHING INTENT : " + tag + " (" + str(prob.item()) + ")" return tag
# return intents.intents.get_matching_intent_for_tag(tag).get('tag')
else: else:
return 'dont_understand' return 'dont_understand'
if __name__ == '__main__':
print(get_tag_for_sentence("Hey, est il"))

15
main.py
View File

@ -1,6 +1,7 @@
import flask import flask
from flask import Flask, request, jsonify, Response from flask import Flask, request, jsonify, Response
import ia.process
from utils import config_utils, flask_utils from utils import config_utils, flask_utils
app = Flask(__name__) app = Flask(__name__)
@ -10,14 +11,18 @@ app = Flask(__name__)
def process_request(): def process_request():
data = flask_utils.get_data_in_request(request) data = flask_utils.get_data_in_request(request)
if 'sentence' not in data: if 'sentence' not in data or not data['sentence']:
flask.abort(Response('You must provide a \'sentence\' parameter!')) flask.abort(Response('You must provide a \'sentence\' parameter (not empty aswell)!'))
print(data) sentence = data['sentence']
return jsonify(data) 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__': if __name__ == '__main__':
# start the flask server # start the flask server
app.config['JSON_AS_ASCII'] = False 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)