Automating running the rasa servers

I am trying to run the Rasa core and actions server automatically after training the model from a python script.

However I get this error:

Access to XMLHttpRequest at 'http://127.0.0.1:5055/webhook/' from origin 'http://127.0.0.1:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Even after adding the following into my Rasa Flask app.py :

from flask_cors import CORS

app = Flask(__name__)
cors = CORS(app)
cors = CORS(app, origins=['http://127.0.0.1:8000'])  #Django where i have the Rasa UI

So I don’t understand why I get this error only when running the commands automatically and not manually.

What could the problem be and how to fix it please ?

Thank you.

This is part of my code

I first stop running the servers then execute the commands to start the severs and generate log files to view the output.

The output looks the same as when running the commands manually.

        log_action_path = "logs/rasa_actions.log"  
        log_core_path = "logs/rasa_core.log"  

        stop_command = 'taskkill /F /IM "rasa.exe"'
        start_core_command = "rasa run -m models --enable-api --cors '*' --debug"
        start_actions_command = 'rasa run actions --cors "*" --debug'

        try:
            result = subprocess.run(stop_command, shell=True, check=True)
            print('result is =',result)
            if result.returncode == 0:
                print('Stop Command completed successfully.')
            else:
                print('Stop Command - An error occurred.')

        except subprocess.CalledProcessError:
            print("Rasa server is not running.")

        # Open a log file for appending the output
        with open(log_action_path, "a") as log_file:
            # Create a subprocess with a new process group
            process = subprocess.Popen(
                start_actions_command,
                shell=True,
                stdout=log_file,
                stderr=subprocess.STDOUT,  # Redirect stderr to stdout
                creationflags=subprocess.CREATE_NEW_CONSOLE,
            )

            if process.poll() is None:
                print('The actions process has started and is still running.')
            else:
                print('The actions process has completed.')
            
         ###### same for start_core_command ######

You may want to set the --cors option to * on the rasa run.

Yes i did already , the code is in the dropdown at the end of the post

start_core_command = "rasa run -m models --enable-api --cors '*' --debug"

It’s the same command i run manually in the terminal and get no problem, except when it is run as a subprocess.