Esempio n. 1
0
    def __init__(
        self,
        agent,
        ttad_model_path,
        ttad_embeddings_path,
        ttad_grammar_path,
        no_ground_truth_actions=False,
    ):
        super(TtadModelDialogueManager, self).__init__(agent, None)

        # the following are still scripted and are handled directly from here
        self.botCapabilityQuery = [
            "what can you do",
            "what else can you do",
            "what do you know",
            "tell me what you can do",
            "what things can you do",
            "what are your capabilities",
            "show me what you can do",
            "what are you capable of",
        ]
        self.botGreetings = ["hi", "hello", "hey"]

        logging.info("using ttad_model_path={}".format(ttad_model_path))
        # Instantiate the TTAD model
        if ttad_model_path:
            self.ttad_model = ActionDictBuilder(
                ttad_model_path,
                embeddings_path=ttad_embeddings_path,
                action_tree_path=ttad_grammar_path,
            )
        self.debug_mode = False

        # ground_truth_data is the ground truth action dict from templated
        # generations and will be queried first if checked in.
        self.ground_truth_actions = {}
        if not no_ground_truth_actions:
            ground_truth_file = os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
                "ground_truth_data.json")
            if os.path.isfile(ground_truth_file):
                with open(ground_truth_file) as f:
                    self.ground_truth_actions = json.load(f)

        self.dialogue_object_parameters = {
            "agent": self.agent,
            "memory": self.agent.memory,
            "dialogue_stack": self.dialogue_stack,
        }
    def __init__(self, agent, dialogue_object_classes, opts, no_ground_truth_actions=False):
        super(NSPDialogueManager, self).__init__(agent, None)
        # "dialogue_object_classes" should be a dict with keys
        # interpeter, get_memory, and put_memory;
        # the values are the corresponding classes
        self.dialogue_objects = dialogue_object_classes
        self.QA_model = None
        # the following are still scripted and are handled directly from here
        self.botCapabilityQuery = [
            "what can you do",
            "what else can you do",
            "what do you know",
            "tell me what you can do",
            "what things can you do",
            "what are your capabilities",
            "show me what you can do",
            "what are you capable of",
            "help me",
            "help",
            "do something",
        ]
        self.botGreetings = ["hi", "hello", "hey"]
        logging.info("using QA_model_path={}".format(opts.QA_nsp_model_path))
        logging.info("using model_dir={}".format(opts.nsp_model_dir))

        # Instantiate the QA model
        if opts.QA_nsp_model_path:
            from ttad.ttad_model.ttad_model_wrapper import ActionDictBuilder

            self.QA_model = ActionDictBuilder(
                opts.QA_nsp_model_path,
                embeddings_path=opts.nsp_embeddings_path,
                action_tree_path=opts.nsp_grammar_path,
            )

        # Instantiate the main model
        if opts.nsp_data_dir is not None:
            from ttad.ttad_transformer_model.query_model import TTADBertModel as Model

            self.model = Model(model_dir=opts.nsp_model_dir, data_dir=opts.nsp_data_dir)
        self.debug_mode = False

        # ground_truth_data is the ground truth action dict from templated
        # generations and will be queried first if checked in.
        self.ground_truth_actions = {}
        if not no_ground_truth_actions:
            if os.path.isfile(opts.ground_truth_file_path):
                with open(opts.ground_truth_file_path) as f:
                    for line in f.readlines():
                        text, logical_form = line.strip().split("\t")
                        self.ground_truth_actions[text] = json.loads(logical_form)

        self.dialogue_object_parameters = {
            "agent": self.agent,
            "memory": self.agent.memory,
            "dialogue_stack": self.dialogue_stack,
        }
