JanviMl commited on
Commit
a7c22d2
Β·
verified Β·
1 Parent(s): 92c4bf7

Create run.py

Browse files
Files changed (1) hide show
  1. src/run.py +113 -0
src/run.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Startup script for FinSolve RAG System
4
+ Runs both FastAPI and Streamlit simultaneously
5
+ """
6
+
7
+ import subprocess
8
+ import sys
9
+ import time
10
+ import os
11
+ import signal
12
+ import threading
13
+ from pathlib import Path
14
+
15
+ def run_fastapi():
16
+ """Run FastAPI server"""
17
+ print("πŸš€ Starting FastAPI server on http://localhost:8000")
18
+ try:
19
+ # Run FastAPI with uvicorn
20
+ subprocess.run([
21
+ sys.executable, "-m", "uvicorn",
22
+ "api:app",
23
+ "--host", "0.0.0.0",
24
+ "--port", "8000",
25
+ "--reload"
26
+ ], check=True)
27
+ except KeyboardInterrupt:
28
+ print("πŸ›‘ FastAPI server stopped")
29
+ except Exception as e:
30
+ print(f"❌ Error running FastAPI: {e}")
31
+
32
+ def run_streamlit():
33
+ """Run Streamlit app"""
34
+ print("πŸš€ Starting Streamlit app on http://localhost:8501")
35
+ try:
36
+ # Wait a moment for FastAPI to start
37
+ time.sleep(3)
38
+
39
+ # Run Streamlit
40
+ subprocess.run([
41
+ sys.executable, "-m", "streamlit", "run",
42
+ "streamlit_app.py",
43
+ "--server.port", "8501",
44
+ "--server.address", "0.0.0.0"
45
+ ], check=True)
46
+ except KeyboardInterrupt:
47
+ print("πŸ›‘ Streamlit app stopped")
48
+ except Exception as e:
49
+ print(f"❌ Error running Streamlit: {e}")
50
+
51
+ def check_dependencies():
52
+ """Check if all required dependencies are installed"""
53
+ required_packages = [
54
+ 'fastapi', 'uvicorn', 'streamlit', 'chromadb',
55
+ 'sentence-transformers', 'openai', 'pandas', 'plotly'
56
+ ]
57
+
58
+ missing_packages = []
59
+ for package in required_packages:
60
+ try:
61
+ __import__(package.replace('-', '_'))
62
+ except ImportError:
63
+ missing_packages.append(package)
64
+
65
+ if missing_packages:
66
+ print(f"❌ Missing packages: {', '.join(missing_packages)}")
67
+ print("πŸ“¦ Install with: pip install -r requirements.txt")
68
+ return False
69
+
70
+ print("βœ… All dependencies satisfied")
71
+ return True
72
+
73
+ def signal_handler(signum, frame):
74
+ """Handle shutdown signals"""
75
+ print("\nπŸ›‘ Shutting down servers...")
76
+ sys.exit(0)
77
+
78
+ def main():
79
+ """Main function to start both servers"""
80
+ print("πŸ€– FinSolve RAG System Startup")
81
+ print("="*50)
82
+
83
+ # Check dependencies
84
+ if not check_dependencies():
85
+ sys.exit(1)
86
+
87
+ # Set up signal handlers for graceful shutdown
88
+ signal.signal(signal.SIGINT, signal_handler)
89
+ signal.signal(signal.SIGTERM, signal_handler)
90
+
91
+ print("\nπŸ—οΈ Starting Complete Tech Stack:")
92
+ print("πŸ“‘ FastAPI: http://localhost:8000")
93
+ print("🌐 Streamlit: http://localhost:8501")
94
+ print("πŸ“– API Docs: http://localhost:8000/docs")
95
+ print("\nπŸ’‘ Press Ctrl+C to stop both servers\n")
96
+
97
+ try:
98
+ # Start FastAPI in a separate thread
99
+ fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
100
+ fastapi_thread.start()
101
+
102
+ # Run Streamlit in main thread
103
+ run_streamlit()
104
+
105
+ except KeyboardInterrupt:
106
+ print("\nπŸ›‘ Received shutdown signal")
107
+ except Exception as e:
108
+ print(f"❌ Startup error: {e}")
109
+ finally:
110
+ print("πŸ‘‹ Goodbye!")
111
+
112
+ if __name__ == "__main__":
113
+ main()