File size: 1,725 Bytes
61d360d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import subprocess
import time
import signal
import sys

def run_services():
    """
    Run both API and Streamlit app as separate processes.
    """
    print("Starting NAFNet services...")
    
    # Set environment variables to suppress warnings
    os.environ["PYTHONWARNINGS"] = "ignore"
    
    # Define commands
    api_cmd = ["python", "api.py"]
    streamlit_cmd = ["streamlit", "run", "app.py", "--server.port=8501"]
    
    # Start API server
    print("Starting API server on port 8001...")
    api_process = subprocess.Popen(api_cmd)
    
    # Wait for API to start
    print("Waiting for API to initialize (5 seconds)...")
    time.sleep(5)
    
    # Start Streamlit app
    print("Starting Streamlit app on port 8501...")
    streamlit_process = subprocess.Popen(streamlit_cmd)
    
    print("\n" + "="*50)
    print("Services started successfully!")
    print("API running at: http://localhost:8001")
    print("Streamlit app running at: http://localhost:8501")
    print("="*50 + "\n")
    
    # Handle graceful shutdown on Ctrl+C
    def signal_handler(sig, frame):
        print("\nShutting down services...")
        streamlit_process.terminate()
        api_process.terminate()
        
        # Wait for processes to terminate
        streamlit_process.wait()
        api_process.wait()
        
        print("Services stopped.")
        sys.exit(0)
    
    # Register signal handler
    signal.signal(signal.SIGINT, signal_handler)
    
    try:
        # Keep the script running
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        # Handle keyboard interrupt (Ctrl+C)
        signal_handler(None, None)

if __name__ == "__main__":
    run_services()