Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
]
|
| 57 |
+
|
| 58 |
+
# If a model file is found, use it
|
| 59 |
+
model_files = [f for f in os.listdir(".") if f.endswith(".tar.gz")]
|
| 60 |
+
if model_files:
|
| 61 |
+
print(f"Found model file: {model_files[0]}")
|
| 62 |
+
cmd.extend(["--model", model_files[0]])
|
| 63 |
+
|
| 64 |
+
print(f"Running command: {' '.join(cmd)}")
|
| 65 |
+
rasa_process = subprocess.Popen(
|
| 66 |
+
cmd,
|
| 67 |
+
stdout=subprocess.PIPE,
|
| 68 |
+
stderr=subprocess.STDOUT
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Stream output for debugging
|
| 72 |
+
for line in rasa_process.stdout:
|
| 73 |
+
sys.stdout.write(f"[Rasa Server] {line.decode()}")
|
| 74 |
+
sys.stdout.flush()
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Error starting Rasa server: {e}", file=sys.stderr)
|