Skip to content

Commit

Permalink
separate admin and bot blueprints
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredfrancis committed Jan 8, 2025
1 parent 72ba292 commit acf4973
Show file tree
Hide file tree
Showing 51 changed files with 71 additions and 746 deletions.
35 changes: 18 additions & 17 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from flask import Flask,send_from_directory
from flask import Flask, send_from_directory, Blueprint
from flask_cors import CORS
from flask_mongoengine import MongoEngine
from config import config
from app.dialogue_manager.dialogue_manager import DialogueManager
from app.bot.dialogue_manager.dialogue_manager import DialogueManager

admin_panel_dist = 'static/'

Expand All @@ -29,19 +29,24 @@ def create_app(env="Development"):
dialogue_manager.update_model(app.config["MODELS_DIR"])
app.dialogue_manager : DialogueManager = dialogue_manager

from app.bots.controllers import bots
from app.nlu.controllers import nlu
from app.intents.controllers import intents
from app.train.controllers import train
from app.chat.controllers import chat
from app.entities.controllers import entities_blueprint
from app.admin.bots.controllers import bots
from app.admin.intents.controllers import intents
from app.admin.train.controllers import train
from app.bot.chat.controllers import chat
from app.admin.entities.controllers import entities_blueprint

app.register_blueprint(nlu)
app.register_blueprint(intents)
app.register_blueprint(train)
# bot endpoints
# TODO: move to a isolated web server
app.register_blueprint(chat)
app.register_blueprint(bots)
app.register_blueprint(entities_blueprint)

# admin endpoints
admin_routes = Blueprint('admin', __name__, url_prefix='/admin/')
admin_routes.register_blueprint(intents)
admin_routes.register_blueprint(train)
admin_routes.register_blueprint(bots)
admin_routes.register_blueprint(entities_blueprint)
app.register_blueprint(admin_routes)


@app.route('/ready')
def ready():
Expand All @@ -51,10 +56,6 @@ def ready():
def static_proxy(path):
return send_from_directory(admin_panel_dist, path)

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

@app.errorhandler(404)
def not_found(error):
return "Not found", 404
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions app/bots/controllers.py → app/admin/bots/controllers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from flask import Blueprint, request, jsonify, Response, abort
from bson.json_util import dumps, loads
from app.bots.models import Bot
from app.intents.models import Intent
from app.entities.models import Entity
from app.commons.utils import update_document
from app.repository.bot import Bot
from app.repository.intents import Intent
from app.repository.entities import Entity
from app.repository.utils import update_document

bots = Blueprint('bots_blueprint', __name__,
url_prefix='/agents/<bot_name>')
url_prefix='/bots/<bot_name>')


@bots.route('/config', methods=['PUT'])
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from bson.json_util import dumps, loads
from bson.objectid import ObjectId
from flask import Blueprint, request, Response, jsonify, abort
from app.commons.utils import update_document
from app.entities.models import Entity
from app.repository.utils import update_document
from app.repository.entities import Entity

entities_blueprint = Blueprint('entities_blueprint', __name__,
url_prefix='/entities')
Expand Down
File renamed without changes.
24 changes: 3 additions & 21 deletions app/intents/controllers.py → app/admin/intents/controllers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import os
from bson.json_util import dumps
from bson.json_util import loads
from bson.objectid import ObjectId
from flask import Blueprint, request, Response, jsonify
from flask import abort
from flask import current_app as app
from app.commons.utils import update_document
from app.intents.models import ApiDetails
from app.intents.models import Intent
from app.intents.models import Parameter
from app.nlu.training import train_pipeline
from app.repository.utils import update_document
from app.repository.intents import ApiDetails, Intent, Parameter

intents = Blueprint('intents_blueprint', __name__,
url_prefix='/intents')


@intents.route('/', methods=['POST'])
def create_intent():
"""
Expand Down Expand Up @@ -104,15 +97,4 @@ def delete_intent(id):
:return:
"""
Intent.objects.get(id=ObjectId(id)).delete()

try:
train_pipeline(app)
except BaseException:
pass

