amirulhazym
commited on
Commit
Β·
cffc7cd
1
Parent(s):
588f5da
refactor(deploy)!: Implement launcher script for decoupled deployment
Browse files- README.md +2 -0
- launcher.py +61 -0
- packages.txt +0 -1
- setup.sh +0 -35
README.md
CHANGED
|
@@ -3,6 +3,8 @@ title: AuraCart AI Assistant
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
|
|
|
|
|
|
| 6 |
python_version: 3.10
|
| 7 |
pinned: false
|
| 8 |
---
|
|
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
app_file: launcher.py
|
| 8 |
python_version: 3.10
|
| 9 |
pinned: false
|
| 10 |
---
|
launcher.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Full Code for: launcher.py
|
| 2 |
+
|
| 3 |
+
import subprocess
|
| 4 |
+
import time
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# --- Step 1: Launch the FastAPI Backend ---
|
| 8 |
+
# We use subprocess.Popen to start the backend in a new process,
|
| 9 |
+
# which runs in the background. We redirect its output to a log file.
|
| 10 |
+
|
| 11 |
+
print("--- LAUNCHER: Starting backend server... ---")
|
| 12 |
+
|
| 13 |
+
# We construct the command to run uvicorn
|
| 14 |
+
backend_command = [
|
| 15 |
+
"uvicorn",
|
| 16 |
+
"v2_multilingual_api.backend.main:app",
|
| 17 |
+
"--host", "0.0.0.0",
|
| 18 |
+
"--port", "8000"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
# Open log files for the backend's output and errors
|
| 22 |
+
backend_log = open("backend.log", "w")
|
| 23 |
+
|
| 24 |
+
# Start the backend process
|
| 25 |
+
backend_process = subprocess.Popen(backend_command, stdout=backend_log, stderr=backend_log)
|
| 26 |
+
|
| 27 |
+
print(f"--- LAUNCHER: Backend process started with PID: {backend_process.pid} ---")
|
| 28 |
+
print("--- LAUNCHER: Waiting 20 seconds for backend to initialize models... ---")
|
| 29 |
+
time.sleep(20)
|
| 30 |
+
|
| 31 |
+
# --- Step 2: Health Check for the Backend ---
|
| 32 |
+
# Check if the process is still running. If poll() returns a number, it means it crashed.
|
| 33 |
+
if backend_process.poll() is not None:
|
| 34 |
+
print("--- LAUNCHER: !!! BACKEND FAILED TO START !!! ---")
|
| 35 |
+
# Read the log file to find out why it failed
|
| 36 |
+
backend_log.close()
|
| 37 |
+
with open("backend.log", "r") as f:
|
| 38 |
+
print(f.read())
|
| 39 |
+
# Exit the launcher with an error code to make the build fail clearly
|
| 40 |
+
exit(1)
|
| 41 |
+
else:
|
| 42 |
+
print("--- LAUNCHER: Backend health check passed. Process is running. ---")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# --- Step 3: Launch the Streamlit Frontend ---
|
| 46 |
+
# This command will run in the foreground and take over the main process.
|
| 47 |
+
# This is what the Hugging Face Spaces platform expects.
|
| 48 |
+
|
| 49 |
+
print("--- LAUNCHER: Starting frontend server... ---")
|
| 50 |
+
|
| 51 |
+
# We use os.system for simplicity to run the final command
|
| 52 |
+
frontend_command = (
|
| 53 |
+
"streamlit run v2_multilingual_api/frontend/app.py "
|
| 54 |
+
"--server.port 7860 "
|
| 55 |
+
"--server.address 0.0.0.0"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
os.system(frontend_command)
|
| 59 |
+
|
| 60 |
+
# Close the log file when the app is shut down
|
| 61 |
+
backend_log.close()
|
packages.txt
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
procps
|
|
|
|
|
|
setup.sh
DELETED
|
@@ -1,35 +0,0 @@
|
|
| 1 |
-
#!/bin/bash
|
| 2 |
-
|
| 3 |
-
# -- Make the script "fail loud" --
|
| 4 |
-
# This command ensures that if any single command fails, the whole script exits immediately.
|
| 5 |
-
set -e
|
| 6 |
-
|
| 7 |
-
# -- Add Execute Permissions --
|
| 8 |
-
# This ensures the server can run this file as a script.
|
| 9 |
-
chmod +x setup.sh
|
| 10 |
-
|
| 11 |
-
echo "--- setup.sh script has started ---"
|
| 12 |
-
|
| 13 |
-
# --- Start the Backend with Logging ---
|
| 14 |
-
echo "Attempting to start backend server..."
|
| 15 |
-
uvicorn v2_multilingual_api.backend.main:app --host 0.0.0.0 --port 8000 > backend.log 2>&1 &
|
| 16 |
-
|
| 17 |
-
echo "Waiting for backend to initialize (20 seconds)..."
|
| 18 |
-
sleep 20
|
| 19 |
-
|
| 20 |
-
# --- Health Check ---
|
| 21 |
-
echo "Performing health check on the backend..."
|
| 22 |
-
if ! pgrep -f "uvicorn v2_multilingual_api.backend.main:app"; then
|
| 23 |
-
echo "--- !!! BACKEND FAILED TO START !!! ---"
|
| 24 |
-
echo "--- Displaying contents of backend.log: ---"
|
| 25 |
-
cat backend.log
|
| 26 |
-
exit 1
|
| 27 |
-
else
|
| 28 |
-
echo "Backend health check PASSED. Process is running."
|
| 29 |
-
fi
|
| 30 |
-
|
| 31 |
-
# --- Launch the Frontend ---
|
| 32 |
-
echo "Starting frontend server..."
|
| 33 |
-
streamlit run v2_multilingual_api.frontend/app.py --server.port 7860 --server.address 0.0.0.0
|
| 34 |
-
|
| 35 |
-
echo "--- setup.sh script has finished ---"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|