-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.py
240 lines (189 loc) · 6.44 KB
/
commands.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from getpass import getpass
import json
import glob
import os
from pathlib import Path
from typing import Union
try:
import pytest
except ImportError:
pytest = None
from aids.app.client import AIDScrapper, ClubClient, HoloClient, bs4
import aids.to_html as to_html
from aids.app.settings import BASE_DIR, secrets_form, DEBUG
from aids.app.models import NAIScenario, Scenario
command_arg_dict = {
"Aid": {
"stories": ("title", "actions"),
"scenarios": ("title",),
"all": ("title", "actions"),
"fenix": (),
},
"Club": {"publish": ("title",)},
"Holo": {},
}
class Aid(AIDScrapper):
def __init__(self):
super().__init__()
self.login()
self.th = to_html.toHtml()
def stories(self, title, min_act):
self.adventures(title, min_act)
self.get_stories()
self.adventures.dump()
self.th.story_to_html()
def scenarios(self, title):
self.prompts(title)
self.get_scenarios()
self.prompts.dump()
self.th.scenario_to_html()
def all(self, title, min_act):
self.stories(title, min_act)
self.scenarios(title)
def fenix(self):
try:
self.prompts.load()
except FileNotFoundError:
self.get_scenarios()
self.prompts.dump()
self.upload_in_bulk(self.prompts)
class Holo(HoloClient):
pass
class Club(ClubClient):
def publish(self, title):
await_completition = True
while await_completition:
if bs4:
self.publish_scenario(title)
await_completition = False
else:
selection = input(
"bs4 not installed, unable to continue... want to install it now?"
"(Enter to install bs4)"
)
if not selection:
os.system("pip3 install bs4")
else:
break
def makejson(source_files: str = "*.scenario", target: str = ""):
data = _scenario_to_json(source_files)
if target:
data.default_json_file = target
data.dump()
def _reformat_context(json_data):
"""reformat the context as memory and AN
usually, 0 is memory whilst 1 is AN. But as
the russians say, \"Trust, but verify\"
"""
for data in json_data["context"]:
if (
not data["contextConfig"]["insertionPosition"]
or data["contextConfig"]["insertionPosition"] < -4
):
json_data["memory"] = data["text"]
elif data["contextConfig"]["insertionPosition"] == -4:
json_data["authorsNote"] = data["text"]
else:
# we can not verify, I guess.
json_data["memory"] = json_data["context"][0]["text"]
json_data["authorsNote"] = json_data["context"][1]["text"]
def _scenario_to_json(source_files: Union[str, Path]):
nai_file_name = glob.glob(str(source_files))
model = Scenario()
for name in nai_file_name:
with open(name) as file:
json_data = json.load(file)
_reformat_context(json_data)
# lorebook to worldInfo is way easier
json_data["worldInfo"] = [
{"keys": entry["keys"], "entry": entry["text"]}
for entry in json_data["lorebook"]["entries"]
]
model.add(json_data.copy())
if DEBUG is False:
print("-------------------------------------")
print(
f'Your NAI scenario "{json_data["title"]}" was successfully '
"re-formatted."
)
print("-------------------------------------")
return model
def makenai(
source_file: str = "scenario.json", target: str = "", single_files: bool = True
):
data = _json_to_scenario(source_file)
if target:
data.default_json_file = target
data.default_scenario_path = target
if single_files:
data.dump_single_files()
else:
data.dump()
def _json_to_scenario(source_file: Union[str, Path]) -> "NAIScenario":
model = NAIScenario()
data_scheme = model.data.copy()
an_scheme = data_scheme["context"].pop()
memory_scheme = data_scheme["context"].pop()
wi_entries_scheme = data_scheme["lorebook"]["entries"].pop()
with open(source_file) as file:
json_data = json.load(file)
for scenario in json_data:
data_scheme.update(scenario)
if "worldInfo" in scenario and scenario["worldInfo"]:
entries = []
for wi in scenario["worldInfo"]:
wi_entries_scheme.update({"text": wi["entry"], "keys": wi["keys"]})
entries.append(wi_entries_scheme.copy())
an_scheme.update({"text": scenario["authorsNote"]})
memory_scheme.update({"text": scenario["memory"]})
data_scheme.update(
{
"context": [memory_scheme.copy(), an_scheme.copy()],
"lorebook": {"entries": entries.copy()},
}
)
model.add(data_scheme.copy())
if DEBUG is False:
print("-------------------------------------")
print(
f'Your AID scenario "{scenario["title"]}" was successfully '
"re-formatted."
)
print("-------------------------------------")
return model
def test():
if pytest:
os.system(f"pytest {str(BASE_DIR)}/app/tests.py")
else:
os.system(f"python -m unittest -v aids.app.tests")
def help():
with open(BASE_DIR / "help.txt") as file:
print(file.read())
def register():
with open(BASE_DIR / "app/secrets.json", "w") as file:
secrets_form.update(
{
"AID_USERNAME": (user := input("AID username: ")),
"AID_PASSWORD": getpass("AID password: "),
}
)
json.dump(secrets_form, file)
print(f"User {user} successfully registered.")
def alltohtml(
file_dir: Union[str, Path] = "", story_outfile: str = "", scenario_outfile: str = ""
):
th = to_html.toHtml()
if file_dir:
th.out_path = file_dir
if story_outfile:
th.story_out_file = story_outfile
if scenario_outfile:
th.scen_out_file = scenario_outfile
try:
th.story_to_html()
except FileNotFoundError:
print("The were no stories to transform into .html")
try:
th.scenario_to_html()
except FileNotFoundError:
print("The were no scenarios to transform into .html")