LogicGoInfotechSpaces commited on
Commit
375118a
·
verified ·
1 Parent(s): 292b6e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -6
app.py CHANGED
@@ -13,7 +13,8 @@ from pymongo import MongoClient
13
  import gridfs
14
  from bson.objectid import ObjectId
15
  from PIL import Image
16
-
 
17
  import firebase_admin
18
  from firebase_admin import credentials, auth
19
 
@@ -93,12 +94,77 @@ class HealthResponse(BaseModel):
93
  # ---------------------------------------------------------------------
94
  # Endpoints
95
  # ---------------------------------------------------------------------
96
- @app.get("/health", response_model=HealthResponse)
97
- def health():
98
- """Public health check"""
99
- mongo.admin.command("ping")
100
- return HealthResponse(status="ok", db=db.name, model="Qwen/Qwen-Image-Edit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  @app.post("/generate")
104
  async def generate(
 
13
  import gridfs
14
  from bson.objectid import ObjectId
15
  from PIL import Image
16
+ from fastapi.concurrency import run_in_threadpool
17
+ import shutil
18
  import firebase_admin
19
  from firebase_admin import credentials, auth
20
 
 
94
  # ---------------------------------------------------------------------
95
  # Endpoints
96
  # ---------------------------------------------------------------------
97
+ # @app.get("/health", response_model=HealthResponse)
98
+ # def health():
99
+ # """Public health check"""
100
+ # mongo.admin.command("ping")
101
+ # return HealthResponse(status="ok", db=db.name, model="Qwen/Qwen-Image-Edit")
102
+ @app.get("/health")
103
+ async def health():
104
+ checks = {
105
+ "api": "ok",
106
+ "mongo": "unknown",
107
+ "gridfs": "unknown",
108
+ "firebase": "unknown",
109
+ "huggingface": "unknown",
110
+ "timestamp": datetime.utcnow().isoformat()
111
+ }
112
+
113
+ errors = {}
114
+
115
+ # ------------------ MONGO CHECK ------------------
116
+ try:
117
+ def mongo_ping():
118
+ mongo.admin.command("ping")
119
+
120
+ await run_in_threadpool(mongo_ping)
121
+ checks["mongo"] = "connected"
122
+ except Exception as e:
123
+ checks["mongo"] = "failed"
124
+ errors["mongo"] = str(e)
125
+
126
+ # ------------------ GRIDFS CHECK ------------------
127
+ try:
128
+ def gridfs_test():
129
+ test_id = fs.put(b"healthcheck", filename="health.txt")
130
+ fs.delete(test_id)
131
+
132
+ await run_in_threadpool(gridfs_test)
133
+ checks["gridfs"] = "ok"
134
+ except Exception as e:
135
+ checks["gridfs"] = "failed"
136
+ errors["gridfs"] = str(e)
137
 
138
+ # ------------------ FIREBASE CHECK ------------------
139
+ try:
140
+ _ = firebase_admin.get_app()
141
+ checks["firebase"] = "initialized"
142
+ except Exception as e:
143
+ checks["firebase"] = "failed"
144
+ errors["firebase"] = str(e)
145
+
146
+ # ------------------ HUGGINGFACE CHECK ------------------
147
+ try:
148
+ _ = hf_client.model_info("Qwen/Qwen-Image-Edit")
149
+ checks["huggingface"] = "reachable"
150
+ except Exception as e:
151
+ checks["huggingface"] = "failed"
152
+ errors["huggingface"] = str(e)
153
+ # ------------------ FINAL STATUS ------------------
154
+ is_healthy = len(errors) == 0
155
+
156
+ if is_healthy:
157
+ checks["status"] = "healthy"
158
+ return checks
159
+
160
+ raise HTTPException(
161
+ status_code=503,
162
+ detail={
163
+ "status": "unhealthy",
164
+ "checks": checks,
165
+ "errors": errors
166
+ }
167
+ )
168
 
169
  @app.post("/generate")
170
  async def generate(