-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
executable file
·394 lines (346 loc) · 13.1 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python3
import discord
from discord.ext import commands
import os
import uuid
import random
from dotenv import load_dotenv
import typing
import requests
import base64
import yaml
import json
import tarfile
from gha_update import update_repo
import re
load_dotenv()
intents = discord.Intents.all()
intents.message_content = True
TMP_DIR = '/tmp/rasa-discord'
TMP_ACTION_FILE = '/tmp/actions.py'
if not os.path.exists(TMP_DIR):
os.mkdir(TMP_DIR)
token = os.getenv('DFLOW_BOT_TOKEN')
rasa_domain_url = os.getenv('RASA_DOMAIN_URL')
rasa_train_model_path = os.getenv('RASA_TRAIN_MODEL_PATH')
rasa_token = os.getenv('RASA_TOKEN')
dflow_domain_url = os.getenv('DFLOW_DOMAIN_URL')
dflow_login_path = os.getenv('DFLOW_LOGIN_PATH')
dflow_register_path = os.getenv('DFLOW_REGISTER_PATH')
dflow_validate_path = os.getenv('DFLOW_VALIDATE_PATH')
dflow_generate_path = os.getenv('DFLOW_GENERATE_PATH')
dflow_push_model_path = os.getenv('DFLOW_PUSH_MODEL_PATH')
dflow_merge_path = os.getenv('DFLOW_MERGE_PATH')
rasa_chat_path = os.getenv('RASA_CHAT_PATH')
rasa_put_model_path = os.getenv('RASA_PUT_MODEL_PATH')
bot_commands = commands.Bot(command_prefix="!", intents=intents)
@bot_commands.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot_commands))
@bot_commands.command("ping")
async def ping(ctx):
await ctx.send('Pong!')
@bot_commands.command("register")
async def register(ctx, *, arg):
# Connect to dflow api
url = f"{dflow_domain_url}{dflow_register_path}"
username = str(ctx.message.author).replace("#",'').replace(".",'').replace(" ",'')
username = re.sub(r"[^a-zA-Z0-9_-]+", "", username)
payload = json.dumps({
'new_user': {
'username': username,
'password': '123123',
'email': arg
}
})
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
try:
response = requests.post(url, data = payload, headers = headers)
print(f"--> Register response: {response}")
if response.status_code in [200, 201, 202, 204]:
await ctx.send(f'User [{username}] registration completed! :robot:')
elif response.status_code in [400]:
await ctx.send(f'User [{username}] already registered!')
else:
print(f'reason {response.reason}')
raise Exception
except:
raise Exception('Register to dflow-api failed')
@register.error
async def register_error(ctx, error):
print('Error:', error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please provide an email...')
else:
await ctx.send(error)
@bot_commands.command("validate")
async def validate(ctx, arg: typing.Optional[discord.Attachment], text: typing.Optional[str]):
await ctx.send('Validating model...')
if text != None:
model = text
elif arg != None:
model = await arg.read()
model = model.decode()
else:
raise Exception('Please provide a correct model...')
# Connect to dflow api
url = f"{dflow_domain_url}{dflow_login_path}"
username = str(ctx.message.author).replace("#",'').replace(".",'').replace(" ",'')
username = re.sub(r"[^a-zA-Z0-9_-]+", "", username)
payload = {
'username': username,
'password': '123123'
}
try:
response = requests.post(url, data = payload)
print(f"--> Login response: {response}")
if response.status_code == 401:
raise Exception
token = response.json()['access_token']
headers = {'Authorization' : f'Bearer {token}'}
except:
raise Exception('Login to dflow-api failed :skull:')
model = model.encode('ascii')
model = base64.b64encode(model)
model = model.decode('ascii')
payload = f'fenc={model}'
url = f"{dflow_domain_url}{dflow_validate_path}"
try:
response = requests.post(url, headers = headers, params = payload)
print(f"--> Validaton response: {response}")
print(f"--> Validaton response JSON: {response.json()}")
if response.status_code not in [200, 201, 202, 204]:
raise Exception
status = response.json()['status']
if status == 200:
await ctx.send('Model validation succeeded :+1:')
else:
await ctx.send(f"Validation failed! Reason: {response.json()['message']} :skull:")
except:
raise Exception('Validation problem with the API')
@validate.error
async def validate_error(ctx, error):
print('Error:', error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please provide a model... ')
else:
await ctx.send(error)
@bot_commands.command("generate")
async def generate(ctx,
arg: typing.Optional[discord.Attachment],
text: typing.Optional[str]):
# await ctx.send('Generating Rasa model...')
if text != None:
model = text
elif arg != None:
model = await arg.read()
model = model.decode()
else:
raise Exception('Please provide a correct model...')
# Connect to dflow api
url = f"{dflow_domain_url}{dflow_login_path}"
username = str(ctx.message.author).replace("#",'').replace(".",'').replace(" ",'')
username = re.sub(r"[^a-zA-Z0-9_-]+", "", username)
payload = {
'username': username,
'password': '123123'
}
try:
response = requests.post(url, data = payload)
print(f"--> Login response: {response}")
if response.status_code == 401:
raise Exception
token = response.json()['access_token']
headers = {'Authorization' : f'Bearer {token}'}
except:
raise Exception('Login with dflow-api failed')
# Generate and store tarball
url = f"{dflow_domain_url}{dflow_generate_path}"
model = model.encode('ascii')
model = base64.b64encode(model)
model = model.decode('ascii')
payload = f'fenc={model}'
try:
response = requests.post(url,
headers=headers,
params=payload
)
print(f"--> Generation response: {response}")
if response.status_code not in [200, 201, 202, 204]:
await ctx.send(f"Model Generation failed! Reason: {response.json()['message']} :skull:")
raise Exception
u_id = uuid.uuid4().hex[0:8]
fpath = os.path.join(TMP_DIR, f'model-{u_id}.tar.gz')
with open(fpath, 'wb') as f:
f.write(response.content)
await ctx.send('dFlow to Rasa model transformation completed. :+1:')
except:
raise Exception('Generation problem with the API')
# Read tarball and send data for Rasa training
tar = tarfile.open(fpath)
payload = {}
for member in tar.getmembers():
f = tar.extractfile(member)
try:
content = f.read()
content = content.decode()
name = member.name.split('/')[-1]
if name in ['nlu.yml', 'stories.yml', 'rules.yml', 'config.yml', 'domain.yml', 'endpoints.yml']:
data = yaml.safe_load(content)
payload.update(data)
if name in ['actions.py']:
with open(TMP_ACTION_FILE, 'wt') as f:
f.write(content)
except:
continue
payload = yaml.dump(payload)
headers = {
'Content-Type': 'application/yaml'
}
try:
url = f"{rasa_domain_url}{rasa_train_model_path}?token={rasa_token}"
response = requests.post(url,
headers=headers,
data=payload,
verify=False
)
print(f"Training new model response: {response}")
filename = response.headers['filename']
print(f'Model {filename} trained successfully!')
await ctx.send('Rasa training completed! :+1:')
except:
print(f"Training new model response: {response.text}")
raise Exception('Problem with Rasa training')
# Activate new model in Rasa instance
model_file_path = f'/app/models/{filename}'
headers = {'Content-Type': 'application/json'}
body_params = {'model_file': model_file_path}
query_params = {'token': 'rasaToken'}
url = f"{rasa_domain_url}{rasa_put_model_path}"
try:
# await ctx.send('Activating new Rasa model...')
response = requests.put(url,
headers=headers,
json=body_params,
params=query_params,
verify=False
)
print(f"Activate new model response: {response}")
await ctx.send('Activated new Rasa model! :+1:')
except:
raise Exception('Problem when activating newly trained Rasa model.')
# Update Rasa action server
update_repo(TMP_ACTION_FILE)
await ctx.send('Generated new actions! :+1:')
await ctx.send('The Continues Delivery (CD) process will now synchronize new actions within the cluster.')
await ctx.send('This process usually takes ~2 minutes to finish')
@generate.error
async def generate_error(ctx, error):
print('Error:', error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please provide a model...')
else:
await ctx.send(error)
@bot_commands.command("push")
async def push(ctx, arg: typing.Optional[discord.Attachment], text: typing.Optional[str]):
await ctx.send('Pushing model...')
if text != None:
model = text
elif arg != None:
model = await arg.read()
model = model.decode()
else:
raise Exception('Please provide a correct model...')
# Connect to dflow api
url = f"{dflow_domain_url}{dflow_login_path}"
username = str(ctx.message.author).replace("#",'').replace(".",'').replace(" ",'')
username = re.sub(r"[^a-zA-Z0-9_-]+", "", username)
payload = {
'username': username,
'password': '123123'
}
try:
response = requests.post(url, data = payload)
print(f"--> Login response: {response}")
if response.status_code == 401:
raise Exception
token = response.json()['access_token']
headers = {'Authorization' : f'Bearer {token}'}
except:
raise Exception('Login to dflow-api failed')
# Store model
model = model.encode('ascii')
model = base64.b64encode(model)
model = model.decode('ascii')
payload = f'fenc={model}'
url = f"{dflow_domain_url}{dflow_push_model_path}"
try:
response = requests.post(url, headers = headers, params = payload)
print(f"--> Push model response: {response}")
if response.status_code not in [200, 201, 202, 204]:
raise Exception
if response.status_code == 200:
await ctx.send('Model pushed correctly!')
else:
await ctx.send(f"Push model failed! Reason: {response.reason}")
except:
raise Exception('Push model problem with the API')
@push.error
async def push_error(ctx, error):
print('Error:', error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please provide a model...')
else:
await ctx.send(error)
@bot_commands.command("train")
async def train(ctx):
await ctx.send('Training model...')
@bot_commands.command("train_merged")
async def train_merged(ctx):
await ctx.send('Training all models...')
@bot_commands.command("merge")
async def merge(ctx):
await ctx.send('Merging user models into a single model...')
url = f"{dflow_domain_url}{dflow_login_path}"
username = str(ctx.message.author).replace("#",'').replace(".",'').replace(" ",'')
username = re.sub(r"[^a-zA-Z0-9_-]+", "", username)
headers = {}
payload = {
'username': username,
'password': '123123'
}
try:
response = requests.post(url, data = payload)
print(f"--> Login response: {response}")
if response.status_code == 401:
raise Exception
token = response.json()['access_token']
headers = {'Authorization' : f'Bearer {token}'}
except:
raise Exception('Login to dflow-api failed')
url = f"{dflow_domain_url}{dflow_merge_path}"
try:
response = requests.get(url, headers=headers)
print(f"--> Merge models response: {response}")
if response.status_code not in [200, 201, 202, 204]:
raise Exception
if response.status_code == 200:
await ctx.send('Model merged correctly!')
u_id = uuid.uuid4().hex[0:8]
fpath = os.path.join(TMP_DIR, f'model-merged-{u_id}.dflow')
with open(fpath, 'wb') as f:
f.write(response.content)
await ctx.send(file=discord.File(fpath))
else:
await ctx.send(f"Merge models failed! Reason: {response.reason}")
except Exception as e:
raise Exception(f'Merge Models problem with the dFlow API: {e}')
@merge.error
async def merge_error(ctx, error):
print('Error:', error)
await ctx.send(error)
if __name__ == "__main__":
bot_commands.run(token)