File size: 3,263 Bytes
a7c22d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
"""
Startup script for FinSolve RAG System
Runs both FastAPI and Streamlit simultaneously
"""

import subprocess
import sys
import time
import os
import signal
import threading
from pathlib import Path

def run_fastapi():
    """Run FastAPI server"""
    print("πŸš€ Starting FastAPI server on http://localhost:8000")
    try:
        # Run FastAPI with uvicorn
        subprocess.run([
            sys.executable, "-m", "uvicorn", 
            "api:app", 
            "--host", "0.0.0.0", 
            "--port", "8000",
            "--reload"
        ], check=True)
    except KeyboardInterrupt:
        print("πŸ›‘ FastAPI server stopped")
    except Exception as e:
        print(f"❌ Error running FastAPI: {e}")

def run_streamlit():
    """Run Streamlit app"""
    print("πŸš€ Starting Streamlit app on http://localhost:8501")
    try:
        # Wait a moment for FastAPI to start
        time.sleep(3)
        
        # Run Streamlit
        subprocess.run([
            sys.executable, "-m", "streamlit", "run", 
            "streamlit_app.py",
            "--server.port", "8501",
            "--server.address", "0.0.0.0"
        ], check=True)
    except KeyboardInterrupt:
        print("πŸ›‘ Streamlit app stopped")
    except Exception as e:
        print(f"❌ Error running Streamlit: {e}")

def check_dependencies():
    """Check if all required dependencies are installed"""
    required_packages = [
        'fastapi', 'uvicorn', 'streamlit', 'chromadb', 
        'sentence-transformers', 'openai', 'pandas', 'plotly'
    ]
    
    missing_packages = []
    for package in required_packages:
        try:
            __import__(package.replace('-', '_'))
        except ImportError:
            missing_packages.append(package)
    
    if missing_packages:
        print(f"❌ Missing packages: {', '.join(missing_packages)}")
        print("πŸ“¦ Install with: pip install -r requirements.txt")
        return False
    
    print("βœ… All dependencies satisfied")
    return True

def signal_handler(signum, frame):
    """Handle shutdown signals"""
    print("\nπŸ›‘ Shutting down servers...")
    sys.exit(0)

def main():
    """Main function to start both servers"""
    print("πŸ€– FinSolve RAG System Startup")
    print("="*50)
    
    # Check dependencies
    if not check_dependencies():
        sys.exit(1)
    
    # Set up signal handlers for graceful shutdown
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)
    
    print("\nπŸ—οΈ Starting Complete Tech Stack:")
    print("πŸ“‘ FastAPI: http://localhost:8000")
    print("🌐 Streamlit: http://localhost:8501")
    print("πŸ“– API Docs: http://localhost:8000/docs")
    print("\nπŸ’‘ Press Ctrl+C to stop both servers\n")
    
    try:
        # Start FastAPI in a separate thread
        fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
        fastapi_thread.start()
        
        # Run Streamlit in main thread
        run_streamlit()
        
    except KeyboardInterrupt:
        print("\nπŸ›‘ Received shutdown signal")
    except Exception as e:
        print(f"❌ Startup error: {e}")
    finally:
        print("πŸ‘‹ Goodbye!")

if __name__ == "__main__":
    main()