Esempio n. 1
0
    async def on_message_delete(self, message):
        '''On a deleted message, post in the deleted messages channel who, when,
        and what the message is.'''

        if ctx.message.author == self.bot.user:
            return

        # create an embed for a deleted message.
        embed = utils.Embed("Deleted message")

        # Embeds are weird, just check the message to see char limit. this can be
        #   changed.
        if len(message.content) > 250:
            val_msg = f"Channel: <#{message.channel.id}>\n"\
            f"User: <@!{message.author.id}>\nMessage: {message.content[0:250]}...\n\n"\
            f"Time (UTC): {utils.dt_obj_to_str(utils.get_utc_time())}"
        else:
            val_msg = f"Channel: <#{message.channel.id}>\n"\
                f"User: <@!{message.author.id}>\nMessage: {message.content}\n\n"\
                f"Time (UTC): {utils.dt_obj_to_str(utils.get_utc_time())}"

        embed.add_text(name="Details", value=val_msg)
        embed.set_thumbnail(message.author.avatar_url)

        # get the logging channel
        channel = message.guild.get_channel(self.kwargs['logging_channel_id'])
        await channel.send(embed=embed.to_ctx())  # send the message
Esempio n. 2
0
    async def on_message_edit(self, before, after):
        '''Logs if a message got edited.'''

        if before.author == self.bot.user:
            return

        # create an embed for a deleted message.
        embed = utils.Embed("Edited message")

        # Embeds are weird, just check the message to see char limit. this can be
        #   changed.

        msg_info = [
            msg.content[0:250] +
            "..." if len(msg.content) > 250 else msg.content
            for msg in [before, after]
        ]

        val_msg = f"Channel: <#{before.channel.id}>\n"\
        f"User: <@!{before.author.id}>\n\n"\
        f'**Before:** {msg_info[0]}\n'\
        f'**After:** {msg_info[1]}\n\n'\
        f"Time (UTC): {utils.dt_obj_to_str(utils.get_utc_time())}"

        embed.add_text(name="Details", value=val_msg)
        embed.set_thumbnail(before.author.avatar_url)

        # get the logging channel
        channel = before.guild.get_channel(self.kwargs['logging_channel_id'])
        await channel.send(embed=embed.to_ctx())  # send the message
Esempio n. 3
0
    async def on_command_error(self, ctx: context.Context, error: Exception):
        """Logs errors for command, then send them into the user"""
        error = getattr(error, "original", error)

        clean_tb = utils.clean_tb_from_exc(error)
        self.logger.warn(COMMAND_ERROR_TEMPLATE, str(ctx.command),
                         ctx.message.content, clean_tb)
        if isinstance(error, commands.CommandNotFound):
            corrected_command = self.fuzzy_search_commands(ctx.invoked_with)
            if not corrected_command:
                return
            try:
                await corrected_command.can_run(ctx)
            except commands.CommandError:
                return self.dispatch("command_error", ctx, error)
            else:
                ctx.command = corrected_command
                return await ctx.reinvoke(call_hooks=True)

        title = f"An error has occured : {error.__class__.__name__}",
        description = utils.to_codeblocks(error, lang='py')
        embed = utils.Embed(title=title, description=description)

        embed.add_field(name="Support server",
                        value=f"[Support server]({config.SUPPORT_SERVER})")

        await ctx.send(embed=embed)
Esempio n. 4
0
    def format_page(self, menu: menus.MenuPages, page: str):
        """Formats the page into an embed"""

        embed = utils.Embed(title=self.title, description=page)

        embed.add_field(name='Description',
                        value=getattr(self.entity, 'help',
                                      self.entity.description))

        embed.set_footer(
            text=f'Page {menu.current_page + 1} out of {self.get_max_pages()}')

        if not self.walked_commands:  # we got an empty cog / group or one that only sends help
            return embed

        example_amount = min(3, len(self.walked_commands))

        example_commands = random.sample(self.walked_commands,
                                         k=example_amount)

        # we might have a cog that mixes both subclassed and default commands

        examples = [
            getattr(c, 'get_example', self.no_ret)(menu.ctx)
            for c in example_commands
        ]

        if not (filtered_examples := [*filter(None, examples)]):
            return embed
