I needed to detect if a model can be trained incrementally or not automatically when a change in the chatbot knowledge data is made. After to search how to do that, I released it is not documented anywhere, nor in this forum. Therefore, I want to share my solution:
def can_fine_tune(old_model: Union[str, PathLike, bytes], new_model: Union[str, PathLike, bytes]) -> bool:
""" Check when a RASA model can be fine-tuned instead of retrain all the model.
:param old_model: The path to the previous tar.gz trained model file.
:param new_model: The path to the directory where the new model is prepared to be trained.
:return: True if it is possible to do a incremental learning, otherwise False.
"""
import asyncio
from rasa.shared.importers.importer import TrainingDataImporter
from rasa.model_training import model
async def _can_fine_tune(old_model: Union[str, PathLike, bytes], new_model: Union[str, PathLike, bytes]) -> bool:
config, domain, nlu = join(new_model, 'config.yml'), join(new_model, 'domain.yml'), join(new_model, 'nlu.yml')
file_importer = TrainingDataImporter.load_from_config(config, domain, [nlu])
with model.unpack_model(old_model) as unpacked:
old_fingerprint = model.fingerprint_from_path(unpacked)
new_fingerprint = await model.model_fingerprint(file_importer)
try:
return model.can_finetune(old_fingerprint, new_fingerprint, core=True, nlu=True)
except TypeError:
logger.warning(f'The data stored in "{old_model}" is not a valid model.')
return False
return asyncio.run(_can_fine_tune(old_model, new_model))
