Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| import subprocess | |
| import time | |
| import webbrowser | |
| import urllib.request | |
| def install_dependencies(): | |
| print("[*] Verifying system dependencies...") | |
| try: | |
| # Check and install requirements | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"]) | |
| print("[+] Dependency checks passed successfully!") | |
| except Exception as e: | |
| print(f"[-] Error installing dependencies: {e}") | |
| sys.exit(1) | |
| def verify_models(): | |
| models_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "backend", "models") | |
| os.makedirs(models_dir, exist_ok=True) | |
| yunet_path = os.path.join(models_dir, "face_detection_yunet_2023mar.onnx") | |
| sface_path = os.path.join(models_dir, "face_recognition_sface_2021dec.onnx") | |
| yunet_url = "https://huggingface.co/opencv/face_detection_yunet/resolve/main/face_detection_yunet_2023mar.onnx" | |
| sface_url = "https://huggingface.co/opencv/face_recognition_sface/resolve/main/face_recognition_sface_2021dec.onnx" | |
| # Download helper | |
| def check_and_download(url, path, name): | |
| if os.path.exists(path) and os.path.getsize(path) > 10000: | |
| print(f"[+] Model '{name}' is already present and validated.") | |
| return | |
| print(f"[*] Downloading {name} ONNX model...") | |
| try: | |
| req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) | |
| with urllib.request.urlopen(req) as response, open(path, 'wb') as out_file: | |
| out_file.write(response.read()) | |
| print(f"[+] Successfully downloaded {name} ({os.path.getsize(path)} bytes)!") | |
| except Exception as e: | |
| print(f"[-] Failed to download {name}: {e}") | |
| sys.exit(1) | |
| check_and_download(yunet_url, yunet_path, "YuNet Face Detector") | |
| check_and_download(sface_url, sface_path, "SFace Vectorizer") | |
| def launch_server(): | |
| print("\n" + "="*60) | |
| print(" SECUREATTEND AI FACIAL ATTENDANCE CONTROL CENTER ") | |
| print("="*60 + "\n") | |
| print("[*] Launching localized FastAPI server on Uvicorn...") | |
| # Open browser after a slight delay to let the server bind first | |
| def open_browser(): | |
| time.sleep(2.0) | |
| print("[+] Opening UI dashboard in web browser: http://127.0.0.1:8000") | |
| webbrowser.open("http://127.0.0.1:8000") | |
| import threading | |
| threading.Thread(target=open_browser, daemon=True).start() | |
| try: | |
| import uvicorn | |
| # Run Uvicorn directly | |
| uvicorn.run("backend.main:app", host="127.0.0.1", port=8000, reload=False) | |
| except KeyboardInterrupt: | |
| print("\n[+] SecureAttend server shut down successfully.") | |
| except Exception as e: | |
| print(f"\n[-] Server execution failed: {e}") | |
| if __name__ == "__main__": | |
| # 1. Install pip requirements | |
| install_dependencies() | |
| # 2. Check/download YuNet and SFace ONNX binaries | |
| verify_models() | |
| # 3. Fire up Uvicorn and browser UI | |
| launch_server() | |