def EstimateChord(idx, dnnmodel, todir=False):
    #dnn = networks.FeatureDNN()
    #dnn = networks.ConvnetFeatExtractor()
    dnn = networks.FullCNNFeatExtractor()
    #dnn = networks.NoOperation()
    dnn.load(dnnmodel)
    dnn.to_gpu(0)
    decoder = networks.NBLSTMCRF()
    decoder.load()
    decoder.to_gpu(0)
    cqtfilelist = np.array(find_files(const.PATH_HCQT, ext="npy"))[idx]
    i = 0
    chainer.config.train = False
    chainer.config.enable_backprop = False
    for cqtfile in cqtfilelist:
        cqt = utils.Embed(utils.PreprocessSpec(np.load(cqtfile)[:, :, :]), 1)
        chroma = dnn.GetFeature(cp.asarray(cqt)).data
        path = decoder.argmax(chroma)
        feat = cp.asnumpy(chroma)
        if todir:
            fname = cqtfile.split("/")[-1] + ".lab"
            alb = cqtfile.split("/")[-2]
            utils.SaveEstimatedLabelsFramewise(
                path, const.PATH_ESTIMATE_CROSS + alb + "/" + fname, feat)
        else:
            utils.SaveEstimatedLabelsFramewise(
                path, const.PATH_ESTIMATE + "%03d.lab" % i, feat)
        i += 1
Esempio n. 6
0
    def format_page(self, menu: menus.MenuPages,
                    page: tp.List[ExtensionResult]) -> utils.Embed:
        """Formats the page into an embed"""

        embed = utils.Embed(title=self.load_type,
                            color=discord.Color.orange(),
                            default_inline=False)

        for ext_name, error in page:

            clean_ext_name = discord.utils.escape_markdown(ext_name)

            if not isinstance(error, str):

                if isinstance(error, EXTENSIONS_IGNORE
                              ):  # those errors aren't worth a full traceback
                    error = str(error)

                else:
                    error = utils.format_exception(*utils.exc_info(error),
                                                   limit=4)

            embed.add_field(name=clean_ext_name,
                            value=utils.codeblock(error, lang='py')[:1024])

        return embed
Esempio n. 7
0
    async def format_page(self, menu: MediaPages, _):
        embed = utils.Embed(title="Help on how to navigate around")
        extra_emojis = [
            f"{emoji} {source.__doc__}"
            for emoji, source
            in menu.extra_sources.items()
            if emoji != self.emoji
        ]

        text = (
            f"You left on page {menu.current_page + 1}, "
            "I'll take you back there if you press any button"
        )
        embed.set_footer(text=text)
        return embed(description='\n\n'.join(extra_emojis))
Esempio n. 8
0
    async def send_command_help(self, command: tp.Union[commands.Command,
                                                        utils.AyumiCommand]):
        """Help for commands"""
        embed = utils.Embed(title=self.get_command_signature(command))

        embed.add_field(name='Description', value=command.help, inline=False)

        embed.add_field(name='Aliases',
                        value=', '.join(command.aliases) or 'No aliases')

        example = getattr(command, 'get_example',
                          self.no_example)(self.context)

        embed.add_field(name='Example', value=example)

        await self.get_destination().send(embed=embed)
Esempio n. 9
0
    async def source(self,
                     ctx: core.Context,
                     *,
                     target: CommandConverter = None):
        """Gets the source for a command"""
        if target is None:
            return await ctx.send(
                f"Drop a star to support my development !\n<{self.bot.config['github']['url']}>"
            )

        callback = target.callback

        if 'help' in target.name:  # special case
            callback = self.bot.help_command.__class__

        try:
            source_lines, line_number = inspect.getsourcelines(callback)

        except OSError:
            raise commands.BadArgument(
                "Sorry ! I couldn't retrieve this command's source code")

        source_lines = textwrap.dedent(''.join(source_lines))

        module = callback.__module__.replace('.', '/') + '.py'

        github_link = f"{self.bot.config['github']['url']}{GITHUB_PATH}{module}#L{line_number}"

        embed = utils.Embed(
            title=f"""Here's the source the command named "{target}" !""")

        embed.add_fields(('External view', f'[Github]({github_link})'),
                         ('Module', discord.utils.escape_markdown(module)),
                         ('Line', line_number))

        if len(source_lines) > 2000:

            content = "Sorry ! The source is too long so I can only send the external view"

            return await ctx.send(content=content, embed=embed)

        src = utils.codeblock(source_lines, lang='py')
        await utils.OnePage({'embed': embed, 'content': src}).start(ctx)
