Skip to content

Commit

Permalink
added create_app for app factory, setup unit test module, removed bli…
Browse files Browse the repository at this point in the history
…nker dependancy
  • Loading branch information
alfredfrancis committed Jul 21, 2021
1 parent 9949764 commit f042a81
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 68 deletions.
11 changes: 11 additions & 0 deletions .pytest_cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"frontend/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings_test.py": true,
"frontend/node_modules/node-gyp/gyp/pylib/gyp/common_test.py": true,
"frontend/node_modules/node-gyp/gyp/pylib/gyp/easy_xml_test.py": true,
"frontend/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs_test.py": true,
"frontend/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja_test.py": true,
"frontend/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode_test.py": true,
"frontend/node_modules/node-gyp/gyp/pylib/gyp/input_test.py": true,
"tests/test_nlu.py::TestCase::test_function": true,
"tests/test_nlu.py::test_function": true
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
python:
- "2.7"
- "3.6"
install:
- pip install -r requirements.txt
script:
Expand Down
76 changes: 37 additions & 39 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@
import os
from blinker import Namespace
from flask import Flask,send_from_directory
from flask_cors import CORS
from flask_mongoengine import MongoEngine

APP_ROOT = os.path.dirname(os.path.abspath(__file__ + "../../"))

app = Flask(__name__)
db = MongoEngine()

CORS(app)
def create_app(env = 'Development'):
app = Flask(__name__)
CORS(app)
# Configurations
try:
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)

# Configurations
try:
env = os.environ['APPLICATION_ENV']
except KeyError as e:
# logging.error('Unknown environment key, defaulting to Development')
env = 'Development'
from app.agents.controllers import bots
from app.nlu.controllers import nlu
from app.intents.controllers import intents
from app.train.controllers import train
from app.endpoint.controllers import endpoint
from app.entities.controllers import entities_blueprint

app.config.from_object('config.%s' % env)
app.config.update(
DEBUG=True,
TESTING=True,
TEMPLATES_AUTO_RELOAD=True)
app.register_blueprint(nlu)
app.register_blueprint(intents)
app.register_blueprint(train)
app.register_blueprint(endpoint)
app.register_blueprint(bots)
app.register_blueprint(entities_blueprint)

db = MongoEngine(app)
admin_panel_dist = os.path.join(APP_ROOT, 'frontend/dist/')

my_signals = Namespace()
@app.route('/<path:path>', methods=['GET'])
def static_proxy(path):
return send_from_directory(admin_panel_dist, path)

from app.agents.controllers import bots
from app.nlu.controllers import nlu
from app.intents.controllers import intents
from app.train.controllers import train
from app.endpoint.controllers import endpoint
from app.entities.controllers import entities_blueprint
@app.route('/')
def root():
print(admin_panel_dist)
return send_from_directory(admin_panel_dist, 'index.html')

@app.errorhandler(404)
def not_found(error):
return "Not found", 404

return app

app.register_blueprint(nlu)
app.register_blueprint(intents)
app.register_blueprint(train)
app.register_blueprint(endpoint)
app.register_blueprint(bots)
app.register_blueprint(entities_blueprint)

admin_panel_dist = os.path.join(APP_ROOT, 'frontend/dist/')

@app.route('/<path:path>', methods=['GET'])
def static_proxy(path):
return send_from_directory(admin_panel_dist, path)

@app.route('/')
def root():
print(admin_panel_dist)
return send_from_directory(admin_panel_dist, 'index.html')

@app.errorhandler(404)
def not_found(error):
return "Not found", 404
11 changes: 2 additions & 9 deletions app/endpoint/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from flask import Blueprint, request, abort
from jinja2 import Template

from app import app
from flask import current_app as app
from app.agents.models import Bot
from app.commons import build_response
from app.endpoint.utils import SilentUndefined
Expand All @@ -16,7 +16,6 @@
from app.nlu.classifiers.sklearn_intent_classifer import \
SklearnIntentClassifier
from app.nlu.entity_extractor import EntityExtractor
from app.nlu.tasks import model_updated_signal

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

Expand Down Expand Up @@ -201,7 +200,7 @@ def api():
return abort(400)


def update_model(app, message, **extra):
def update_model():
"""
Signal hook to be called after training is completed.
Reloads ml models and synonyms.
Expand All @@ -223,12 +222,6 @@ def update_model(app, message, **extra):
app.logger.info("Intent Model updated")


with app.app_context():
update_model(app, "Models updated")

model_updated_signal.connect(update_model, app)


def predict(sentence):
"""
Predict Intent using Intent classifier
Expand Down
2 changes: 1 addition & 1 deletion app/endpoint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
from jinja2 import Undefined

from app import app
from flask import current_app as app
from app.entities.models import Entity


Expand Down
8 changes: 3 additions & 5 deletions app/nlu/tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# -*- coding: utf-8 -*-

from app import app
from app import my_signals
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.nlu.classifiers.sklearn_intent_classifer import \
SklearnIntentClassifier
from app.nlu.entity_extractor import EntityExtractor

model_updated_signal = my_signals.signal('model-updated')

def train_models():
"""
Expand All @@ -28,8 +27,7 @@ def train_models():
for intent in intents:
train_all_ner(intent.intentId, intent.trainingData)

model_updated_signal.send(app, message="Training Completed.")

update_model()

def train_intent_classifier(intents):
"""
Expand Down
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Config(object):

class Development(Config):
DEBUG = True
TEMPLATES_AUTO_RELOAD=True

class Testing(Config):
DEBUG = True
TESTING = True

class Production(Config):
# MongoDB Database Details
Expand Down
4 changes: 3 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask_script import Manager
from app import app
from app import create_app

app = create_app()

manager = Manager(app)

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Flask==1.1.2
flask-mongoengine==0.9.5
Flask-Script==2.0.6
Flask-Cors==3.0.3
blinker==1.4

bs4==0.0.1
html2text==2017.10.4
Expand Down
4 changes: 2 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app import app

from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True, threaded=True)
Empty file added tests/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions tests/test_nlu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from app import create_app
import unittest

class TestCase(unittest.TestCase):
def setUp(self):
self.app = create_app('Testing')
self.app_context = self.app.app_context()
self.app_context.push()
self.client = self.app.test_client()

def tearDown(self):
self.app_context.pop()

def test_train_nlu(self):
rv = self.client.post('/nlu/build_models', json={}, follow_redirects=True)
assert rv.status_code == 200

if __name__ == '__main__':
unittest.main()
9 changes: 0 additions & 9 deletions tests/test_sample.py

This file was deleted.

0 comments on commit f042a81

Please sign in to comment.