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
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"))

13
main.py
View File

@ -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,11 +11,15 @@ 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__':