-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (43 loc) · 1.69 KB
/
app.py
File metadata and controls
58 lines (43 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from flask import Flask, render_template, request, redirect, url_for
from agent.global_agent import *
import markdown
# first we need to create a child class out of the AIAgent class to enable proper functionality
class Agent(AIAgent):
def __init__(self):
super().__init__()
# we'll just have to overwrite the run function to run once and return a reply dialogue.
def run(self, user_input : str):
intent = self.getIntent(user_input)
if 'None' in intent.text:
return {'assistant':self.handleNoneIntent()}
elif 'Fallback' in intent.text:
return {'assistant': self.handleDunnoIntent()}
else:
# the output comes as a text repr. of a JSON/Python dictionary
# we need to convert it first
output = intent.text
return {'assistant':self.handleFetchRecipe(output)}
# building the Flask app
app = Flask(__name__)
app.config['debug'] = True
# creating the agent
agent = Agent()
# conversation as a list
conversation = []
@app.route('/')
def index():
return render_template('chatpage.html', messages = conversation)
@app.route('/consume_message', methods=['POST', 'GET'])
def chat():
if request.method == "POST":
user_input = request.form.get('user_message')
# add the user dialogue to the conversation history
conversation.append({'user':user_input})
# get the agent output
output = agent.run(user_input)
#modify the output to HTML syntax
output['assistant'] = markdown.markdown(output['assistant'])
conversation.append(output)
return redirect('/')
if __name__ == "__main__":
app.run(debug=True)