model_pred_report.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import json
  2. import requests
  3. import csv
  4. from difflib import SequenceMatcher
  5. from django.utils import timezone
  6. from .models import Review, CustomReply
  7. # constants
  8. nlu_server_url = 'http://localhost:5005'
  9. replies = CustomReply.objects.all()
  10. def model_inference(text):
  11. url = nlu_server_url + '/model/parse'
  12. payload = {'text': text}
  13. headers = {'content-type': 'application/json'}
  14. response = requests.post(url, data=json.dumps(payload), headers=headers)
  15. if response.status_code == 200:
  16. res = response.json()
  17. intents_rankings = res.get('intent_ranking')
  18. intents = []
  19. for intent in intents_rankings:
  20. if intent.get('confidence') > 0.2:
  21. intents.append(intent['name'])
  22. return intents
  23. def get_review_actual_intent(review):
  24. actual_reply = review.reply.replied_text
  25. for c_r in replies:
  26. replied_text = c_r.reply
  27. similarity = SequenceMatcher(None, actual_reply, replied_text).ratio()
  28. if similarity > 0.7:
  29. return c_r.reply_category
  30. return None