def TrainTranscribeDNNChord(idx, epoch=20, saveas="dnn_deepchroma.model"):
    cqtfilelist = np.array(find_files(const.PATH_HCQT, ext="npy"))[idx]
    chordlablist = np.array(
        find_files(const.PATH_CHORDLAB, ext=["lab", "chords"]))[idx]

    featurelist = []
    targetlist = []
    chainer.config.train = True
    chainer.config.enable_backprop = True

    for cqtfile, labfile in zip(cqtfilelist, chordlablist):
        cqt = np.load(cqtfile)[0, :, :]
        chroma = voc.LoadChromaTarget(labfile)
        min_sz = min([cqt.shape[0], chroma.shape[0]])
        cqt = utils.Embed(utils.PreprocessSpec(cqt[:min_sz]), size=7)
        chroma = chroma[:min_sz]
        featurelist.append(cqt)
        targetlist.append(chroma.astype(np.int32))
    featurelist = np.concatenate(featurelist)
    targetlist = np.concatenate(targetlist)
    itemcnt = targetlist.shape[0]
    print("DNN Training begin...")
    dnn = networks.FeatureDNN()
    dnn.train = True
    model = networks.DNNModel(predictor=dnn)
    model.to_gpu()
    opt = optimizers.AdaDelta()
    opt.setup(model)
    for ep in range(epoch):
        randidx = np.random.permutation(itemcnt)
        sumloss = 0.0
        for i in range(0, itemcnt, const.DNN_TRAIN_BATCH):
            X = cp.asarray(featurelist[randidx[i:i + const.DNN_TRAIN_BATCH]])
            T = cp.asarray(targetlist[randidx[i:i + const.DNN_TRAIN_BATCH]])
            opt.update(model, X, T)
            sumloss += model.loss.data * const.DNN_TRAIN_BATCH
        print("epoch %d/%d  loss=%.3f" % (ep + 1, epoch, sumloss / itemcnt))

    dnn.save(saveas)
