amirulhazym commited on
Commit
7e0c6a8
·
1 Parent(s): 061a705

fix(deploy): Prevent backend re-execution on Streamlit rerun

Browse files
Files changed (1) hide show
  1. 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
- # --- 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 = (
@@ -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)