-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
54 lines (45 loc) · 1.55 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
from flask import Flask, render_template, request, jsonify
import aiml
import os
from googletrans import Translator
kernel = aiml.Kernel()
penerjemah = Translator()
def load_kern(forcereload):
if os.path.isfile("bot_brain.brn") and not forcereload:
kernel.bootstrap(brainFile= "bot_brain.brn")
else:
kernel.bootstrap(learnFiles = os.path.abspath("aiml/std-startup.xml"), commands = "load aiml b")
kernel.saveBrain("bot_brain.brn")
kernel = aiml.Kernel()
if os.path.isfile("bot_brain.brn"):
kernel.bootstrap(brainFile = "bot_brain.brn")
else:
kernel.bootstrap(learnFiles = os.path.abspath("aiml/std-startup.xml"), commands = "load aiml b")
kernel.saveBrain("bot_brain.brn")
app = Flask(__name__)
@app.route("/")
def hello():
load_kern(False)
return render_template('chat.html')
@app.route("/ask", methods=['POST','GET'])
def ask():
message = str(penerjemah.translate(request.form['chatmessage'], dest='en').text)
print(message)
if message == "save":
kernel.saveBrain("bot_brain.brn")
return jsonify({"status":"ok", "answer":"Brain Saved"})
elif message == "reload":
load_kern(True)
return jsonify({"status":"ok", "answer":"Brain Reloaded"})
elif message == "quit":
return jsonify({"status":"ok", "answer":"exit Thank You"})
exit()
# kernel now ready for use
else:
# while True:
bot_response = kernel.respond(message)
# print bot_response
bot_response = penerjemah.translate(bot_response, dest='id').text
return jsonify({'status':'OK','answer':bot_response})
if __name__ == "__main__":
app.run(debug=True)