Load only once the model of core and rasa in flask [custom code]

rasa-core

(Azizullah) #1

I using different agents, For each request in loads the model. I need a help how can I load the model only whats ? what is the disadvantages of my the server.run ? @Ghostvv @souvikg10 @MetcalfeTom

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils import EndpointConfig
from rasa_core.run import serve_application
from rasa_core import config
from rasa_core.tracker_store import RedisTrackerStore
from rasa_core.domain import TemplateDomain
import json
from flask import Flask, url_for
from flask import Flask, abort, request 
import logging
import rasa_core
from flask_cors import CORS, cross_origin
from flask import jsonify
app = Flask(__name__)


cors = CORS(app, resources={r"/English": {"origins": "http://localhost:port"}})
@app.route('/en', methods=['POST']) 
@cross_origin(origin='localhost',headers=['Content- Type','Authorization']) 
def English():
    interpreter = RasaNLUInterpreter('./models/en_nlu/Auto/Auto')
    domain = TemplateDomain.load("models/en/domain.yml")
    action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
    tracker_store = RedisTrackerStore(domain,host="localhost", port=6379, db= 13)

    agent = Agent.load('./models/en', interpreter=interpreter, action_endpoint=action_endpoint,tracker_store=tracker_store)

    if not request.json:
        abort(400)

    user=request.json['user']
    message=request.json['message']
    message= message.decode('utf-8')

    return jsonify(agent.handle_text(text_message=message, sender_id=user))

@app.route('/ar', methods=['POST']) 
def Arabic():
    interpreter = RasaNLUInterpreter('./models/ar_nlu/Auto/Auto')
    domain = TemplateDomain.load("models/ar/domain.yml")
    action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
    tracker_store = RedisTrackerStore(domain,host="localhost", port=6379, db= 13)

    agent = Agent.load('./models/ar', interpreter=interpreter, action_endpoint=action_endpoint,tracker_store=tracker_store)

    if not request.json:
        abort(400)

    user=request.json['user']
    message=request.json['message']
    message= message.decode('utf-8')

    return jsonify(agent.handle_text(text_message=message, sender_id=user))


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)