Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
[Empathetic Dialogues] Switch to DialogTeacher (#4405)
Browse files Browse the repository at this point in the history
* Start revising ED teacher

* Convert over ED teacher

* Silly

* Minor

* Remove episode_done

* More cleanup

* Fix

* Test fix

* Force new CI check

* Note

* Cleanup

* Update parlai/tasks/empathetic_dialogues/agents.py

Co-authored-by: Stephen Roller <roller@fb.com>

* Update parlai/tasks/empathetic_dialogues/agents.py

Co-authored-by: Stephen Roller <roller@fb.com>

* Minor

* Fixes

* EDPersonaTopicifierTeacher fix

* Fix ID

* Hopefully fix style gen teacher PR

* Add back fields

* Update test_blended_skill_talk.py

* Update test_blended_skill_talk.py

* Convert over EDPersonaTopicifierTeacher

* EDPersonaTopicifierTeacher overhaul

* Minor

* Minor

* Remove imports

* Black

Co-authored-by: Stephen Roller <roller@fb.com>
  • Loading branch information
EricMichaelSmith and stephenroller committed Mar 15, 2022
1 parent 17afcb5 commit 4f7b38e
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 458 deletions.
113 changes: 27 additions & 86 deletions parlai/tasks/blended_skill_talk/agents.py
Expand Up @@ -4,15 +4,13 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from parlai.core.params import ParlaiParser
import copy
import json
import os
import random
import re
from collections import defaultdict
from typing import List, Optional, Dict, Tuple
from tqdm import tqdm

from parlai.core.opt import Opt
from parlai.core.teachers import (
Expand Down Expand Up @@ -65,7 +63,12 @@ def _topic_to_persona_path(opt: Opt) -> str:


def _cached_data_path(opt: Opt, experiencer_side_only: bool) -> str:
# Build the data if it doesn't exist.
"""
Build the data if it doesn't exist.
See EDPersonaTopicifierTeacher in ParlAI v1.5.1 and earlier for the code to add
persona strings to the base EmpatheticDialogues dataset.
"""
build(opt)
dt = opt['datatype'].split(':')[0]
side_string = 'experiencer_only' if experiencer_side_only else 'both_sides'
Expand Down Expand Up @@ -168,101 +171,39 @@ class EDPersonaTopicifierTeacher(EmpatheticDialoguesTeacher):
Adds persona and WoW topic to ED context strings.
"""

RECOMPILE_DEFAULT = False

@classmethod
def add_cmdline_args(
cls, parser: ParlaiParser, partial_opt: Optional[Opt] = None
) -> ParlaiParser:
super().add_cmdline_args(parser, partial_opt=partial_opt)
agent = parser.add_argument_group('EDPersonaTopicifierTeacher arguments')
agent.add_argument(
'--recompile-persona-topic-data',
type='bool',
default=cls.RECOMPILE_DEFAULT,
help='Re-compile data with ConvAI2 personas and WoW topics added. Only useful for demonstrating how data was produced.',
)
return parser

def __init__(self, opt, shared=None):
self.persona_topicifier = PersonaTopicifier(
opt=opt, should_have_personas=False, should_have_topics=False
)
super().__init__(opt, shared=shared)
self.id = 'parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher'

if (
self.remove_political_convos is True
or self.opt.get('deepmoji') is not None
or self.opt.get('fasttextloc') is not None
or self.opt.get('prepend', -1) > 0
):
raise NotImplementedError(
'Removing political conversations or using deepmoji, fasttextloc, or '
'prepend not supported with this teacher.'
)

# Running over all examples is really slow because the process of finding a WoW
# topic is expensive, so let's load cached data with personas and topics unless
# --recompile-persona-topic-data is True
if opt.get('recompile_persona_topic_data', self.RECOMPILE_DEFAULT):
self.data_path = (
_cached_data_path(
opt=self.opt, experiencer_side_only=self.experiencer_side_only
)
+ '.recompiled'
)
warn_once(f'Compiling data file for {self.data_path}.')
self.persona_topic_data = self._compile_data()
warn_once(f'Saving data to {self.data_path}.')
with PathManager.open(self.data_path, 'w') as f_write:
json.dump(self.persona_topic_data, f_write)
else:
self.data_path = _cached_data_path(
opt=self.opt, experiencer_side_only=self.experiencer_side_only
)
warn_once(f'Loading cached data from {self.data_path}.')
with PathManager.open(self.data_path, 'r') as f_read:
self.persona_topic_data = json.load(f_read)

def _compile_data(self) -> List[List[dict]]:
def _get_datafile(self, opt) -> str:
"""
Compile data to be saved for faster future use.
Specify a custom datafile path for examples with personas.
"""
warn_once(f'Starting to compile {self.num_episodes():d} episodes.')
all_data = []
for episode_idx in tqdm(range(self.num_episodes())):
episode_data = []
entry_idx = 0
while True:
example_data = self._get_example(
episode_idx=episode_idx, entry_idx=entry_idx
)
episode_data.append(example_data)
if example_data['episode_done']:
all_data.append(episode_data)
break
else:
entry_idx += 1

return all_data

def _get_example(self, episode_idx: int, entry_idx: Optional[int] = None):
"""
Get example from the base ED teacher and add persona and WoW topic strings.
"""
gotten = super().get(episode_idx, entry_idx=entry_idx)
if entry_idx == 0:
modified_text = self.persona_topicifier.get_modified_text(gotten['text'])
gotten['text'] = modified_text
return gotten
experiencer_side_only = self._get_experiencer_side_only(opt)
return _cached_data_path(opt=opt, experiencer_side_only=experiencer_side_only)

def get(self, episode_idx: int, entry_idx: Optional[int] = None) -> dict:
def setup_data(self, path):
"""
Get example from the final data with personas and WoW topic strings.
"""
if entry_idx is None:
entry_idx = 0
return self.persona_topic_data[episode_idx][entry_idx]

warn_once(f'Loading cached data from {path}.')
with PathManager.open(path, 'r') as f_read:
persona_topic_data = json.load(f_read)

for episode in persona_topic_data:
for entry_idx, entry in enumerate(episode):

# For compatibility with DialogTeacher
del entry['episode_done']
if self._get_base_datatype(self.opt) == 'train':
del entry['label_candidates']

new_episode = entry_idx == 0
yield entry, new_episode


class PersonaTopicifier:
Expand Down
Expand Up @@ -5,7 +5,7 @@ acts:
episode_done: false
eval_labels:
- Did you suffer any injuries?
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- I hope it goes well! If it makes you feel any better, most of them are probably
just as nervous and are looking for any excuse to relax and let their guard
Expand Down Expand Up @@ -62,7 +62,7 @@ acts:
episode_done: true
eval_labels:
- Why did you feel guilty? People really shouldn't drive drunk.
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- Yep! or if you're house will be hit. Many possibilities with tornadoes!
- 'Wow! I better! I Stopped at Associates of General Studies, because I wanted
Expand Down Expand Up @@ -101,7 +101,7 @@ acts:
eval_labels:
- Yeah i wanted to tell you about the time i was hit by a drunk driver im so happy
to still be alive after that experience
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- Sugar will do that to kids every time. Just don't let them on a roller coaster
to make it even worse.
Expand Down Expand Up @@ -150,7 +150,7 @@ acts:
episode_done: true
eval_labels:
- Yeah he was punished hes in jail still
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- Are you really able to work from home? Finding a gig is so difficult, I'm glad
that it is working for you.
Expand Down Expand Up @@ -194,7 +194,7 @@ acts:
eval_labels:
- sorry to hear! do you have any idea about the break up? did you think about
it ?
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- That is insane! I've never heard of anything like that before. You all should
start protesting!
Expand Down
Expand Up @@ -3,8 +3,7 @@ acts:
deepmoji_ctx: null
emotion: sentimental
episode_done: false
id: blended_skill_talk:e_d_persona_topicifier
label_candidates: []
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
labels:
- Was this a friend you were in love with, or just a best friend?
prepend_cand: null
Expand All @@ -28,8 +27,7 @@ acts:
deepmoji_ctx: null
emotion: sentimental
episode_done: false
id: blended_skill_talk:e_d_persona_topicifier
label_candidates: []
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
labels:
- Where has she gone?
prepend_cand: null
Expand All @@ -41,8 +39,7 @@ acts:
deepmoji_ctx: null
emotion: sentimental
episode_done: true
id: blended_skill_talk:e_d_persona_topicifier
label_candidates: []
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
labels:
- Oh was this something that happened because of an argument?
prepend_cand: null
Expand All @@ -54,8 +51,7 @@ acts:
deepmoji_ctx: null
emotion: sentimental
episode_done: false
id: blended_skill_talk:e_d_persona_topicifier
label_candidates: []
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
labels:
- This was a best friend. I miss her.
prepend_cand: null
Expand All @@ -77,8 +73,7 @@ acts:
deepmoji_ctx: null
emotion: sentimental
episode_done: true
id: blended_skill_talk:e_d_persona_topicifier
label_candidates: []
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
labels:
- We no longer talk.
prepend_cand: null
Expand Down
Expand Up @@ -5,7 +5,7 @@ acts:
episode_done: false
eval_labels:
- Are you fine now?
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- It's amazing how many feelings and memories mementos like that can bring back.
- Good for you! There is nothing wrong or uncool about short hair.
Expand Down Expand Up @@ -54,7 +54,7 @@ acts:
episode_done: true
eval_labels:
- Cool :) Is your car damaged a lot?
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- I've actually never gone there. What's it like?
- College is when the tear will start to flow. I love children.
Expand Down Expand Up @@ -92,7 +92,7 @@ acts:
episode_done: false
eval_labels:
- That's funny, hope he didn't give you a heart attack.
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- o no what did you do?
- I definitely recommend getting some mace and a pocket knife. Stay safe out there!
Expand Down Expand Up @@ -146,7 +146,7 @@ acts:
episode_done: true
eval_labels:
- I would probably scream also.
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- 'That really does make it special. I''m glad you have that. '
- It must've have been. Glad they are okay now.
Expand Down Expand Up @@ -184,7 +184,7 @@ acts:
episode_done: false
eval_labels:
- 'Wow! That sounds amazing. Where are you going? '
id: blended_skill_talk:e_d_persona_topicifier
id: parlai.tasks.blended_skill_talk.agents:EDPersonaTopicifierTeacher
label_candidates:
- What kind of school was she at before?
- Yes, I hate those kind of people. They have nothing to do expect lying about
Expand Down
10 changes: 0 additions & 10 deletions parlai/tasks/empathetic_dialogues/README.md
Expand Up @@ -14,15 +14,5 @@ Returns examples like so:
with additional task specific fields:
- [situation]: a 1-3 sentence description of the situation that the conversation is
- [emotion]: one of 32 emotion words
Other optional fields:
- [prepend_ctx]: fasttext prediction on context line - or None
- [prepend_cand]: fasttext prediction on label line (candidate) - or None
- [deepmoji_ctx]: vector encoding from deepmoji penultimate layer - or None
- [deepmoji_cand]: vector encoding from deepmoji penultimate layer for label line (candidate) - or None

## EmotionClassificationSituationTeacher
Classifier that returns the situation and emotion for each episode given by `EmpatheticDialoguesTeacher`. Examples:
- [text]: A 1-3 sentence description of the situation that the conversation is (equivalent to [situation] for `EmpatheticDialoguesTeacher`)
- [labels]: one of 32 emotion words (equivalent to [emotion] for `EmpatheticDialoguesTeacher`)

Tags: #EmpatheticDialogues, #All, #ChitChat

0 comments on commit 4f7b38e

Please sign in to comment.