Exemplo n.º 1
0
 def train(engine: Engine, ctx: Context):
     datas, targets = ctx.inputs
     ctx.datas, ctx.targets = datas.to(ctx.device), targets.to(ctx.device)
     ctx.outputs = ctx.net(ctx.datas)
     ctx.loss = ctx.criterion(ctx.outputs, ctx.targets)
     ctx.optimizer.zero_grad()
     ctx.loss.backward()
     ctx.optimizer.step()
Exemplo n.º 2
0
 async def cog_command_error(self, ctx: Context,
                             error: commands.CommandError):
     ctx.locally_handled = True
     if isinstance(error, NotInVoice):
         return await ctx.send(
             "You must be in a voice channel to use this command.")
     elif isinstance(error, BotNotInVoice):
         return await ctx.send("I'm not in a voice channel.")
     else:
         ctx.locally_handled = False
Exemplo n.º 3
0
    def __init__(self, args):
        signal.signal(signal.SIGUSR1, self.load_items_rules)
        signal.signal(signal.SIGTERM, self.stop)
        self.loop = None

        self.context = Context()
        self.context.config = {'server': {'port': 8880}}
        self.context.add_cb(CB_ONCHANGE, self.on_item_change)

        self.conf_dir = args.config_dir or os.path.join(BASE_PATH, 'config')
        self.load_config()
Exemplo n.º 4
0
 def debug_train(engine: Engine, ctx: Context):
     datas, targets = ctx.inputs
     ctx.datas, ctx.targets = datas.to(ctx.device), targets.to(ctx.device)
     ctx.timer.mark("to device")
     ctx.outputs = ctx.net(ctx.datas)
     ctx.timer.mark("forward")
     ctx.loss = ctx.criterion(ctx.outputs, ctx.targets)
     ctx.timer.mark("criterion")
     ctx.optimizer.zero_grad()
     ctx.loss.backward()
     ctx.timer.mark("backward")
     ctx.optimizer.step()
     ctx.timer.mark("step")
Exemplo n.º 5
0
def start(r, c: Context):
    if c.last_user_utterance.intent == 'smalltalk.greetings.whatsup':
        r.say("smalltalk.greetings.whatsup")
        return
    r.say("hello")
    if not c.has_outgoing_intent('how are you', 10):
        r.ask("how are you")
        return 'asking', 'how_are_you', 1
    else:
        if not c.has_outgoing_intent('what i can do', 10):
            r.say("what i can do")
        else:
            r.say("what you can say")
        return States.SMALLTALK
Exemplo n.º 6
0
def record_phone_damage(r, c: Context):
    if c.current_question.id in ('damage_type', 'cause_of_damage'):
        return  # We handle this as a normal answerchecker

    dmg_type = c.last_user_utterance.parameters.get('damage_type')
    dmg_type = dmg_type[0] if isinstance(
        dmg_type, list) and len(dmg_type) > 0 else dmg_type
    if dmg_type:
        c.add_answer_to_question('damage_type', str(dmg_type))

    if not c.has_outgoing_intent("sorry for broken phone", 40):
        r.say("sorry for broken phone"
              )  # Automatically grounds damage_type if given

    c['is_phone_broken'] = True
    return True
Exemplo n.º 7
0
def parse_mesh_config(t):
    # Build the parser
    parser = yacc.yacc()
    parser.context = Context()
    parser.parse(t, lexer=lex)

    print 'Parsed OK'
    return parser.context
Exemplo n.º 8
0
 async def _listquotes(self, ctx: Context, *, query: str = None):
     """Retrieves a list of 10 random quotes that contain the query.
     """
     async with ctx.typing():
         quotes = await self.filter_quotes(ctx, query)
         rand.shuffle(quotes)
         selected = quotes[:min(len(quotes), 10)]
         await ctx.send(content=">>> " + '\n'.join(selected))
     await ctx.react(success)
