LogicGoInfotechSpaces commited on
Commit
482c43a
·
verified ·
1 Parent(s): df13730

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -2
app.py CHANGED
@@ -27,7 +27,8 @@ from collections import defaultdict
27
  import facexlib.utils.misc as facex_misc
28
  from basicsr.utils.realesrganer import RealESRGANer
29
  from facexlib.utils.misc import download_from_url
30
-
 
31
  from utils.dataops import auto_split_upscale
32
  from typing import List, Optional
33
  from datetime import datetime
@@ -928,10 +929,62 @@ DEFAULT_WITH_MODEL_NAME = True
928
  # Singleton Upscale instance for API
929
  _api_upscale = Upscale()
930
 
 
 
 
931
  @fastapi_app.get("/health")
932
  def health(_: bool = Depends(_verify_bearer_token)):
933
- return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935
  @fastapi_app.get("/source")
936
  def source(_: bool = Depends(_verify_bearer_token)):
937
  return {
 
27
  import facexlib.utils.misc as facex_misc
28
  from basicsr.utils.realesrganer import RealESRGANer
29
  from facexlib.utils.misc import download_from_url
30
+ from fastapi.concurrency import run_in_threadpool
31
+ import shutil
32
  from utils.dataops import auto_split_upscale
33
  from typing import List, Optional
34
  from datetime import datetime
 
929
  # Singleton Upscale instance for API
930
  _api_upscale = Upscale()
931
 
932
+ # @fastapi_app.get("/health")
933
+ # def health(_: bool = Depends(_verify_bearer_token)):
934
+ # return {"status": "ok"}
935
  @fastapi_app.get("/health")
936
  def health(_: bool = Depends(_verify_bearer_token)):
937
+ checks = {
938
+ "api": "ok",
939
+ "gpu": "unknown",
940
+ "torch": "unknown",
941
+ "disk": "unknown",
942
+ "mongo": "skipped",
943
+ "weights_dir": "unknown",
944
+ "timestamp": datetime.utcnow().isoformat()
945
+ }
946
+
947
+ errors = {}
948
 
949
+ # -------- 1. TORCH & CUDA CHECK --------
950
+ try:
951
+ checks["torch"] = torch.__version__
952
+ checks["gpu"] = "available" if torch.cuda.is_available() else "cpu-only"
953
+ if torch.cuda.is_available():
954
+ checks["gpu_name"] = torch.cuda.get_device_name(0)
955
+ checks["gpu_mem_allocated_mb"] = int(torch.cuda.memory_allocated(0) / 1024 / 1024)
956
+ except Exception as e:
957
+ checks["gpu"] = "failed"
958
+ errors["gpu"] = str(e)
959
+ # -------- 2. MONGODB CHECK (OPTIONAL) --------
960
+ if _mongo_client is not None:
961
+ try:
962
+ def mongo_ping():
963
+ _mongo_client.admin.command("ping")
964
+
965
+ await run_in_threadpool(mongo_ping)
966
+ checks["mongo"] = "connected"
967
+ except Exception as e:
968
+ checks["mongo"] = "failed"
969
+ errors["mongo"] = str(e)
970
+ else:
971
+ checks["mongo"] = "not_configured"
972
+
973
+ # -------- FINAL STATUS --------
974
+ is_healthy = len(errors) == 0
975
+
976
+ if is_healthy:
977
+ checks["status"] = "healthy"
978
+ return checks
979
+
980
+ raise HTTPException(
981
+ status_code=503,
982
+ detail={
983
+ "status": "unhealthy",
984
+ "checks": checks,
985
+ "errors": errors
986
+ }
987
+ )
988
  @fastapi_app.get("/source")
989
  def source(_: bool = Depends(_verify_bearer_token)):
990
  return {