async def handle_add(channel, msg, attachments, **kwargs): global LAST_SAVED_FILE await status(msg='Adding image...') if not attachments: err_msg = config.format_string('Invalid use of {start_command_character}add. Usage: {start_command_character}add <class> <attachment> (No attachment given)') await send_message(channel, err_msg) return attach = attachments[0] try: img_class = msg[0] except: err_msg = config.format_string('Invalid use of {start_command_character}add. Usage: {start_command_character}add <class> <attachment> (No class given)') await send_message(channel, err_msg) return class_path = IMG_SAVE_PATH / 'train' / img_class class_path.mkdir(exist_ok=True) uploaded_filename = attach.filename if uploaded_filename[uploaded_filename.rindex('.'):] not in SUPPORTED_FILETYPES: err_msg = config.format_string('Invalid use of {start_command_character}add. Filetype must be one of {supported_filetypes}') await send_message(channel, err_msg) return fname = class_path / unique_filename(uploaded_filename) await attach.save(fname) LAST_SAVED_FILE = fname
async def handle_predict(channel, msg, attachments, **kwargs): await status(msg='predicting...') user_message_id = kwargs.get('msg_id') save_path = IMG_SAVE_PATH / 'predict' save_path.mkdir(exist_ok=True) if attachments: attach = attachments[0] uploaded_filename = attach.filename if uploaded_filename[uploaded_filename.rindex('.'):].lower( ) not in SUPPORTED_FILETYPES: err_msg = config.format_string( 'Invalid use of {start_command_character}predict. Filetype must be one of {supported_filetypes}' ) await send_message(channel, err_msg) return fname = save_path / unique_filename(uploaded_filename) await attach.save(fname) else: fname = save_path / unique_filename("") try: urllib.request.urlretrieve(msg[0], fname) except ValueError: err_msg = config.format_string( 'Invalid use of {start_command_character}predict. Usage: {start_command_character}predict <attachment, url> (No valid url given)' ) await send_message(channel, err_msg) return except: err_msg = config.format_string('Error occured upon loading url') await send_message(channel, err_msg) return cmd = f'python learner.py --img_path "{fname}"' if config.get('enable_auto_class_add'): cmd += f' --auto_class_add_threshold {config.get("auto_class_add_threshold")} --message_id {user_message_id}' stdout, stderr = await run_cmd(cmd) if stdout: stdout_msg = await send_message(channel, stdout) if stderr: await send_message(channel, f'[stderr]\n{stderr}') # only add to the queue if the image was auto-added to the training set if int(r.get(f'{user_message_id}_added')): queue.append({ 'user_message_id': user_message_id, 'bot_message_id': stdout_msg.id, 'auto_added_image': r.get(user_message_id) })
async def handle_help(channel, msg, attachments, **kwargs): msgs = [] start_command_character = config.get('start_command_character') for command, command_info in COMMANDS.items(): command_info = config.format_string(command_info['info']) msgs.append(f'{start_command_character}{command} - {command_info}') await send_message(channel, '\n'.join(msgs))
async def handle_toploss(channel, msg, attachments, **kwargs): err_msg = config.format_string( 'Top losses not found! Run {start_command_character}train first.') await send_file(channel, 'top_losses.jpg', err_msg)
async def handle_cm(channel, msg, attachments, **kwargs): err_msg = config.format_string( 'Confusion matrix not found! Run {start_command_character}train first.' ) await send_file(channel, 'confusion_matrix.jpg', err_msg)