Esempio n. 11
0
    def format_page(self, menu: menus.Menu, anime: dict):
        """An extremely lazy way to put everything together"""

        title = [
            anime.get('title'),
        ]  # api is kind of inconsistant

        if rating := anime.get('rated'):
            title.append(f'[{rating}]')

        if episodes := anime.get('episodes'):
            title.append(f"Episodes : {episodes}")

        embed = utils.Embed(
            title=' | '.join(title),
            url=anime.get('url'),
            description=(anime.get('synopsis')
                         or 'Not found')[:2000])  # might get none

        embed.set_footer(text=self.footer)

        funcs = (
            lambda: embed.set_thumbnail(url=anime['image_url']),
            lambda: embed.add_field(
                name='Genres', value=self.format_named_data(anime['genres'])),
            lambda: embed.add_field(name='Producers',
                                    value=self.format_named_data(anime[
                                        'producers'])),
            lambda: embed.add_field(
                name='Licensors', value='-' + '\n-'.join(anime['licensors'])),
            lambda: embed.add_field(name='Rank', value=anime['rank']),
def TrainNStepCRF(idx,
                  epoch=20,
                  augment=0,
                  featmodel=const.DEFAULT_CONVNETFILE,
                  path_blstm="blstm.model",
                  savefile="nblstm_crf.model"):
    cqtfilelist = np.array(find_files(const.PATH_HCQT, ext="npy"))
    chordlablist = np.array(
        find_files(const.PATH_CHORDLAB, ext=["lab", "chords"]))
    if idx is not None:
        cqtfilelist = cqtfilelist[idx]
        chordlablist = chordlablist[idx]
    chainer.config.train = False
    chainer.config.enable_backprop = False
    #dnn = networks.TripleDNNExtractor()
    #dnn = networks.FeatureDNN()
    dnn = networks.FullCNNFeatExtractor()
    #dnn = networks.NoOperation()
    #dnn = networks.ConvnetFeatExtractor()
    dnn.load(featmodel)
    dnn.to_gpu(0)

    rnn = networks.NBLSTMCRF()
    rnn.blstm.load(path_blstm)
    rnn.to_gpu(0)
    opt = optimizers.MomentumSGD()
    opt.setup(rnn)
    #opt.add_hook(optimizer.WeightDecay(0.001))
    X = []
    T = []
    for cqtfile, labfile in zip(cqtfilelist, chordlablist):
        cqt = utils.Embed(utils.PreprocessSpec(np.load(cqtfile)[:, :, :]), 1)
        feature = cp.asnumpy(dnn.GetFeature(cp.asarray(cqt)).data)
        lab = utils.LoadLabelArr(labfile)
        min_sz = min([feature.shape[0], lab.shape[0]])
        X.append(feature[:min_sz, :])
        T.append(lab[:min_sz])
    sizes = np.array([x.shape[0] for x in X], dtype="int32")
    print("start epoch:")
    chainer.config.train = False
    chainer.config.enable_backprop = True
    last_loss = np.inf
    for ep in range(epoch):
        sum_loss = 0.0
        rand_songid = np.random.randint(len(X),
                                        size=np.sum(sizes) //
                                        const.DECODER_TRAIN_SEQLEN * 8)
        for i in range(0, rand_songid.size, const.DECODER_TRAIN_BATCH):
            xbatch = []
            tbatch = []
            for songid in rand_songid[i:i + const.DECODER_TRAIN_BATCH]:
                seq_len = sizes[songid]
                idx = np.random.randint(seq_len - const.DECODER_TRAIN_SEQLEN -
                                        1)
                x_snip = X[songid][idx:idx + const.DECODER_TRAIN_SEQLEN, :]
                t_snip = T[songid][idx:idx + const.DECODER_TRAIN_SEQLEN]
                if augment > 0:
                    shift = np.random.randint(augment)
                    x_snip, t_snip = shift_data(x_snip, t_snip, shift)
                xbatch.append(Variable(cp.asarray(x_snip)))
                tbatch.append(Variable(cp.asarray(t_snip)))
            rnn.cleargrads()
            opt.update(rnn, xbatch, tbatch)
            sum_loss += rnn.loss.data

        print("epoch %d/%d loss=%.3f" % (ep + 1, epoch, sum_loss / 12800.0))
        rnn.save(savefile)
Esempio n. 13
0
    np.stack([
        np.abs(
            cqt(wav,
                sr=C.SR,
                hop_length=C.H,
                n_bins=C.BIN_CNT,
                bins_per_octave=C.OCT_BIN,
                fmin=fmin * (h + 1),
                filter_scale=2,
                tuning=None)).T.astype(np.float32) for h in range(C.CQT_H)
    ]))
spec_dnn = U.Embed(U.PreprocessSpec(
    np.abs(
        cqt(wav,
            sr=C.SR,
            hop_length=C.H,
            n_bins=144,
            bins_per_octave=24,
            filter_scale=2,
            tuning=None)).T.astype(np.float32)),
                   size=1)

#dat = np.load("/media/wuyiming/TOSHIBA EXT/midihcqt_12/000005.npy")
#dat_24 = np.load("/media/wuyiming/TOSHIBA EXT/midihcqt_24/000005.npz")
#spec_dnn = U.Embed(U.PreprocessSpec(dat_24["spec"]),size=7)

spec = spec[:, :250, :]
spec_dnn = spec_dnn[:250, :]
cnn = networks.FullCNNFeatExtractor()
cnn.load("fullcnn_crossentropy_6000.model")

deepchroma = networks.FeatureDNN()
Esempio n. 14
0
 async def format_page(self, menu: MediaPages, data: dict) -> utils.Embed:
     """Formats the media into a embed showing the main informations"""
     embed = utils.Embed(title=self.format_title(data))
     size = ("medium", "extraLarge")[self.__class__ is TemplateMediaSource]
     if cover_img := data["coverImage"][size]:
         embed.set_thumbnail(url=cover_img)
Esempio n. 15
0
 async def send_bot_help(self, mapping):
     embed = utils.Embed(
         title=f"[{self.bot.user}] here is my general help command !")