Search code examples
pythonflaskportrasa-nlurasa-core

Make a server listen to a specific port or Flask app running on another?


Versiones

rasa-core : 0.9.0a3
rasa-nlu : 0.12.3

Problema

I have a web-app chat and a Rasa server that is a chatbot and I want the chatbot to handle the message received from the first one.

At the end of this tutorial one can read that the path was to launch the following command to start the RASA-NLU server by executing the "run_server.bat" script. Load the custom trained model and start listening to port 5000:

$ python -m rasa_nlu.server --path projects

I tried the following but the terminal tells me that the port is already in use

(myFlaskAppenv) mike@mike-thinks:~/Programing/Rasa/myflaskapp$ python -m rasa_nlu.server --path ~/Programing/Rasa/myflaskapp
/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
2018-06-04 17:01:20+0100 [-] Log opened.
2018-06-04 17:01:20+0100 [-] Unhandled error in Deferred:
2018-06-04 17:01:20+0100 [-] Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/rasa_nlu/server.py", line 402, in <module>
        rasa.app.run('0.0.0.0', cmdline_args.port)
      File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/klein/app.py", line 392, in run
        endpoint.listen(Site(self.resource()))
      File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/twisted/internet/endpoints.py", line 488, in listen
        interface=self._interface)
    --- <exception caught here> ---
      File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/twisted/internet/defer.py", line 121, in execute
        result = callable(*args, **kw)
      File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/twisted/internet/posixbase.py", line 495, in listenTCP
        p.startListening()
      File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/twisted/internet/tcp.py", line 980, in startListening
        raise CannotListenError(self.interface, self.port, le)
    twisted.internet.error.CannotListenError: Couldn't listen on 0.0.0.0:5000: [Errno 98] Address already in use.

Indeed :

(myFlaskAppenv) mike@mike-thinks:~/Programing/Rasa/myflaskapp$ python app.py 
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 213-078-895
 * Detected change in '/home/mike/Programing/Rasa/myflaskapp/app.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 213-078-895

So, for the moment when I try to talk to the chatbot it gives me that in the terminal lou I put app.py that contains all the routes:

127.0.0.1 - - [04/Jun/2018 16:42:46] "GET /parse?q=hi HTTP/1.1" 404 -
HOUSTON ! WE GOT AN EXCETPITON !
Expecting value: line 1 column 1 (char 0)
127.0.0.1 - - [04/Jun/2018 16:42:46] "POST /chat HTTP/1.1" 200 -

Here is the part of app.py that refers to the handling of chatbot messages, tell me if there is more need, but I think it is not the most important:

@app.route('/chat',methods=["POST"])
def chat():
    try:
        user_message = request.form["text"]
        response = requests.get("http://localhost:5000/parse",params={"q":user_message})
        response = response.json()
        entities = response.get("entities")
        topresponse = response["topScoringIntent"]
        intent = topresponse.get("intent")
        print("Intent {}, Entities {}".format(intent,entities))
        if intent == "gst-info":
            response_text = gst_info(entities)# "Sorry will get answer soon" #get_event(entities["day"],entities["time"],entities["place"])
        elif intent == "gst-query":
            response_text = gst_query(entities)
        else:
            response_text = get_random_response(intent)
        return jsonify({"status":"success","response":response_text})
    except Exception as e:
        print("HOUSTON ! WE GOT AN EXCETPITON !")
        print(e)
        return jsonify({"status":"success","response":"Sorry I am not trained to do that yet..."})

So, what command should I make for the chatbot server to start listening and discussing with port 5000?


[image of the chatbot]3


Solution

  • Just choose the port you want your app to run :

    if __name__ == '__main__':
        app.run(port=your_port,debug=True)