Esempio n. 3
0
class TtadModelDialogueManager(DialogueManager):
    def __init__(
        self,
        agent,
        ttad_model_path,
        ttad_embeddings_path,
        ttad_grammar_path,
        no_ground_truth_actions=False,
    ):
        super(TtadModelDialogueManager, self).__init__(agent, None)

        # the following are still scripted and are handled directly from here
        self.botCapabilityQuery = [
            "what can you do",
            "what else can you do",
            "what do you know",
            "tell me what you can do",
            "what things can you do",
            "what are your capabilities",
            "show me what you can do",
            "what are you capable of",
        ]
        self.botGreetings = ["hi", "hello", "hey"]

        logging.info("using ttad_model_path={}".format(ttad_model_path))
        # Instantiate the TTAD model
        if ttad_model_path:
            self.ttad_model = ActionDictBuilder(
                ttad_model_path,
                embeddings_path=ttad_embeddings_path,
                action_tree_path=ttad_grammar_path,
            )
        self.debug_mode = False

        # ground_truth_data is the ground truth action dict from templated
        # generations and will be queried first if checked in.
        if not no_ground_truth_actions and os.path.isfile(
                "ground_truth_data.json"):
            with open("ground_truth_data.json") as f:
                self.ground_truth_actions = json.load(f)
        else:
            self.ground_truth_actions = {}

        self.dialogue_object_parameters = {
            "agent": self.agent,
            "memory": self.agent.memory,
            "dialogue_stack": self.dialogue_stack,
        }

    def run_model(self, chat: Tuple[str, str]) -> Optional[DialogueObject]:
        """Process a chat and maybe modify the dialogue stack"""

        if chat[1] == "ipdb":
            ipdb.set_trace()

        if len(self.dialogue_stack
               ) > 0 and self.dialogue_stack[-1].awaiting_response:
            return None

        # chat is a single line command
        speaker, chatstr = chat
        preprocessed_chatstrs = preprocess.preprocess_chat(chatstr)

        # Push appropriate DialogueObjects to stack if incomign chat
        # is one of the scripted ones
        if any([
                chat in self.botCapabilityQuery
                for chat in preprocessed_chatstrs
        ]):
            return BotCapabilities(**self.dialogue_object_parameters)
        if any([chat in self.botGreetings for chat in preprocessed_chatstrs]):
            return BotGreet(**self.dialogue_object_parameters)
        if any(["debug_remove" in chat for chat in preprocessed_chatstrs]):
            return BotVisionDebug(**self.dialogue_object_parameters)

        # Assume non-compound command for now
        action_dict = self.ttad(preprocessed_chatstrs[0])
        return self.handle_action_dict(speaker, action_dict)

    def handle_action_dict(self, speaker: str,
                           d: Dict) -> Optional[DialogueObject]:
        """Return the appropriate DialogueObject to handle an action dict "d"

        "d" should have spans resolved by corefs not yet resolved to a specific
        MemoryObject
        """
        coref_resolve(self.agent.memory, d)
        logging.info('ttad post-coref "{}" -> {}'.format(speaker, d))

        if d["dialogue_type"] == "NOOP":
            return Say("I don't know how to answer that.",
                       **self.dialogue_object_parameters)
        elif d["dialogue_type"] == "HUMAN_GIVE_COMMAND":
            return Interpreter(speaker, d, **self.dialogue_object_parameters)
        elif d["dialogue_type"] == "PUT_MEMORY":
            return PutMemoryHandler(speaker, d,
                                    **self.dialogue_object_parameters)
        elif d["dialogue_type"] == "GET_MEMORY":
            return GetMemoryHandler(speaker, d,
                                    **self.dialogue_object_parameters)
        else:
            raise ValueError("Bad dialogue_type={}".format(d["dialogue_type"]))

    def ttad(self, s: str) -> Dict:
        """Query TTAD model to get the action dict"""

        if s in self.ground_truth_actions:
            d = self.ground_truth_actions[s]
            logging.info('Found gt action for "{}"'.format(s))
        else:
            d = self.ttad_model.parse([s])

        # Get the words from indices in spans
        process_spans(d, re.split(r" +", s))
        logging.info('ttad pre-coref "{}" -> {}'.format(s, d))

        # log to sentry
        sentry_sdk.capture_message(
            json.dumps({
                "type": "ttad_pre_coref",
                "in": s,
                "out": d
            }))
        return d
    def __init__(self, agent, dialogue_object_classes, opts):
        super(NSPDialogueManager, self).__init__(agent, None)
        # "dialogue_object_classes" should be a dict with keys
        # interpeter, get_memory, and put_memory;
        # the values are the corresponding classes
        self.dialogue_objects = dialogue_object_classes
        self.QA_model = None
        # the following are still scripted and are handled directly from here
        self.botCapabilityQuery = [
            "what can you do",
            "what else can you do",
            "what do you know",
            "tell me what you can do",
            "what things can you do",
            "what are your capabilities",
            "show me what you can do",
            "what are you capable of",
            "help me",
            "help",
            "do something",
        ]
        safety_words_path = opts.ground_truth_data_dir + "safety.txt"
        if os.path.isfile(safety_words_path):
            self.safety_words = self.get_safety_words(safety_words_path)
        else:
            self.safety_words = []
        # Load bot greetings
        greetings_path = opts.ground_truth_data_dir + "greetings.json"
        if os.path.isfile(greetings_path):
            self.botGreetings = json.load(open(greetings_path))
        else:
            self.botGreetings = {
                "hello": ["hi", "hello", "hey"],
                "goodbye": ["bye"]
            }
        logging.info("using QA_model_path={}".format(opts.QA_nsp_model_path))
        logging.info("using model_dir={}".format(opts.nsp_model_dir))

        # Instantiate the QA model
        if opts.QA_nsp_model_path:
            from ttad.ttad_model.ttad_model_wrapper import ActionDictBuilder

            self.QA_model = ActionDictBuilder(
                opts.QA_nsp_model_path,
                embeddings_path=opts.nsp_embeddings_path,
                action_tree_path=opts.nsp_grammar_path,
            )

        # Instantiate the main model
        if opts.nsp_data_dir is not None:
            from ttad.ttad_transformer_model.query_model import TTADBertModel as Model

            self.model = Model(model_dir=opts.nsp_model_dir,
                               data_dir=opts.nsp_data_dir)
        self.debug_mode = False

        # if web_app option is enabled
        self.webapp_dict = {}
        self.web_app = opts.web_app
        if self.web_app:
            logging.info("web_app flag has been enabled")
            logging.info("writing to file: %r " % (web_app_filename))
            # os.system("python ./python/craftassist/web_app_socket.py &")

        # ground_truth_data is the ground truth action dict from templated
        # generations and will be queried first if checked in.
        self.ground_truth_actions = {}
        if not opts.no_ground_truth:
            if os.path.isdir(opts.ground_truth_data_dir):
                files = glob(opts.ground_truth_data_dir + "datasets/*.txt")
                for dataset in files:
                    with open(dataset) as f:
                        for line in f.readlines():
                            text, logical_form = line.strip().split("|")
                            clean_text = text.strip('"')
                            self.ground_truth_actions[clean_text] = json.loads(
                                logical_form)

        self.dialogue_object_parameters = {
            "agent": self.agent,
            "memory": self.agent.memory,
            "dialogue_stack": self.dialogue_stack,
        }

        @sio.on("queryParser")
        def query_parser(sid, data):
            logging.info("inside query parser.....")
            logging.info(data)
            x = self.get_logical_form(s=data["chat"], model=self.model)
            logging.info(x)
            payload = {"action_dict": x}
            sio.emit("render_parser_output", payload)
Esempio n. 5
0
"""
Copyright (c) Facebook, Inc. and its affiliates.

This is a utility script which allows the user to easily query the ttad model

At the prompt, try typing "build me a cube"
"""

import faulthandler
import fileinput
import json
import signal

from ttad.ttad_model.ttad_model_wrapper import ActionDictBuilder

faulthandler.register(signal.SIGUSR1)

if __name__ == "__main__":
    print("Loading...")

    # ttad_model_path = os.path.join(os.path.dirname(__file__), "models/ttad/ttad.pth")
    # ttad_embedding_path = os.path.join(os.path.dirname(__file__), "models/ttad/ttad_ft_embeds.pth")
    # ttad_model = ActionDictBuilder(ttad_model_path, ttad_embedding_path)
    ttad_model = ActionDictBuilder()

    print("> ", end="", flush=True)
    for line in fileinput.input():
        action_dict = ttad_model.parse([line.strip()])
        print(json.dumps(action_dict))
        print("\n> ", end="", flush=True)