# remove NER model for the deleted story
try:
os.remove("{}/{}.model".format(app.config["MODELS_DIR"], id))
except OSError:
pass
return jsonify({"result": True})
return jsonify({"status": "success"})
File renamed without changes.
16 changes: 14 additions & 2 deletions app/train/controllers.py → app/admin/train/controllers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from bson.objectid import ObjectId
from flask import Blueprint, request, jsonify
from app.intents.models import Intent
from app.repository.intents import Intent
from flask import Blueprint,request, current_app as app, jsonify
from app.bot.nlu.training import train_pipeline


train = Blueprint('train_blueprint', __name__,
url_prefix='/train')
Expand Down Expand Up @@ -28,3 +30,13 @@ def get_training_data(story_id):
"""
story = Intent.objects.get(id=ObjectId(story_id))
return jsonify(story.trainingData)


@train.route('/build_models', methods=['POST'])
def build_models():
"""
Build Intent classification and NER Models
:return:
"""
train_pipeline(app)
return jsonify({"result": True})
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions app/chat/controllers.py → app/bot/chat/controllers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from flask import Blueprint, request, abort, current_app as app
from app.dialogue_manager.models import ChatModel
from app.bot.dialogue_manager.models import ChatModel
from flask import jsonify

chat = Blueprint('chat', __name__, url_prefix='/api')
chat = Blueprint('bots', __name__, url_prefix='/bots/v1/')

@chat.route('/v1', methods=['POST'])
@chat.route('/chat', methods=['POST'])
def api():
"""
Endpoint to converse with the chatbot.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import logging
from typing import Dict, List, Optional, Tuple
from jinja2 import Template
from app.bots.models import Bot
from app.intents.models import Intent
from app.nlu.pipeline import NLUPipeline, IntentClassifier, EntityExtractor, SpacyFeaturizer
from app.dialogue_manager.utils import SilentUndefined, call_api, get_synonyms, split_sentence
from app.dialogue_manager.models import ChatModel, IntentModel, ParameterModel
from app.repository.bot import Bot
from app.repository.intents import Intent
from app.bot.nlu.pipeline import NLUPipeline, IntentClassifier, EntityExtractor, SpacyFeaturizer
from app.bot.dialogue_manager.utils import SilentUndefined, call_api, get_synonyms, split_sentence
from app.bot.dialogue_manager.models import ChatModel, IntentModel, ParameterModel

logger = logging.getLogger('dialogue_manager')

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from jinja2 import Undefined

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


def split_sentence(sentence):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
4 changes: 2 additions & 2 deletions app/nlu/pipeline.py → app/bot/nlu/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class IntentClassifier(NLUComponent):
"""Intent classification wrapper component."""

def __init__(self):
from app.nlu.classifiers.sklearn_intent_classifer import SklearnIntentClassifier
from app.bot.nlu.classifiers.sklearn_intent_classifer import SklearnIntentClassifier
self.classifier = SklearnIntentClassifier()

def train(self, training_data: List[Dict[str, Any]], model_path: str) -> None:
Expand All @@ -109,7 +109,7 @@ class EntityExtractor(NLUComponent):
"""Entity extraction wrapper component."""

def __init__(self, synonyms: Optional[Dict[str, str]] = None):
from app.nlu.entity_extractors.crf_entity_extractor import CRFEntityExtractor
from app.bot.nlu.entity_extractors.crf_entity_extractor import CRFEntityExtractor
self.extractor = CRFEntityExtractor(synonyms or {})

def train(self, training_data: List[Dict[str, Any]], model_path: str) -> None:
Expand Down
6 changes: 3 additions & 3 deletions app/nlu/training.py → app/bot/nlu/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os

from app import DialogueManager
from app.intents.models import Intent
from app.nlu.pipeline import NLUPipeline, IntentClassifier, EntityExtractor, SpacyFeaturizer
from app.dialogue_manager.utils import get_synonyms
from app.repository.intents import Intent
from app.bot.nlu.pipeline import NLUPipeline, IntentClassifier, EntityExtractor, SpacyFeaturizer
from app.bot.dialogue_manager.utils import get_synonyms

def train_pipeline(app):
"""
Expand Down
12 changes: 0 additions & 12 deletions app/commons/error_codes.py

This file was deleted.

1 change: 0 additions & 1 deletion app/nlu/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions app/nlu/controllers.py

This file was deleted.

Empty file added app/repository/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion app/static/0.2edb1edba482be9033ec.chunk.js

This file was deleted.

Loading

0 comments on commit acf4973

Please sign in to comment.