| """ |
| Local FastAPI Server Startup Script |
| Runs the certificate verification API with .pth model |
| """ |
|
|
| import os |
| import sys |
| import subprocess |
| from pathlib import Path |
|
|
| def check_requirements(): |
| """Check if required files exist""" |
| print("π Checking requirements...") |
| |
| required_files = [ |
| "api.py", |
| "vit_seal_checker.pth", |
| "vit_seal_classifier.py", |
| "yolo_seal_detector.py", |
| "models/best.pt" |
| ] |
| |
| missing_files = [] |
| for file in required_files: |
| if not os.path.exists(file): |
| missing_files.append(file) |
| print(f" β Missing: {file}") |
| else: |
| print(f" β
Found: {file}") |
| |
| if missing_files: |
| print(f"\nβ οΈ Warning: {len(missing_files)} required file(s) missing") |
| print("Some features may not work correctly.") |
| else: |
| print("\nβ
All required files present!") |
| |
| return len(missing_files) == 0 |
|
|
| def check_dependencies(): |
| """Check if required Python packages are installed""" |
| print("\nπ Checking Python dependencies...") |
| |
| required_packages = [ |
| "fastapi", |
| "uvicorn", |
| "torch", |
| "torchvision", |
| "transformers", |
| "pillow", |
| "ultralytics" |
| ] |
| |
| missing_packages = [] |
| for package in required_packages: |
| try: |
| __import__(package) |
| print(f" β
{package}") |
| except ImportError: |
| print(f" β {package}") |
| missing_packages.append(package) |
| |
| if missing_packages: |
| print(f"\nβ οΈ Missing packages: {', '.join(missing_packages)}") |
| print("Install with: pip install -r requirements.txt") |
| return False |
| else: |
| print("\nβ
All dependencies installed!") |
| return True |
|
|
| def start_server(port=8000): |
| """Start the FastAPI server""" |
| print(f"\nπ Starting FastAPI server on port {port}...") |
| print("="*60) |
| print(f"π¦ Model: vit_seal_checker.pth (PyTorch)") |
| print(f"π API URL: http://localhost:{port}") |
| print(f"π Docs: http://localhost:{port}/api/docs") |
| print(f"π Health: http://localhost:{port}/api/health") |
| print("="*60) |
| print("\nPress Ctrl+C to stop the server\n") |
| |
| try: |
| |
| subprocess.run([ |
| sys.executable, |
| "api.py" |
| ], env={**os.environ, "PORT": str(port)}) |
| except KeyboardInterrupt: |
| print("\n\nπ Server stopped by user") |
| except Exception as e: |
| print(f"\nβ Error starting server: {e}") |
|
|
| def main(): |
| """Main function""" |
| print("\n" + "="*60) |
| print("π― Certificate Verification API - Local Server") |
| print("="*60) |
| |
| |
| files_ok = check_requirements() |
| deps_ok = check_dependencies() |
| |
| if not deps_ok: |
| print("\nβ Cannot start server: Missing dependencies") |
| print("Run: pip install -r requirements.txt") |
| sys.exit(1) |
| |
| if not files_ok: |
| response = input("\nβ οΈ Continue anyway? (y/n): ") |
| if response.lower() != 'y': |
| print("Exiting...") |
| sys.exit(1) |
| |
| |
| port = 8000 |
| if len(sys.argv) > 1: |
| try: |
| port = int(sys.argv[1]) |
| except ValueError: |
| print(f"β οΈ Invalid port: {sys.argv[1]}, using default 8000") |
| |
| |
| start_server(port) |
|
|
| if __name__ == "__main__": |
| main() |
|
|