nlu_utils.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from django.conf import settings
  2. from requests import post
  3. import json
  4. nlu_server_url = settings.NLU_SERVER_URI
  5. def model_inference(text):
  6. url = nlu_server_url + '/model/parse'
  7. payload = {'text': text}
  8. headers = {'content-type': 'application/json'}
  9. response = post(url, data=json.dumps(payload), headers=headers)
  10. if response.status_code == 200:
  11. return response.json()
  12. return response
  13. def analyze_inference(response):
  14. '''
  15. response has four property
  16. ['intent', 'entities', 'intent_ranking', 'text']
  17. we took all intents that has more than 10% of intent confident.
  18. all the intents that has bellow confidence has been omitted.
  19. :param response:
  20. :return: dictionary with key of intent and value of it's confident.
  21. '''
  22. res_intents = response.get('intent_ranking')
  23. intents = {}
  24. for intent in res_intents:
  25. key = intent.get('name')
  26. values = intent.get('confidence')
  27. if values > 0.1:
  28. intents[key] = int(values*100)
  29. return intents