File size: 2,402 Bytes
56b1f67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import subprocess
import threading
import time
import sys

# Set environment variables to fix permission issues
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
os.environ["RASA_USER_HOME"] = "/tmp/rasa"
os.environ["SQLALCHEMY_SILENCE_UBER_WARNING"] = "1"

# Ensure models directory exists in writable location
models_dir = "/tmp/models"
if not os.path.exists(models_dir):
    os.makedirs(models_dir, exist_ok=True)
    print(f"Created models directory at {models_dir}")

# Function to run Rasa action server
def run_action_server():
    print("Starting Rasa Action Server...")
    try:
        # Action server usually runs on port 5055
        action_process = subprocess.Popen(
            ["rasa", "run", "actions", "--port", "5055"],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT
        )
        
        # Stream output for debugging
        for line in action_process.stdout:
            sys.stdout.write(f"[Action Server] {line.decode()}")
            sys.stdout.flush()
            
    except Exception as e:
        print(f"Error starting Action Server: {e}", file=sys.stderr)

# Start the action server in a separate thread
action_thread = threading.Thread(target=run_action_server, daemon=True)
action_thread.start()

# Wait a bit to let the action server initialize
print("Waiting for Action Server to initialize...")
time.sleep(10)

# Start the main Rasa server
print("Starting Rasa Main Server...")
try:
    # The endpoint config needs to point to the action server
    cmd = [
        "rasa", 
        "run", 
        "--enable-api", 
        "--cors", "*", 
        "--port", "7860",
        "--endpoints", "endpoints.yml",  # Make sure endpoints.yml contains action_endpoint
    ]
    
    # If a model file is found, use it
    model_files = [f for f in os.listdir(".") if f.endswith(".tar.gz")]
    if model_files:
        print(f"Found model file: {model_files[0]}")
        cmd.extend(["--model", model_files[0]])
    
    print(f"Running command: {' '.join(cmd)}")
    rasa_process = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    
    # Stream output for debugging
    for line in rasa_process.stdout:
        sys.stdout.write(f"[Rasa Server] {line.decode()}")
        sys.stdout.flush()
        
except Exception as e:
    print(f"Error starting Rasa server: {e}", file=sys.stderr)