MetiMiester commited on
Commit
a959408
·
verified ·
1 Parent(s): 2c954bb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +53 -59
main.py CHANGED
@@ -1,59 +1,53 @@
1
- # main.py — ASGI entrypoint for BubbleGuard Web (HF-ready, with GDrive fetch)
2
- # Coder: Amir
3
- # ------------------------------------------------
4
- # - Downloads model bundles from Google Drive (via download_assets.py)
5
- # - Starts Uvicorn serving app_server:app
6
- # - Respects HOST/PORT env (PORT is injected by Hugging Face Spaces)
7
-
8
- import os
9
- import sys
10
- import traceback
11
-
12
- def _truthy(val: str) -> bool:
13
- return str(val).strip().lower() in {"1", "true", "yes", "on"}
14
-
15
- def main() -> None:
16
- # --- Env & logging setup ---
17
- host = os.getenv("HOST", "0.0.0.0")
18
- port = int(os.getenv("PORT", "8000"))
19
- debug = _truthy(os.getenv("DEBUG", "0"))
20
-
21
- print(f"[BubbleGuard] Starting server on {host}:{port}")
22
- folder_id = os.getenv("GDRIVE_FOLDER_ID", "").strip()
23
- if folder_id:
24
- print(f"[BubbleGuard] Using Google Drive folder: {folder_id}")
25
- else:
26
- print("[BubbleGuard] No GDRIVE_FOLDER_ID set (will try per-file IDs if provided).")
27
-
28
- # --- Ensure assets (idempotent) before importing the app ---
29
- try:
30
- from download_assets import ensure_all_assets
31
- ensure_all_assets()
32
- except Exception as e:
33
- print("[BubbleGuard] ERROR while preparing assets:", file=sys.stderr)
34
- traceback.print_exc()
35
- raise SystemExit(1)
36
-
37
- # --- Run Uvicorn ---
38
- try:
39
- import uvicorn
40
- # Use string import so app is imported after assets are ready
41
- uvicorn.run(
42
- "app_server:app",
43
- host=host,
44
- port=port,
45
- reload=False, # safer with large models
46
- workers=1, # avoid duplicating model memory
47
- log_level="debug" if debug else "info",
48
- proxy_headers=True, # play nice behind proxies (HF Spaces)
49
- forwarded_allow_ips="*",
50
- timeout_keep_alive=10,
51
- access_log=True,
52
- )
53
- except Exception:
54
- print("[BubbleGuard] Uvicorn failed to start:", file=sys.stderr)
55
- traceback.print_exc()
56
- raise SystemExit(1)
57
-
58
- if __name__ == "__main__":
59
- main()
 
1
+ # main.py — ASGI entrypoint for BubbleGuard Web (HF-ready, with GDrive fetch)
2
+ # Author: Amir
3
+
4
+ import os
5
+ import sys
6
+ import traceback
7
+
8
+ def _truthy(val: str) -> bool:
9
+ return str(val).strip().lower() in {"1", "true", "yes", "on"}
10
+
11
+ def main() -> None:
12
+ host = os.getenv("HOST", "0.0.0.0")
13
+ port = int(os.getenv("PORT", "8000"))
14
+ debug = _truthy(os.getenv("DEBUG", "0"))
15
+
16
+ print(f"[BubbleGuard] Starting server on {host}:{port}")
17
+ folder_id = os.getenv("GDRIVE_FOLDER_ID", "").strip()
18
+ if folder_id:
19
+ print(f"[BubbleGuard] Using Google Drive folder: {folder_id}")
20
+ else:
21
+ print("[BubbleGuard] No GDRIVE_FOLDER_ID set (will fail to load assets).")
22
+
23
+ # Ensure assets before importing the app
24
+ try:
25
+ from download_assets import ensure_all_assets
26
+ ensure_all_assets()
27
+ except Exception as e:
28
+ print("[BubbleGuard] ERROR while preparing assets:", file=sys.stderr)
29
+ traceback.print_exc()
30
+ raise SystemExit(1)
31
+
32
+ # Start Uvicorn after assets are ready
33
+ try:
34
+ import uvicorn
35
+ uvicorn.run(
36
+ "app_server:app",
37
+ host=host,
38
+ port=port,
39
+ reload=False,
40
+ workers=1,
41
+ log_level="debug" if debug else "info",
42
+ proxy_headers=True,
43
+ forwarded_allow_ips="*",
44
+ timeout_keep_alive=10,
45
+ access_log=True,
46
+ )
47
+ except Exception:
48
+ print("[BubbleGuard] Uvicorn failed to start:", file=sys.stderr)
49
+ traceback.print_exc()
50
+ raise SystemExit(1)
51
+
52
+ if __name__ == "__main__":
53
+ main()