How to connect Rasa with Flask

509 Views Asked by At

First I have enabled the API by using

rasa run -m models --enable-api --cors "*" --debug

then I tested that using postman; it's showing the intent ranking:

Screen shot of Postman

In the next step want to run web, so I have the written a Flask app.

from flask import Flask, render_template, url_for, request, jsonify
import requests

app = Flask(__name__)

#@app.route('/')
#def hello_world():
    #return render_template('home.html')

@app.route('/chat',methods=["POST"])
def chat():

        user_message = request.form['text']
        response = requests.post("http://localhost:5005/model/parse", params ={"message": user_message} )
        return jsonify({"status":"success","response":response_text})

if __name__ == "__main__":
    app.run(debug = True, port = 5000)

but I'm unable to fetch the intent ranking over there. This the output while I'm using flask app:

Flask screenshot

Can someone help me sort this out?

1

There are 1 best solutions below

1
On

Your flask endpoint is expecting input from the form. You are sending a POST request with JSON in your second Postman example.

You have two options (do only one of them, doing both will get you the same error):

A) change your postman request, so it uses form-data instead of JSON. This has worked for me:Postman screenshot

B) Rewrite processing of the request in your code. You are sending JSON in your POST request, so you should process JSON, see How to get POSTed JSON in Flask?

from flask import Flask, render_template, url_for, request, jsonify
import requests

app = Flask(__name__)

@app.route('/chat',methods=["POST"])
def chat():
    user_message = request.get_json()['text']
    response = requests.post("http://localhost:5005/model/parse", params={"message": user_message} )
    return jsonify({"status": "success", "response": response.text})


if __name__ == "__main__":
    app.run(debug = True, port = 5000)