How to run training and replace current RASA model from python script

Hi all, I’m currently using RASA 2.8.1. My project require me to call rasa train from the python script and then replace the currently used model with the newly trained one. Previously, I have used subprocess to call this line from a script

rasa train --domain data

I have 2 problems:

  1. Are there any way to call rasa train directly from the python code without going through bash?
  2. After training, how can I use the new model without restarting rasa server?

Thank you all.

Hello :slight_smile:

Sadly, the answer to the first question is no.

As for the second one, what’s your deployment method?

Hello, I deploy rasa on a remote server by calling rasa run.

So you can also execute it with Python subprocess, or with the HTTP API

So I can just kill the current server then use subprocess to run it?

I think this would be enough, hopefully it works :slight_smile:

I use a custom channel in rasa to receive an api which signal training new model and then replacing it. If I kill, train and then start the model again, then is there any problems that can happen with the ongoing conversations?

The ongoing conversations will be stopped since the server will be restarting, but that’s probably just it

Thank you, it seems like I need to change my approach to the problem if we need to restart in other to unload and replace the current model

1 Like

Don’t hesitate to ask if you have any issues :slight_smile:

Yeah, because I found the train/unload/replace trained model in the HTTP api page so I figured that there are ways to do it in python code. Can you show me where I can find the code for the HTTP API in RASA github? https://rasa.com/docs/rasa/pages/http-api#operation/unloadModel

Here :slight_smile:

1 Like

Thank you so much. It’s exactly what I need. I have accepted your Answer as the Solution.

1 Like

Thanks, glad to be of help :slight_smile:

So after looking through the code you gave me. I have figured out my problems. I have trained the data using:

from rasa.model_training import train
training_result = train(
    domain="directory-of-domain", config="config.yml", training_files="directory-of-nlu-files")

I have unload and replace the model by sending requests to rasa server using requests.

import requests
requests.delete('server/model')
requests.put('server/model', {"model_file":"absolute path to model file"}

This way I doesn’t have to kill and start the server when I change the models

1 Like

Yes you’re right!

Sorry, I don’t know why I previously talked as if the server needed to be restarted!

Here I should’ve said “The ongoing conversations will be stopped while the model is being changed, but that’s probably just it”

can we write a separate python script and run this code? will it be able to unload previous models and place a new model?

I want to do this job through jenkins, i.e train the new model and replace it with current one, not sure how to do it

I imagine you could. I’m investigating this now for a project in my company using MLFlow for versioning and deployment.

I got the model path with model_path = training_results.model

and then used that like this:

requests.delete('server/model')
requests.put('server/model', {"model_file":"/rasa/mlflow-rasa/{}".format(model_path)})

I’ll let you know how it goes

thanks for reply @jonathanpwheat, I’m successfully able to delete the current model on server by hitting api but unable to put or replace model when server is running, it will be really helpful if you find the answer

I’m getting the same result - doesn’t swap out without a restart I’ll keep digging and will let you know if I discover anything