def reset_database(r, c: Context, for_all=False):
    log.info("Resetting")
    if c.get("reset") is not None:
        r.say("system reset", parameters={"n_reset": c.get("reset")})
        c["reset"] = None
        raise StopPropagation

    if for_all and not is_admin(c.user):
        return r.say("no permission")

    users = None if for_all else [c.user]
    log.warning(
        f"Resetting and restarting for {'all' if all else [str(u) for u in users]}..."
    )

    # Reset the whole context (including UserAnswers)
    n_reset = c.reset_all()
    c["reset"] = n_reset

    raise ForceReevaluation
Exemplo n.º 10
0
 async def _randomquote(self, ctx: Context, *, query: str = None):
     """Retrieves a random quote that contains the query.
     """
     async with ctx.typing():
         quotes = await self.filter_quotes(ctx, query)
         if len(quotes) == 1:
             selected = quotes[0]
         else:
             selected = quotes[rand.randint(0, len(quotes) - 1)]
         await ctx.send(content='>>> ' + selected)
     await ctx.react(success)
Exemplo n.º 11
0
def clarify(r: ResponseComposer, c: Context):
    should_send_example = False

    if c.last_user_utterance.intent == 'example':
        should_send_example = True

    if c.has_outgoing_intent('give_hint', age_limit=2):
        should_send_example = True
    else:
        # Renders hint from `questionnaires.yml`
        r.give_hint(c.current_question)

    if should_send_example:
        send_example(r, c)
Exemplo n.º 12
0
    async def rawmsg(self, ctx: Context, message_id: int = None):
        """
        Get the raw content of a message.

        If the message is too long, it will be posted on a gist.
        """
        ref = ctx.message.reference
        if message_id is None and isinstance(ref.resolved, discord.Message):
            message_id = ref.resolved.id
        elif not ref:
            return await ctx.send("Give you a message you filthy noob")
        mess = await self.bot.http.get_message(ctx.channel.id, message_id)
        info = ctx.codeblock(json.dumps(mess, indent=4), language="json")
        if len(info) > 2000:
            return await ctx.post(
                info.removeprefix("```json\n").removesuffix("```"),
                "json",
                gist=True)
        return await ctx.send(info)
Exemplo n.º 13
0
def parse_zip_file(zip_in, zip_out):
    # Build the parser
    parser = yacc.yacc()

    fIn = ZipFile(zip_in, 'r')
    fOut = ZipFile(zip_out, 'w')
    parser.context = Context(src_zip_file=fIn, dst_zip_file=fOut)

    try:
        conf = fIn.open('config.txt').read()
    except:
        conf = fIn.open('src/config.txt').read()

    parser.parse(conf, lexer=lex)

    fIn.close()
    fOut.close()

    print 'Parsed OK'
    return parser.context
Exemplo n.º 14
0
    def __call__(self, context=None, **kwargs):
        """
        Create the widget with the given context and override some
        constructor arguments.

        :param context: context to create the widget in
        :returns: widget object
        """
        if context is not None:
            context = Context(context)
        args = self._kwargs.copy()
        args.update(kwargs)
        args['context'] = context
        try:
            widget = self._cls(**args)
            widget.__template__ = self
            widget.name = self.name
        except Exception, e:
            log.exception('unable to create widget %s', self._cls)
            raise RuntimeError('unable to create %s%s: %s' %
                               (self._cls, args.keys(), e))
Exemplo n.º 15
0
def change_formal_address(r, c: Context):
    if c.get("we_say_du"):
        r.say("we say du")
        c["we_say_du"] = False
        return

    ut = c.last_user_utterance
    if not ut.text:
        return

    # If the formal address changes, we need to force a reevaluation of the response templates parameters,
    # so that the changes in shared parameters go into effect
    if re.search(r'\b(du|dein|dich|dir)\b', ut.text, re.IGNORECASE):
        if c.user.formal_address is True:
            c.user.formal_address = False
            c["we_say_du"] = True
            c.user.save()
            raise ForceReevaluation
    elif re.search(r'\b([Ii]hr|Sie|Ihnen|Euer|haben [sS]ie|sind [Ss]ie)\b',
                   ut.text):
        if c.user.formal_address is False:
            c.user.formal_address = True
            c.user.save()
            raise ForceReevaluation
