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