-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
110 lines (80 loc) · 4.92 KB
/
setup.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
import os
import pip
from global_constants import DB_NAME
# We have to determine this BEFORE calling other imports to prevent creating an empty db before execution of config-script
db_exists = os.path.exists(DB_NAME)
from peewee import *
import modules
from starterkit.fallback_module.conf import chatbot, db
from entities import *
""" This file is only needed ONCE (when executing the assistant the first time).
If you want to reset your assistant just delete the 'db.sqlite3' in the project's
root directory and restart this script. """
""" ++++++++++++++++++++++ Assistant modules +++++++++++++++++++++++++++++++++++++++
No modules are required. If you don't want to use any additional features, then
you can change the following array to an empty one:
ENABLED_MODULES = []
If you disabled all modules then the assistant can only talk with you (= get_smart_answer).
By this you can also easily add some modules to your project and place it here into the list.
The more modules you have enabled the longer a potential answer of the bot might need.
You can also create modules by your own. Just look into abstr_answer.py for a more detailed
documentation. When you created one you place your module files into the 'modules' package
to keep the project structure clean and then just import it here and place it in the
'ENABLED_MODULES'-array.
CHANGE: ++ OPTIONAL ++ [-> by default, the assistant uses all modules] """
""" Your IDE might indicate that no reference has been found in __init__.py.
As we are importing all modules dynamically, you can safely ignore this warning. """
ENABLED_MODULES = [
modules.get_welcome_msg,
modules.get_random_question,
modules.get_random_fact
] # DO NOT add get_smart_answer.py here, bc. it is not a module
""" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++ BELOW CODE SHOULDN'T BE CHANGED, UNLESS YOU KNOW WHAT YOU ARE DOING +++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ """
# Installer of dependencies (used e.g. by conf.py's)
def install_dependency(package):
pip.main(['install', package])
if not db_exists: # do not evaluate here again whether db exists (bc. of imports and automatic creation on connection etc.)
print("Starting assistant setup.")
# +++++++++++++++++++++++ Database setup - Persistence ++++++++++++++++++++++++++
print("SETUP: Starting db and dependency setup.")
# Connect to db, create db structure and insert data
db.connect()
db.create_tables([
EnabledModule, ChatKeyword, EnabledModuleHasChatKeyword
])
# INSERT all configured/enabled modules ++++++++++++++++++++++
for module in ENABLED_MODULES:
# Save configured module from conf.py file
if module.conf.ENABLED_MODULE.save() <= 0:
print("CONFIDENTIAL:ERROR: Could not enable module -> "+str(module.conf.ENABLED_MODULE.class_name))
else: # only save module specific params if previous operation was successful.
# Save all keywords of enabled module + save relationship afterwards
for chat_keyword in module.conf.CHAT_KEYWORDS:
# Save keyword
if chat_keyword.save() <= 0:
print("CONFIDENTIAL:ERROR: Could not save keyword -> "+str(chat_keyword.chat_keyword))
else: # only save relationship if keyword saving was successful
# Create relationship to module
if EnabledModuleHasChatKeyword(
enabled_module_id=module.conf.ENABLED_MODULE,
chat_keyword_id=chat_keyword
).save() <= 0:
print("CONFIDENTIAL:ERROR: Could not establish relationship between -> "+str(module.conf.ENABLED_MODULE.class_name)+" and "+str(chat_keyword.chat_keyword))
# After db setup of single module, start dependency installing of module
for dependency in module.conf.DEPENDENCIES:
install_dependency(dependency)
""" +++++++++ Train assistant (= default module (not removeable without coding) +++
-> Train bot/assistant with default language (english) """
print("Training assistant, this can take a while.")
#TODO: make language configurable (also modules) and also train via twitter (also language chooseable)
#chatbot.train("chatterbot.corpus.english")
chatbot.train()
print("SETUP: Ended db and dependency setup.")
# TODO: Delete conf.py's after successful installation (or ask user to remove installation data = BETTER), bc. only needed once
else:
print("CONFIDENTIAL: Found database 'db.sqlite3'. Stopping script execution.\n"+
"Solution -> Delete the database file and re-execute this script OR just start the main.py.")