amirulhazym
commited on
Commit
·
7e0c6a8
1
Parent(s):
061a705
fix(deploy): Prevent backend re-execution on Streamlit rerun
Browse files- launcher.py +43 -46
launcher.py
CHANGED
@@ -1,52 +1,52 @@
|
|
1 |
-
# Full Code for: launcher.py
|
2 |
|
|
|
3 |
import subprocess
|
4 |
import time
|
5 |
import os
|
6 |
|
7 |
-
# ---
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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 |
-
# ---
|
32 |
-
# Check if the process
|
33 |
-
if backend_process.poll() is not None:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
else:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
# --- Step 3: Launch the Streamlit Frontend ---
|
46 |
-
# This
|
47 |
-
#
|
48 |
|
49 |
-
print("--- LAUNCHER:
|
50 |
|
51 |
# We use os.system for simplicity to run the final command
|
52 |
frontend_command = (
|
@@ -55,7 +55,4 @@ frontend_command = (
|
|
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()
|
|
|
1 |
+
# Full, Final, and Corrected Code for: launcher.py
|
2 |
|
3 |
+
import streamlit as st
|
4 |
import subprocess
|
5 |
import time
|
6 |
import os
|
7 |
|
8 |
+
# --- Use Streamlit's session state to ensure this runs only ONCE ---
|
9 |
+
if 'backend_process' not in st.session_state:
|
10 |
+
print("--- LAUNCHER: First run. Starting backend server... ---")
|
11 |
+
|
12 |
+
# We construct the command to run uvicorn
|
13 |
+
backend_command = [
|
14 |
+
"uvicorn",
|
15 |
+
"v2_multilingual_api.backend.main:app",
|
16 |
+
"--host", "0.0.0.0",
|
17 |
+
"--port", "8000"
|
18 |
+
]
|
19 |
+
|
20 |
+
# Open log files for the backend's output and errors
|
21 |
+
backend_log = open("backend.log", "w")
|
22 |
+
|
23 |
+
# Start the backend process and store it in the session state
|
24 |
+
st.session_state.backend_process = subprocess.Popen(
|
25 |
+
backend_command, stdout=backend_log, stderr=backend_log
|
26 |
+
)
|
27 |
+
|
28 |
+
print(f"--- LAUNCHER: Backend process started with PID: {st.session_state.backend_process.pid} ---")
|
29 |
+
print("--- LAUNCHER: Waiting 20 seconds for backend to initialize models... ---")
|
30 |
+
time.sleep(20)
|
31 |
+
|
32 |
+
# --- Health Check for the Backend ---
|
33 |
+
# Check if the process crashed during startup
|
34 |
+
if st.session_state.backend_process.poll() is not None:
|
35 |
+
print("--- LAUNCHER: !!! BACKEND FAILED TO START ON INITIALIZATION !!! ---")
|
36 |
+
backend_log.close()
|
37 |
+
with open("backend.log", "r") as f:
|
38 |
+
print(f.read())
|
39 |
+
st.error("The backend server failed to start. Please check the logs.")
|
40 |
+
# We use st.stop() to halt the app if the backend fails
|
41 |
+
st.stop()
|
42 |
+
else:
|
43 |
+
print("--- LAUNCHER: Backend health check passed. Process is running. ---")
|
44 |
+
|
|
|
45 |
# --- Step 3: Launch the Streamlit Frontend ---
|
46 |
+
# This part of the code will run every time, but the backend is already running.
|
47 |
+
# The command is to run the *actual* frontend app file.
|
48 |
|
49 |
+
print("--- LAUNCHER: Handing off to the main Streamlit frontend application. ---")
|
50 |
|
51 |
# We use os.system for simplicity to run the final command
|
52 |
frontend_command = (
|
|
|
55 |
"--server.address 0.0.0.0"
|
56 |
)
|
57 |
|
58 |
+
os.system(frontend_command)
|
|
|
|
|
|