Skip to content

Commit

Permalink
made spacy model configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredfrancis committed Jul 26, 2021
1 parent 49bc147 commit b5470ac
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 32 deletions.
8 changes: 8 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

db = MongoEngine()

spacy_tokenizer = None

def create_app(env = 'Development'):
app = Flask(__name__)
CORS(app)
Expand All @@ -15,9 +17,14 @@ def create_app(env = 'Development'):
env = os.environ['APPLICATION_ENV']
except KeyError as e:
app.logger.info('Unknown environment key, defaulting to Development')

app.config.from_object('config.%s' % env)
db.init_app(app)

import spacy
global spacy_tokenizer
spacy_tokenizer = spacy.load(app.config["SPACY_LANG_MODEL"])

from app.agents.controllers import bots
from app.nlu.controllers import nlu
from app.intents.controllers import intents
Expand Down Expand Up @@ -50,6 +57,7 @@ def not_found(error):
from app.endpoint.controllers import update_model
with app.app_context():
update_model()

return app


Expand Down
70 changes: 48 additions & 22 deletions app/endpoint/controllers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-
import copy
import json

from flask import Blueprint, request, abort
from jinja2 import Template

from flask import current_app as app
from app.agents.models import Bot
from app.commons import build_response
Expand All @@ -16,36 +14,64 @@
from app.nlu.classifiers.sklearn_intent_classifer import \
SklearnIntentClassifier
from app.nlu.entity_extractor import EntityExtractor

endpoint = Blueprint('api', __name__, url_prefix='/api')

sentence_classifier = SklearnIntentClassifier()
synonyms = None
entity_extraction = None


# Request Handler
@endpoint.route('/v1', methods=['POST'])
def api():
"""
Endpoint to converse with chatbot.
Endpoint to Converse with the Chatbot.
Chat context is maintained by exchanging the payload between client and bot.
sample input/output payload =>
{
"currentNode": "",
"complete": false,
"parameters": [],
"extractedParameters": {},
"missingParameters": [],
"intent": {
},
"context": {},
"input": "hello",
"speechResponse": [
]
}
---
definitions:
currentNode:
type: string
complete:
type: boolean
context:
type: object
properties:
parameters:
type: array
items:
type: object
properties:
name:
type: string
type:
type: string
required:
type: boolean
extractedParameters:
type: object
properties:
country:
type: string
speechResponse:
type: array
items:
type: string
intent:
type: object
properties:
object_id:
type: string
confidence:
type: number
id:
type: string
input:
type: string
missingParameters:
type: array
items:
owner:
type: string
date:
type: string
:param json:
:return json:
Expand Down
2 changes: 0 additions & 2 deletions app/nlu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
import spacy
spacy_tokenizer = spacy.load("en")
7 changes: 2 additions & 5 deletions app/nlu/classifiers/sklearn_intent_classifer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import string
import os
import cloudpickle
import spacy
import numpy as np

spacynlp = spacy.load('en')
from app import spacy_tokenizer

class SklearnIntentClassifier:

Expand All @@ -17,7 +14,7 @@ def get_spacy_embedding(self, sentence):
:param sentence:
:return list of clean tokens:
"""
spacy_obj = spacynlp(sentence)
spacy_obj = spacy_tokenizer(sentence)
return np.array(spacy_obj.vector)

def train(self, X, y, outpath=None, verbose=True):
Expand Down
3 changes: 1 addition & 2 deletions app/nlu/entity_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import pycrfsuite
from flask import current_app as app

from app.nlu import spacy_tokenizer
from app import spacy_tokenizer


class EntityExtractor:
Expand Down
2 changes: 1 addition & 1 deletion app/nlu/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask import current_app as app
from app.endpoint.controllers import update_model
from app.intents.models import Intent
from app.nlu import spacy_tokenizer
from app import spacy_tokenizer
from app.nlu.classifiers.sklearn_intent_classifer import \
SklearnIntentClassifier
from app.nlu.entity_extractor import EntityExtractor
Expand Down

0 comments on commit b5470ac

Please sign in to comment.