Spaces:
Paused
Paused
| #!/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() |