Parcourir la source

Change ner model with a url of ner server

Mohidul Islam il y a 4 ans
Parent
commit
86fd6c77db

+ 8 - 4
name_extractor/utils.py

@@ -1,4 +1,6 @@
 import re
+import requests
+import json
 from decimal import Decimal
 from django.conf import settings
 from .models import Staff
@@ -7,13 +9,15 @@ from review.models import Review
 from facebook_app.models import FacebookReview, FacebookPage
 from yelp.models import YelpReview, YelpLocation
 
-ner_model = getattr(settings, 'SPACY_NER_MODEL')
+ner_model_url = getattr(settings, 'NER_SERVER_URI')
 
 
 def get_all_names(text):
-    doc = ner_model(text)
-    names = {ent.text for ent in doc.ents if ent.label_ in ['PERSON', 'ORG', 'PRODUCT']}
-    return names
+    url = ner_model_url + '/names/all'
+    payload = {'text': text}
+    headers = {'content-type': 'application/json'}
+    res = requests.post(url, data=json.dumps(payload), headers=headers).json()
+    return res.get('names')
 
 
 def all_staffs(location_id):

+ 9 - 6
nlu_job/nlu_utils.py

@@ -1,5 +1,6 @@
 import re
 import operator
+import requests
 from django.conf import settings
 from requests import post
 import json
@@ -8,7 +9,7 @@ from review.models import Review
 from facebook_app.models import FacebookReview
 
 nlu_server_url = getattr(settings, 'NLU_SERVER_URI')
-ner_model = getattr(settings, 'SPACY_NER_MODEL')
+ner_model_url = getattr(settings, 'NER_SERVER_URI')
 
 
 def filter_with_last_ten_reviews(location_id, replies, platform='google'):
@@ -70,11 +71,13 @@ def is_a_name(name):
     :return -> a boolean True/False:
     '''
 
-    doc = ner_model(name)
-    for ent in doc.ents:
-        if ent.label_ in ['PERSON']:
-            return True
-    return False
+    url = ner_model_url + '/name'
+    payload = {
+        'name': name
+    }
+    headers = {'content-type': 'application/json'}
+    res = requests.post(url, data=json.dumps(payload), headers=headers).json()
+    return res.get('name')
 
 
 # This function will not use anymore

+ 1 - 7
review_automation/settings/dev.py

@@ -1,11 +1,4 @@
-import spacy
 from .base import *
-try:
-    SPACY_NER_MODEL = 'model-spacy'
-    # SPACY_NER_MODEL = spacy.load('en_core_web_sm')
-    print('\033[92mSpacy model small size has loaded!\033[0m')
-except OSError:
-    print('\033[93mNo spacy model has found. Please install a small sized spacy model.\033[0m')
 
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = True
@@ -31,6 +24,7 @@ DATABASES = {
 
 HOST_URI = "http://127.0.0.1:8000"
 NLU_SERVER_URI = 'http://localhost:1996'
+NER_SERVER_URI = 'http://localhost:2020'
 # selenium-firefox instance
 
 BROWSER = 'Bangladesh'

+ 2 - 7
review_automation/settings/prod.py

@@ -1,12 +1,5 @@
-import spacy
 from .base import *
 
-try:
-    SPACY_NER_MODEL = spacy.load('en_core_web_lg')
-    print('\033[93mSpacy model large size has loaded!\033[0m')
-except OSError:
-    print('\033[93mNo spacy model has found. Please install a large sized spacy model.\033[0m')
-
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = False
 
@@ -48,5 +41,7 @@ AUTH_PASSWORD_VALIDATORS = [
 
 HOST_URI = "http://10.0.0.36:5005"
 NLU_SERVER_URI = 'http://localhost:1996'
+NER_SERVER_URI = 'http://localhost:2020'
+
 # selenium-firefox instance
 BROWSER = get_firefox_browser()