Exemplo n.º 16
0
 def validation(engine: Engine, ctx: Context):
     datas, targets = ctx.inputs
     ctx.datas, ctx.targets = datas.to(ctx.device), targets.to(ctx.device)
     ctx.outputs = ctx.net(ctx.datas)
     ctx.loss = ctx.criterion(ctx.outputs, ctx.targets)
Exemplo n.º 17
0
def context_mock(state, u):
    ctx = Context(user_mock(), initial_state=state)
    ctx.add_user_utterance(u)
    return ctx
Exemplo n.º 18
0
import torch
import flame

import torch.utils.data
from torchvision import datasets
import torchvision.transforms

from core import lenet, Context
from flame.engine import Phase

ctx = Context()

ctx.max_epoch = flame.hocon.get_int("strategy.epoch")

transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize(
        mean=flame.hocon.get_list("dataset.mean"),
        std=flame.hocon.get_list("dataset.std"),
    ),
])
train_dataset = datasets.MNIST(
    root=flame.hocon.get_string("dataset.root"),
    train=True,
    transform=transform,
    download=True,
)
train_loader = torch.utils.data.DataLoader(
    dataset=train_dataset,
    batch_size=flame.hocon.get_int("strategy.train.batch_size"),
    shuffle=True,
Exemplo n.º 19
0
class Main(object):
    running = True
    futs = []

    def __init__(self, args):
        signal.signal(signal.SIGUSR1, self.load_items_rules)
        signal.signal(signal.SIGTERM, self.stop)
        self.loop = None

        self.context = Context()
        self.context.config = {'server': {'port': 8880}}
        self.context.add_cb(CB_ONCHANGE, self.on_item_change)

        self.conf_dir = args.config_dir or os.path.join(BASE_PATH, 'config')
        self.load_config()

    def init_actors(self):
        mqtt_act = MqttActor()
        self.context.actors = {'mqtt': mqtt_act, 'astro': AstroActor()}

        if self.context.config['mqtt'].get('out_topic'):
            self.context.add_cb(CB_ONCHECK, mqtt_act.send_out)

        if 'modbus' in self.context.config:
            LOG.info('add modbus actor host %s',
                     self.context.config['modbus']['host'])
            self.context.actors['modbus'] = ModbusActor(
                self.context.config['modbus']['host'],
                self.context.config['modbus']['port'])

        if 'kodi' in self.context.config:
            for k, v in self.context.config['kodi'].items():
                LOG.info('add kodi actor %s, addr %s', k, v)
                self.context.actors['kodi_' + k] = KodiActor(k, v)

        if 'kankun' in self.context.config:
            for k, v in self.context.config['kankun'].items():
                LOG.info('add kankun actor %s, addr %s', k, v)
                self.context.actors['kankun' + k] = KankunActor(k, v)

        if 'slack' in self.context.config:
            self.context.actors['slack'] = SlackActor(
                self.context.config['slack']['url'])

        for actor in self.context.actors.values():
            actor.init(self.context.config, self.context)

        try:
            self.load_dump(DUMP_FILE)
        except:
            LOG.exception('cannot load state')

    def debug(self, sig, stack):
        LOG.info('DEBUG!!!')
        with open('running_stack', 'w') as f:
            f.write('Mahno debug\n\n')
            traceback.print_stack(stack, file=f)
            f.write('\n')
            f.write(self.context.items.as_list())
            f.write('\n')

    def stop(self, *args):
        self.save_dump(DUMP_FILE)
        sys.exit(0)

    def save_dump(self, fn):
        LOG.info('saving state')
        dump = self.context.items.as_list()
        with open(fn, 'wb') as f:
            pickle.dump(dump, f)

    def load_dump(self, fn):
        if not os.path.isfile(fn):
            LOG.info('no dump file')
            return

        LOG.info('loading state from dump')

        with open(fn, 'rb') as f:
            dump = pickle.load(f)

        for st in dump:
            s = self.context.items.get_item(st['name'])
            if s:
                s._value = s.convert_value(st['value'])
                s.changed = st['changed']
                s.checked = st['checked']
                # s.ttl = st.get('ttl', 0)

    def load_config(self):
        LOG.info('loading config files from %s', self.conf_dir)

        if not os.path.isfile(os.path.join(self.conf_dir, 'config.yml')):
            LOG.error('no config.yml in {}'.format(self.conf_dir))
            sys.exit(1)

        self.context.config = yaml.load(
            open(os.path.join(self.conf_dir, 'config.yml'),
                 'r',
                 encoding='UTF-8'))
        self.load_items_rules()

    def load_items_rules(self):
        LOG.info('loading items and rules')
        self.context.rules = []

        for s in os.listdir(self.conf_dir):
            if s.startswith('items_') and s.endswith('.yml'):
                try:
                    self.load_items_file(os.path.join(self.conf_dir, s))
                except:
                    LOG.exception('yml items load')
            elif s.startswith('rules_') and s.endswith('.yml'):
                try:
                    self.load_rules_file(os.path.join(self.conf_dir, s))
                except:
                    LOG.exception('yml rules load')

    def load_items_file(self, fname):
        conf = yaml.load(open(fname, 'r', encoding='UTF-8'))

        n = 0
        for item in conf:
            s = read_item(item)
            if s:
                self.context.items.add_item(s)
                n += 1
        LOG.info('load %s items from config %s', n, fname)

    def load_rules_file(self, fname):
        LOG.info('load rules from file %s', fname)
        conf = yaml.load(open(fname, 'r', encoding='UTF-8'))

        n = 0
        for r in conf:
            rule = None

            if 'trigger' in r:
                rule = Rule(r)
            if 'thermostat' in r:
                rule = ThermostatRule(r)

            if not rule:
                LOG.error('cannon make rule from definition %s', r)
                continue

            self.context.add_rule(rule)
            n += 1
        LOG.info('load %s rules from file %s', n, fname)

    async def cron_checker(self):
        while self.running:
            for rule in self.context.rules:
                try:
                    v = rule.check_time()
                    if v is not None:
                        self.do_async(rule.process_cron, v)
                except:
                    RULES_LOG.exception('cron worker on rule %s', rule.name)

            await asyncio.sleep(0.5)

    async def on_item_change(self, name, val, old_val, age):
        for rule in self.context.rules:
            if rule.check_item_change(name, val, old_val, age):
                try:
                    self.do_async(rule.process_item_change, name, val, old_val,
                                  age)
                except:
                    RULES_LOG.exception('item change on rule %s', rule.name)

    async def commands_processor(self):
        while self.running:
            if self.context.commands:
                cmd, args = self.context.commands.popleft()
                for actor in self.context.actors.values():
                    if actor.name == cmd:
                        self.do_async(actor.command, args)

            await asyncio.sleep(0.01)

    def do_async(self, fn, *args):
        if asyncio.iscoroutinefunction(fn):
            asyncio.ensure_future(fn(*args), loop=self.loop)
        else:
            self.loop.call_soon(functools.partial(fn, *args))

    def run(self):
        self.loop = asyncio.get_event_loop()
        self.context.loop = self.loop
        self.futs = []
        self.init_actors()

        for s in [self.cron_checker(), self.commands_processor()]:
            self.futs.append(asyncio.ensure_future(s))

        for actor in self.context.actors.values():
            self.futs.append(asyncio.ensure_future(actor.loop()))

        if self.context.actors.get('mqtt') and self.context.config['mqtt'].get(
                'out_topic'):
            self.futs.append(
                asyncio.ensure_future(
                    self.context.actors.get('mqtt').periodical_sender()))

        try:
            srv = http_server.get_app(self.context, self.context.config,
                                      self.loop)
            asyncio.ensure_future(srv, loop=self.loop)
            self.loop.run_forever()
        finally:
            self.running = False

            for x in self.context.actors.values():
                self.do_async(x.stop)

            asyncio.wait(self.futs, loop=self.loop)
            self.save_dump(DUMP_FILE)
            self.loop.close()
def c(user):
    return Context(user, None)