Translate

Integrating chatbot with website-RASA + Flask

 

Integrating chatbot with website-RASA + Flask

pip install Flask
from flask import Flask, render_template, request, jsonify
import os,sys,requests, json
from random import randint
 
app = Flask(__name__,template_folder='templates')
@app.route('/')
def home():
return render_template('index.html')
  
@app.route('/parse',methods=['POST', 'GET'] )
def extract():
text=str(request.form.get('value1'))
payload = json.dumps({"sender": "Rasa","message": text})
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.request("POST", url="http://localhost:5005/webhooks/rest/webhook", headers=headers, data=payload)
response=response.json()
resp=[]
for i in range(len(response)):
try:
resp.append(response[i]['text'])
except:
continue
result=resp
return render_template('index.html', result=result,text=text)
if __name__ == "__main__": 
app.run(debug=True)
``` 
 
```
rasa run -m models --enable-api
#or  
rasa run -m models --enable-api cors "*" 
```
 
```
python app.py
 
#or 
python3 app.py
```

Solutions of “jinja2.exceptions.TemplateNotFound: index.html”

While runing FLASK_APP=app.py FLASK_DEBUG=true flask run in the command line, sometimes you may see a TemplateNotFound Exception e.g. “jinja2.exceptions.TemplateNotFound: index”. Here is a step-by-step guide that will walk you through how you may have this issue resolved.

The error message was due to the lack of a “index.html” file in the “Template” directory. To resolve the issue, simply create a folder name it “Templates”. Then move “index.html” into this newly created folder.

Besides, in file “app.py”, revisions need to be made so that it reads something like this:

app=Flask(__name__,template_folder='templates')

Save this. Flask runs again and there wouldn’t be any error this time!

Comments

Popular Posts

bot

Popular post