Spaces:
Paused
Paused
Create run.py
Browse files- 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()
|