File size: 3,494 Bytes
3a32bd4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | """
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:
# Run the API server
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)
# Check requirements
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)
# Get port from command line or use default
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
start_server(port)
if __name__ == "__main__":
main()
|