Spaces:
Build error
Build error
| #!/usr/bin/env python3 | |
| """ | |
| Simple startup script for the Crowd Detection Backend | |
| """ | |
| import subprocess | |
| import sys | |
| import time | |
| import requests | |
| def check_dependencies(): | |
| """Check if required packages are installed""" | |
| required_packages = [ | |
| 'fastapi', 'uvicorn', 'websockets', 'opencv-python', | |
| 'ultralytics', 'numpy', 'scipy', 'pillow', 'python-multipart', 'aiofiles' | |
| ] | |
| missing_packages = [] | |
| for package in required_packages: | |
| try: | |
| __import__(package.replace('-', '_')) | |
| print(f"β {package}") | |
| except ImportError: | |
| missing_packages.append(package) | |
| print(f"β {package}") | |
| if missing_packages: | |
| print(f"\nπ¨ Missing packages: {', '.join(missing_packages)}") | |
| print("Installing missing packages...") | |
| try: | |
| subprocess.run([sys.executable, "-m", "pip", "install"] + missing_packages, check=True) | |
| print("β All packages installed successfully!") | |
| except subprocess.CalledProcessError: | |
| print("β Failed to install packages. Please install manually:") | |
| print(f"pip install {' '.join(missing_packages)}") | |
| return False | |
| return True | |
| def start_server(): | |
| """Start the FastAPI server""" | |
| print("\nπ Starting Crowd Detection Backend...") | |
| try: | |
| # Start the server | |
| process = subprocess.Popen([ | |
| sys.executable, "-m", "uvicorn", | |
| "main:app", | |
| "--host", "0.0.0.0", | |
| "--port", "7860", # changed | |
| "--workers", "1" # better than --reload in container | |
| ]) | |
| print("β Server started successfully!") | |
| print("π Backend URL: http://localhost:7860") | |
| print("π API Docs: http://localhost:7860/docs") | |
| print("π Health Check: http://localhost:7860/health") | |
| print("\nπ‘ To test the API:") | |
| print(" curl http://localhost:7860/health") | |
| print(" curl http://localhost:7860/") | |
| print("\nβΉοΈ Press Ctrl+C to stop the server") | |
| # Wait for the process to complete | |
| process.wait() | |
| except KeyboardInterrupt: | |
| print("\nπ Shutting down server...") | |
| if process: | |
| process.terminate() | |
| process.wait() | |
| print("β Server stopped") | |
| except Exception as e: | |
| print(f"β Failed to start server: {e}") | |
| return False | |
| return True | |
| def test_endpoints(): | |
| """Test basic endpoints""" | |
| print("\nπ§ͺ Testing endpoints...") | |
| try: | |
| # Test health endpoint | |
| response = requests.get("http://localhost:7860/health", timeout=5) | |
| if response.status_code == 200: | |
| print("β Health endpoint working") | |
| data = response.json() | |
| print(f" Status: {data.get('status')}") | |
| print(f" Zones: {data.get('zones_count')}") | |
| print(f" Cameras: {data.get('cameras_count')}") | |
| else: | |
| print(f"β Health endpoint failed: {response.status_code}") | |
| # Test zones endpoint | |
| response = requests.get("http://localhost:7860/zones/heatmap", timeout=5) | |
| if response.status_code == 200: | |
| print("β Zones endpoint working") | |
| zones = response.json() | |
| print(f" Found {len(zones)} zones") | |
| else: | |
| print(f"β Zones endpoint failed: {response.status_code}") | |
| except requests.exceptions.ConnectionError: | |
| print("β Cannot connect to server. Is it running?") | |
| except Exception as e: | |
| print(f"β Test failed: {e}") | |
| if __name__ == "__main__": | |
| print("π§ Crowd Detection Backend Startup") | |
| print("=" * 50) | |
| # Check dependencies | |
| if not check_dependencies(): | |
| print("β Dependency check failed. Exiting.") | |
| sys.exit(1) | |
| # Start server | |
| if start_server(): | |
| print("β Backend startup completed successfully!") | |
| else: | |
| print("β Backend startup failed!") | |
| sys.exit(1) |