Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load(path) -> Optional["TrackerFeaturizer"]:
"""Loads the featurizer from file."""
featurizer_file = os.path.join(path, "featurizer.json")
if os.path.isfile(featurizer_file):
return jsonpickle.decode(rasa.utils.io.read_file(featurizer_file))
else:
logger.error(
"Couldn't load featurizer for policy. "
"File '{}' doesn't exist.".format(featurizer_file)
)
return None
def load(
cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional[Metadata] = None,
cached_component: Optional["RegexFeaturizer"] = None,
**kwargs: Any,
) -> "RegexFeaturizer":
file_name = meta.get("file")
regex_file = os.path.join(model_dir, file_name)
if os.path.exists(regex_file):
known_patterns = rasa.utils.io.read_json_file(regex_file)
return RegexFeaturizer(meta, known_patterns=known_patterns)
else:
return RegexFeaturizer(meta)
def persist_specification(self, model_path: Text) -> None:
"""Persists the domain specification to storage."""
domain_spec_path = os.path.join(model_path, "domain.json")
rasa.utils.io.create_directory_for_file(domain_spec_path)
metadata = {"states": self.input_states}
utils.dump_obj_as_json_to_file(domain_spec_path, metadata)
def conversation_id_from_args(args: Any, kwargs: Any) -> Optional[Text]:
argnames = rasa.utils.common.arguments_of(f)
try:
sender_id_arg_idx = argnames.index("conversation_id")
if "conversation_id" in kwargs: # try to fetch from kwargs first
return kwargs["conversation_id"]
if sender_id_arg_idx < len(args):
return args[sender_id_arg_idx]
return None
except ValueError:
return None
def load_specification(cls, path: Text) -> Dict[Text, Any]:
"""Load a domains specification from a dumped model directory."""
metadata_path = os.path.join(path, "domain.json")
specification = json.loads(rasa.utils.io.read_file(metadata_path))
return specification
def persist(self, path: Text) -> None:
config_file = os.path.join(path, "botfront_handoff_policy.json")
meta = {
"service": self.service,
"realm": self.realm,
"triggers": self.triggers,
"room_prefix": self.room_prefix,
"first_responders": self.first_responders,
"announcement_room": self.announcement_room,
}
rasa.utils.io.create_directory_for_file(config_file)
rasa.utils.io.dump_obj_as_json_to_file(config_file, meta)
def persist_specification(self, model_path: Text) -> None:
"""Persists the domain specification to storage."""
domain_spec_path = os.path.join(model_path, "domain.json")
rasa.utils.io.create_directory_for_file(domain_spec_path)
metadata = {"states": self.input_states}
rasa.utils.io.dump_obj_as_json_to_file(domain_spec_path, metadata)
def load_tracker_from_json(tracker_dump: Text, domain: Domain) -> DialogueStateTracker:
"""Read the json dump from the file and instantiate a tracker it."""
tracker_json = json.loads(rasa.utils.io.read_file(tracker_dump))
sender_id = tracker_json.get("sender_id", UserMessage.DEFAULT_SENDER_ID)
return DialogueStateTracker.from_dict(
sender_id, tracker_json.get("events", []), domain.slots
)