nsomabalint commited on
Commit
34211d2
·
verified ·
1 Parent(s): 3702d32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -2
app.py CHANGED
@@ -1,9 +1,78 @@
1
  import os
2
  import subprocess
 
3
  import time
 
4
 
 
 
5
  os.environ["RASA_USER_HOME"] = "/tmp/rasa"
6
  os.environ["SQLALCHEMY_SILENCE_UBER_WARNING"] = "1"
7
 
8
- print("Starting Rasa server...")
9
- subprocess.run(["rasa", "run", "--enable-api", "--cors", "*", "--port", "7860"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import subprocess
3
+ import threading
4
  import time
5
+ import sys
6
 
7
+ # Set environment variables to fix permission issues
8
+ os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
9
  os.environ["RASA_USER_HOME"] = "/tmp/rasa"
10
  os.environ["SQLALCHEMY_SILENCE_UBER_WARNING"] = "1"
11
 
12
+ # Ensure models directory exists in writable location
13
+ models_dir = "/tmp/models"
14
+ if not os.path.exists(models_dir):
15
+ os.makedirs(models_dir, exist_ok=True)
16
+ print(f"Created models directory at {models_dir}")
17
+
18
+ # Function to run Rasa action server
19
+ def run_action_server():
20
+ print("Starting Rasa Action Server...")
21
+ try:
22
+ # Action server usually runs on port 5055
23
+ action_process = subprocess.Popen(
24
+ ["rasa", "run", "actions", "--port", "5055"],
25
+ stdout=subprocess.PIPE,
26
+ stderr=subprocess.STDOUT
27
+ )
28
+
29
+ # Stream output for debugging
30
+ for line in action_process.stdout:
31
+ sys.stdout.write(f"[Action Server] {line.decode()}")
32
+ sys.stdout.flush()
33
+
34
+ except Exception as e:
35
+ print(f"Error starting Action Server: {e}", file=sys.stderr)
36
+
37
+ # Start the action server in a separate thread
38
+ action_thread = threading.Thread(target=run_action_server, daemon=True)
39
+ action_thread.start()
40
+
41
+ # Wait a bit to let the action server initialize
42
+ print("Waiting for Action Server to initialize...")
43
+ time.sleep(10)
44
+
45
+ # Start the main Rasa server
46
+ print("Starting Rasa Main Server...")
47
+ try:
48
+ # The endpoint config needs to point to the action server
49
+ cmd = [
50
+ "rasa",
51
+ "run",
52
+ "--enable-api",
53
+ "--cors", "*",
54
+ "--port", "7860",
55
+ "--endpoints", "endpoints.yml", # Make sure endpoints.yml contains action_endpoint
56
+ "--models", models_dir
57
+ ]
58
+
59
+ # If a model file is found, use it
60
+ model_files = [f for f in os.listdir(".") if f.endswith(".tar.gz")]
61
+ if model_files:
62
+ print(f"Found model file: {model_files[0]}")
63
+ cmd.extend(["--model", model_files[0]])
64
+
65
+ print(f"Running command: {' '.join(cmd)}")
66
+ rasa_process = subprocess.Popen(
67
+ cmd,
68
+ stdout=subprocess.PIPE,
69
+ stderr=subprocess.STDOUT
70
+ )
71
+
72
+ # Stream output for debugging
73
+ for line in rasa_process.stdout:
74
+ sys.stdout.write(f"[Rasa Server] {line.decode()}")
75
+ sys.stdout.flush()
76
+
77
+ except Exception as e:
78
+ print(f"Error starting Rasa server: {e}", file=sys.stderr)