Beispiel #1
0
 def from_dict(cls, raw, Scenario, scenario_db=None):
     if 'scenario' in raw:
         scenario = Scenario.from_dict(None, raw['scenario'])
     # Compatible with old data formats (to be removed)
     elif scenario_db:
         print('WARNING: scenario should be provided in the example')
         scenario = scenario_db.get(raw['scenario_uuid'])
     else:
         raise ValueError('No scenario')
     uuid = raw['scenario_uuid']
     events = Event.gather_eval([Event.from_dict(e) for e in raw['events']])
     outcome = raw['outcome']
     ex_id = raw['uuid']
     if 'agents' in raw:
         agents = {int(k): v for k, v in raw['agents'].iteritems()}
     else:
         agents = None
     agents_info = raw.get('agents_info', None)
     return Example(scenario,
                    uuid,
                    events,
                    outcome,
                    ex_id,
                    agents,
                    agents_info=agents_info)
Beispiel #2
0
 def test_dict(cls, raw):
     uuid = raw['scenario_uuid']
     events = Event.gather_eval([Event.from_dict(e) for e in raw['events']])
     outcome = raw['outcome']
     ex_id = raw['uuid']
     if 'agents' in raw:
         agents = {int(k): v for k, v in raw['agents'].iteritems()}
     else:
         agents = None
     agents_info = raw.get('agents_info', None)
     return Example(None,
                    uuid,
                    events,
                    outcome,
                    ex_id,
                    agents,
                    agents_info=agents_info)
Beispiel #3
0
    def render_chat(cls,
                    chat,
                    agent=None,
                    partner_type='human',
                    worker_ids=None):
        events = Event.gather_eval(
            [Event.from_dict(e) for e in chat["events"]])

        if len(events) == 0:
            return False, False, None

        def get_worker_id(agent_id):
            if worker_ids is None:
                return 'N/A'
            id_ = worker_ids.get(str(agent_id), None)
            return id_ if id_ is not None else ''

        chat_html = [
            '<div class=\"chatLog\">',
            '<div class=\"divTitle\"> Chat Log: %s <br> Agent 0: %s Agent 1: %s </div>'
            % (chat['uuid'], get_worker_id(0), get_worker_id(1)),
            '<table class=\"chat\">'
        ]

        # Used for visualizing chat during debugging
        agent_str = {0: '', 1: ''}
        if agent is not None:
            agent_str[agent] = 'Agent %d (you)' % agent
            agent_str[1 - agent] = 'Agent %d (%s)' % (1 - agent, partner_type)
        elif 'agents' in chat and chat['agents']:
            for agent in (0, 1):
                agent_str[agent] = 'Agent %d (%s)' % (
                    agent, cls.agent_labels[chat['agents'][str(agent)]])
        else:
            for agent in (0, 1):
                agent_str[agent] = 'Agent %d (%s)' % (agent, 'unknown')

        for event in events:
            if not event.time:
                t = None
            else:
                t = datetime.datetime.fromtimestamp(float(
                    event.time)).strftime('%Y-%m-%d %H:%M:%S')
            a = agent_str[event.agent]
            s = cls.render_event(event)
            if s is None:
                continue

            try:
                tags = ', '.join(event.tags)
            except AttributeError:
                tags = ''

            if event.metadata is None:
                response_tag = ''
                template = ''
                received_row = None
            else:
                sent_data = event.metadata['sent']
                response_tag = sent_data['logical_form']['intent']
                template = sent_data['template']
                if isinstance(template, dict):
                    template = template['template']

                # Received event
                received_data = event.metadata['received']
                partner_tag = received_data['logical_form']['intent']
                partner_template = ' '.join(received_data['template'])
                received_row = '<tr class=\"agent%d\">\
                        <td class=\"time\">%s</td>\
                        <td class=\"agent\">%s</td>\
                        <td class=\"tags\">%s</td>\
                        <td class=\"act\">%s</td>\
                        <td class=\"template\">%s</td>\
                        <td class=\"message\">%s</td>\
                       </tr>' % (event.agent, '', '', '', partner_tag,
                                 partner_template, '')

            row = '<tr class=\"agent%d\">\
                    <td class=\"time\">%s</td>\
                    <td class=\"agent\">%s</td>\
                    <td class=\"tags\">%s</td>\
                    <td class=\"act\">%s</td>\
                    <td class=\"template\">%s</td>\
                    <td class=\"message\">%s</td>\
                   </tr>' % (event.agent, t, a, tags, response_tag, template,
                             s)
            if received_row:
                chat_html.append(received_row)
            chat_html.append(row)

        chat_html.extend(['</table>', '</div>'])

        completed = False if chat["outcome"] is None or chat["outcome"][
            "reward"] == 0 else True

        return completed, True, chat_html