-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·86 lines (71 loc) · 2.54 KB
/
main.py
File metadata and controls
executable file
·86 lines (71 loc) · 2.54 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
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
from flask import Flask, render_template, request, jsonify, redirect, session
import aiml, os
from werkzeug import secure_filename
from pre import preprocessing as process
from auth import login_required, login, logout, register
app = Flask(__name__, static_url_path='/static')
app.config['UPLOAD_FOLDER'] = 'message/'
app.secret_key = "weubfyiwobyfuw;elfuvyw;vy56243i38v8;evwf8fvywelu"
kernel = aiml.Kernel()
@app.route("/")
@login_required
def hello():
return render_template('chat.html', user= session['user'])
@app.route("/train", methods=['GET','POST'])
@login_required
def train():
if request.method == "POST":
file = request.files['file']
name = session['user']
mess_folder = 'message/' + name
if not os.path.exists(mess_folder):
os.makedirs(mess_folder)
path = "aiml/" + name + "/Temp"
if not os.path.exists(path):
os.makedirs(path)
path1 = "aiml/" + name + "/XMLS"
if not os.path.exists(path1):
os.makedirs(path1)
path2 = "aiml/" + name + "/brain"
if not os.path.exists(path2):
os.makedirs(path2)
filename = secure_filename(file.filename)
if filename == "messages.htm":
app.config['UPLOAD_FOLDER'] = 'message/' + name + '/'
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
process(name)
return redirect('/')
else:
return ("Bad kind of filename, Kindly Upload your message.htm")
else:
return render_template('Train.html', user= session['user'])
@login_required
@app.route("/ask/<path:name>", methods=['POST'])
def ask(name):
message = str(request.form['messageText'])
user = session['user']
if not os.path.isdir("aiml/"+ user):
return jsonify({'status':'OK','answer': 'you need to train first'})
filename = "aiml/"+ user +"/brain/" + name + ".brn"
if os.path.isfile(filename):
kernel.bootstrap(brainFile = filename)
else:
res = 'unknown user' + name
return jsonify({'status':'OK','answer': res})
if message == "quit":
bot_response = "See you later " + name
else:
bot_response = kernel.respond(message)
return jsonify({'status':'OK','answer':bot_response})
@app.route('/register', methods=['GET', 'POST'])
def do_register():
return register()
@app.route('/login', methods=['GET', 'POST'])
def do_login():
return login()
@app.route('/logout')
@login_required
def do_logout():
return logout()
if __name__ == "__main__":
app.run(debug=True)