From 3cc6a7989122107b4ddfaaec1ecd32582e4d40e3 Mon Sep 17 00:00:00 2001 From: Megan Ung <20617868+meganung@users.noreply.github.com> Date: Thu, 25 May 2023 10:34:15 -0700 Subject: [PATCH] Reframing task (#5039) * reframe thoughts task and dataset * project readme * removed unused imports * add to task list --- parlai/tasks/reframe_thoughts/__init__.py | 5 ++ parlai/tasks/reframe_thoughts/agents.py | 54 ++++++++++++++ parlai/tasks/reframe_thoughts/build.py | 40 +++++++++++ parlai/tasks/reframe_thoughts/test.py | 11 +++ .../test/reframe_thoughts_test.yml | 70 +++++++++++++++++++ .../test/reframe_thoughts_train.yml | 55 +++++++++++++++ .../test/reframe_thoughts_valid.yml | 53 ++++++++++++++ parlai/tasks/task_list.py | 12 ++++ projects/reframe_thoughts/README.md | 17 +++++ 9 files changed, 317 insertions(+) create mode 100644 parlai/tasks/reframe_thoughts/__init__.py create mode 100644 parlai/tasks/reframe_thoughts/agents.py create mode 100644 parlai/tasks/reframe_thoughts/build.py create mode 100644 parlai/tasks/reframe_thoughts/test.py create mode 100644 parlai/tasks/reframe_thoughts/test/reframe_thoughts_test.yml create mode 100644 parlai/tasks/reframe_thoughts/test/reframe_thoughts_train.yml create mode 100644 parlai/tasks/reframe_thoughts/test/reframe_thoughts_valid.yml create mode 100644 projects/reframe_thoughts/README.md diff --git a/parlai/tasks/reframe_thoughts/__init__.py b/parlai/tasks/reframe_thoughts/__init__.py new file mode 100644 index 00000000000..240697e3247 --- /dev/null +++ b/parlai/tasks/reframe_thoughts/__init__.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +# Copyright (c) Facebook, Inc. and its affiliates. +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. diff --git a/parlai/tasks/reframe_thoughts/agents.py b/parlai/tasks/reframe_thoughts/agents.py new file mode 100644 index 00000000000..493c5434c33 --- /dev/null +++ b/parlai/tasks/reframe_thoughts/agents.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +# Copyright (c) Facebook, Inc. and its affiliates. +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +import os +import copy +import json +from parlai.core.teachers import DialogTeacher +from .build import build + + +def _path(opt): + # Build the data if it doesn't exist. + build(opt) + dt = opt['datatype'].split(':')[0] + return os.path.join( + opt['datapath'], 'reframe_thoughts', 'reframe_thoughts_dataset', dt + '.txt' + ) + + +class ReframeThoughtsTeacher(DialogTeacher): + def __init__(self, opt, shared=None): + opt = copy.deepcopy(opt) + opt['task'] = "reframe_thoughts" + opt['datafile'] = _path(opt) + super().__init__(opt, shared) + + def setup_data(self, datafile): + all_data = [json.loads(line.strip()) for line in open(datafile)] + for data in all_data: + persona = data["persona"] + pattern = data["pattern"] + " (" + data["pattern_def"] + ")" + thought = data["thought"] + persona_token = "" + pattern_token = "[PAT]" + thought_token = "[THT]" + for reframe in data["reframes"]: + input = " ".join( + [ + persona_token, + persona, + pattern_token, + pattern, + thought_token, + thought, + ] + ).strip() + yield (input, reframe['reframe']), True + + +class DefaultTeacher(ReframeThoughtsTeacher): + pass diff --git a/parlai/tasks/reframe_thoughts/build.py b/parlai/tasks/reframe_thoughts/build.py new file mode 100644 index 00000000000..7de3446ed5e --- /dev/null +++ b/parlai/tasks/reframe_thoughts/build.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Copyright (c) Facebook, Inc. and its affiliates. +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Download and build the data if it does not exist. + +from parlai.core.build_data import DownloadableFile +import parlai.core.build_data as build_data +import os + +REFRAME_VERSION = "v0.1" + +RESOURCES = [ + DownloadableFile( + f'http://parl.ai/downloads/reframe_thoughts/reframe_thoughts_{REFRAME_VERSION}.tar.gz', + f'reframe_thoughts_{REFRAME_VERSION}.tar.gz', + 'bfbfc61c26341dd64b59945c3d290caba67fa2db435fb01ac309cef295222c99', + ) +] + + +def build(opt): + dpath = os.path.join(opt['datapath'], 'reframe_thoughts') + version = REFRAME_VERSION + + if not build_data.built(dpath, version_string=version): + print('[building data: ' + dpath + ']') + if build_data.built(dpath): + # An older version exists, so remove these outdated files. + build_data.remove_dir(dpath) + build_data.make_dir(dpath) + + # Download the data. + for downloadable_file in RESOURCES: + downloadable_file.download_file(dpath) + + # Mark the data as built. + build_data.mark_done(dpath, version_string=version) diff --git a/parlai/tasks/reframe_thoughts/test.py b/parlai/tasks/reframe_thoughts/test.py new file mode 100644 index 00000000000..bd7b1cf7e45 --- /dev/null +++ b/parlai/tasks/reframe_thoughts/test.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +# Copyright (c) Facebook, Inc. and its affiliates. +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from parlai.utils.testing import AutoTeacherTest # noqa: F401 + + +class TestDefaultTeacher(AutoTeacherTest): + task = 'reframe_thoughts' diff --git a/parlai/tasks/reframe_thoughts/test/reframe_thoughts_test.yml b/parlai/tasks/reframe_thoughts/test/reframe_thoughts_test.yml new file mode 100644 index 00000000000..653f18a6cb8 --- /dev/null +++ b/parlai/tasks/reframe_thoughts/test/reframe_thoughts_test.yml @@ -0,0 +1,70 @@ +acts: +- - episode_done: true + eval_labels: + - My neighbor calls me by my first name instead of my nickname. He may think he + doesn't know me well enough to use it. I will let him know that I don't mind + if he calls me Bubblegum. + id: reframe_thoughts + text: 'i''ve found the cure for the common cold . my nickname is bubblegum . i + am plotting on taking over the world . my brother and i''ve bright red hair + . [PAT] Jumping to conclusions: mind reading (Inferring a person''s possible + or probable (usually negative) thoughts from their behavior and nonverbal communication; + taking precautions against the worst suspected case without asking the person. + + ) [THT] My neighbor calls me by my first name instead of my nickname. He probably + thinks bubblegum is a tacky nickname.' +- - episode_done: true + eval_labels: + - My neighbor calls me by my silly first name, but since it's my name he is obliged + to call me by it. + id: reframe_thoughts + text: 'i''ve found the cure for the common cold . my nickname is bubblegum . i + am plotting on taking over the world . my brother and i''ve bright red hair + . [PAT] Jumping to conclusions: mind reading (Inferring a person''s possible + or probable (usually negative) thoughts from their behavior and nonverbal communication; + taking precautions against the worst suspected case without asking the person. + + ) [THT] My neighbor calls me by my first name instead of my nickname. He probably + thinks bubblegum is a tacky nickname.' +- - episode_done: true + eval_labels: + - My neighbor calls me by my first name instead of my nickname. He is such a formal + gentleman. + id: reframe_thoughts + text: 'i''ve found the cure for the common cold . my nickname is bubblegum . i + am plotting on taking over the world . my brother and i''ve bright red hair + . [PAT] Jumping to conclusions: mind reading (Inferring a person''s possible + or probable (usually negative) thoughts from their behavior and nonverbal communication; + taking precautions against the worst suspected case without asking the person. + + ) [THT] My neighbor calls me by my first name instead of my nickname. He probably + thinks bubblegum is a tacky nickname.' +- - episode_done: true + eval_labels: + - My wife recently got laid off from her job and since I'm creating my own start + up company she can come to work with me now instead of having to worry about + looking for another one. + id: reframe_thoughts + text: 'i design video games for a living . my wife recently got laid off from + her human resources job . my son plays on the local football team . i have been + on the design team for over 65 video games . i am trying to create my own startup + company in the gaming industry . [PAT] Catastrophizing (Catastrophizing: Giving + greater weight to the worst possible outcome, however unlikely, or experiencing + a situation as unbearable or impossible when it is just uncomfortable.) [THT] + My wife recently got laid off from her huyman resources job, she''ll never be + able to find another job and we will lose our house soon.' +- - episode_done: true + eval_labels: + - My wife recently got laid off from her human resources job. I'm sure she will + be able to find another job soon. + id: reframe_thoughts + text: 'i design video games for a living . my wife recently got laid off from + her human resources job . my son plays on the local football team . i have been + on the design team for over 65 video games . i am trying to create my own startup + company in the gaming industry . [PAT] Catastrophizing (Catastrophizing: Giving + greater weight to the worst possible outcome, however unlikely, or experiencing + a situation as unbearable or impossible when it is just uncomfortable.) [THT] + My wife recently got laid off from her huyman resources job, she''ll never be + able to find another job and we will lose our house soon.' +num_episodes: 18635 +num_examples: 18635 diff --git a/parlai/tasks/reframe_thoughts/test/reframe_thoughts_train.yml b/parlai/tasks/reframe_thoughts/test/reframe_thoughts_train.yml new file mode 100644 index 00000000000..dc6aacd120d --- /dev/null +++ b/parlai/tasks/reframe_thoughts/test/reframe_thoughts_train.yml @@ -0,0 +1,55 @@ +acts: +- - episode_done: true + id: reframe_thoughts + labels: + - I like my cats. I have a fun imagination, but I know it's not likely they are + plotting against me to eat me. + text: 'i have a pencil thin mustache . i have six tattoos , all of cats . my mother + is a ballet dancer . i am from baltimore md . [PAT] Catastrophizing (Catastrophizing: + Giving greater weight to the worst possible outcome, however unlikely, or experiencing + a situation as unbearable or impossible when it is just uncomfortable.) [THT] + I like my cats. I think one day they will plot against me and eat me in my sleep.' +- - episode_done: true + id: reframe_thoughts + labels: + - I like my cats. I’ll get used to living with them and will be less afraid as + time goes on. + text: 'i have a pencil thin mustache . i have six tattoos , all of cats . my mother + is a ballet dancer . i am from baltimore md . [PAT] Catastrophizing (Catastrophizing: + Giving greater weight to the worst possible outcome, however unlikely, or experiencing + a situation as unbearable or impossible when it is just uncomfortable.) [THT] + I like my cats. I think one day they will plot against me and eat me in my sleep.' +- - episode_done: true + id: reframe_thoughts + labels: + - I like my cats so I will make sure to give them all the exercise and playtime + that they need. + text: 'i have a pencil thin mustache . i have six tattoos , all of cats . my mother + is a ballet dancer . i am from baltimore md . [PAT] Catastrophizing (Catastrophizing: + Giving greater weight to the worst possible outcome, however unlikely, or experiencing + a situation as unbearable or impossible when it is just uncomfortable.) [THT] + I like my cats. I think one day they will plot against me and eat me in my sleep.' +- - episode_done: true + id: reframe_thoughts + labels: + - I'm a vegan, and the restaurant served me a dis with fish in it. I believe I + should have told them I am a vegan. + text: i like visiting art museums in my spare time . i just started dating someone + new . i m a student . i am a vegan . [PAT] Overgeneralization (Someone who overgeneralizes + makes faulty generalizations from insufficient evidence. Even if something bad + happens only once, it is expected to happen over and over again.) [THT] I'm + a vegan, and the restaurant served me a dish with fish in it. They're trying + to kill me. +- - episode_done: true + id: reframe_thoughts + labels: + - I'm vegan, and the restaurant served me a dish with a fish in it. I'll just + notify the waiter and have it sent back. + text: i like visiting art museums in my spare time . i just started dating someone + new . i m a student . i am a vegan . [PAT] Overgeneralization (Someone who overgeneralizes + makes faulty generalizations from insufficient evidence. Even if something bad + happens only once, it is expected to happen over and over again.) [THT] I'm + a vegan, and the restaurant served me a dish with fish in it. They're trying + to kill me. +num_episodes: 5249 +num_examples: 5249 diff --git a/parlai/tasks/reframe_thoughts/test/reframe_thoughts_valid.yml b/parlai/tasks/reframe_thoughts/test/reframe_thoughts_valid.yml new file mode 100644 index 00000000000..3ce05b3473a --- /dev/null +++ b/parlai/tasks/reframe_thoughts/test/reframe_thoughts_valid.yml @@ -0,0 +1,53 @@ +acts: +- - episode_done: true + eval_labels: + - I tried making my own hummus for the party. My friends will appreciate my hard + work. + id: reframe_thoughts + text: 'i love to eat pita bread with hummus . i am bipolar . i am thinking of + becoming a mother next year . i go to work early in the morning as a nurse . + i am a mother of three . [PAT] Jumping to conclusions: Fortune-telling (Predicting + outcomes (usually negative) of events.) [THT] I tried making my own hummus for + the party. My friends will hate it.' +- - episode_done: true + eval_labels: + - Working from home is convenient and allows me to spend more time with my family. + id: reframe_thoughts + text: i have a cat . i hope to move out soon . i work at home , editing . i've + depression and anxiety so i don't really go out a lot . i live at home with + my dad and brother . [PAT] Discounting the positive (Rejecting positive experiences + by insisting they "don't count" for some reason or other.) [THT] Although working + from home is convenient, my family gets tired of me being around. +- - episode_done: true + eval_labels: + - Working from home is convenient. My family probably appreciates that I have + a good job. + id: reframe_thoughts + text: i have a cat . i hope to move out soon . i work at home , editing . i've + depression and anxiety so i don't really go out a lot . i live at home with + my dad and brother . [PAT] Discounting the positive (Rejecting positive experiences + by insisting they "don't count" for some reason or other.) [THT] Although working + from home is convenient, my family gets tired of me being around. +- - episode_done: true + eval_labels: + - Although working at home is convenient, my family hopes I will start working + more at the office which would allow me to focus more. + id: reframe_thoughts + text: i have a cat . i hope to move out soon . i work at home , editing . i've + depression and anxiety so i don't really go out a lot . i live at home with + my dad and brother . [PAT] Discounting the positive (Rejecting positive experiences + by insisting they "don't count" for some reason or other.) [THT] Although working + from home is convenient, my family gets tired of me being around. +- - episode_done: true + eval_labels: + - I have been dieting for two months now, and today I had a setback because I + ate a cupcake. That's okay though, I'll go to the gym and work off that cupcake + and get stronger at the same time! + id: reframe_thoughts + text: i cannot drive . i'm on a diet . i am thirty two years old . i've been married + eight years . i'm a house wife . [PAT] Mental filtering (Filtering distortions + occur when an individual dwells only on the negative details of a situation + and filters out the positive aspects.) [THT] My diet is ruined. Sure I have + done it for 2 months now and lost weight but I ate a cupcake today. +num_episodes: 2623 +num_examples: 2623 diff --git a/parlai/tasks/task_list.py b/parlai/tasks/task_list.py index c23919978af..113487186ac 100644 --- a/parlai/tasks/task_list.py +++ b/parlai/tasks/task_list.py @@ -601,6 +601,18 @@ "description": ("Open-domain QA dataset based on Freebase triples."), "links": {"arXiv": "https://arxiv.org/abs/1506.02075"}, }, + { + "id": "ReframeUnhelpfulThoughts", + "display_name": "Reframe Unhelpful Thoughts", + "task": "reframe_thoughts", + "tags": [], + "description": ( + "Dataset of about 10k examples of thoughts containing unhelpful " + "thought patterns conditioned on a given persona, accompanied by about " + "27k positive reframes." + ), + "links": {}, + }, { "id": "SNLI", "display_name": "The Stanford Natural Language Inference (SNLI) Corpus", diff --git a/projects/reframe_thoughts/README.md b/projects/reframe_thoughts/README.md new file mode 100644 index 00000000000..65a40819f97 --- /dev/null +++ b/projects/reframe_thoughts/README.md @@ -0,0 +1,17 @@ +# Training Models to Generate, Recognize, and Reframe Unhelpful Thoughts + +Mounica Maddela, Megan Ung, Jing Xu, Andrea Madotto, Heather Foran, Y-Lan Boureau + +## Abstract + +Many cognitive approaches to well-being, such as recognizing and reframing unhelpful thoughts, have received considerable empirical support over the past decades, yet still lack truly widespread adoption in self-help format. A barrier to that adoption is a lack of adequately specific and diverse dedicated practice material. This work examines whether current language models can be leveraged to both produce a virtually unlimited quantity of practice material illustrating standard unhelpful thought patterns matching specific given contexts, and generate suitable positive reframing proposals. We propose \textsc{PatternReframe}, a novel dataset of about 10k examples of thoughts containing unhelpful thought patterns conditioned on a given persona, accompanied by about 27k positive reframes. By using this dataset to train and/or evaluate current models, we show that existing models can already be powerful tools to help generate an abundance of tailored practice material and hypotheses, with no or minimal additional model training required. + +## Paper + + + +## Data + +``` +parlai display_data -t reframe_thoughts +```