-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·125 lines (90 loc) · 2.81 KB
/
app.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# imports
import os
from flask import (Flask, request, render_template, flash, jsonify)
from flask_sqlalchemy import SQLAlchemy
from bot import (Bot, INTENT_JSON)
import json
# get the folder where this file runs
basedir = os.path.abspath(os.path.dirname(__file__))
# configuration
DATABASE = 'users.db'
DEBUG = True
SECRET_KEY = 'precious'
USERNAME = 'admin'
PASSWORD = 'admin'
# define the full path for the database
DATABASE_PATH = os.path.join(basedir, DATABASE)
# database config
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + DATABASE_PATH
SQLALCHEMY_TRACK_MODIFICATIONS = False
def load_intents():
with open(INTENT_JSON) as json_data:
return json.load(json_data)
# Create app
app = Flask(__name__)
app.config.from_object(__name__)
db = SQLAlchemy(app)
import models
def handle_save(username, emoji):
if emoji is None:
return
utf8emoji = emoji.encode(encoding='UTF-8')
if utf8emoji is None:
return
new_entry = models.User(username, utf8emoji)
user_data = db.session.query(models.User).filter_by(username=username).first()
if not user_data:
db.session.add(new_entry)
else:
user_data.emoji = utf8emoji
db.session.commit()
def handle_return_saved(username):
user_data = db.session.query(models.User).filter_by(username=username).first()
emoji = user_data.emoji
return emoji
@app.route('/', methods=['GET', 'POST'])
def index():
intents = load_intents()
bot = Bot(intents, basedir)
bot.create_model()
bot.load_model()
if request.method == 'POST':
username = request.form['username']
query = request.form['query']
flash(query)
result = None
tag, response, emoji = bot.response(query)
if tag == 'save':
handle_save(username, emoji)
elif tag == 'return_saved':
emoji = handle_return_saved(username)
result = ('%s %s') % (response, str(emoji, 'utf-8'))
if not result:
result = response
if result:
flash(result)
return render_template('index.html')
@app.route('/chat/', methods=['GET'])
def chat():
intents = load_intents()
bot = Bot(intents, basedir)
bot.create_model()
bot.load_model()
username = request.args.get("username")
query = request.args.get("query")
result = None
tag, response, emoji = bot.response(query)
print('tag: %s' % tag)
print('response: %s' % response)
print('emoji: %s' % emoji)
if tag == 'save':
handle_save(username, emoji)
elif tag == 'return_saved':
emoji = handle_return_saved(username)
res = ('%s %s') % (response, str(emoji, 'utf-8'))
result = {'response': res}
if not result:
result = {'response': response}
return jsonify(result)
if __name__ == '__main__':
app.run()