Spaces:
Running on T4
Running on T4
diagnostics: surface gen_error + vram in /api/health
Browse files
app.py
CHANGED
|
@@ -115,8 +115,17 @@ def _prep(req: "GenReq"):
|
|
| 115 |
|
| 116 |
@app.get("/api/health")
|
| 117 |
def health():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
return {"status": _state["status"], "device": _state["device"], "model": MODEL_ID,
|
| 119 |
-
"error": _state["error"]}
|
| 120 |
|
| 121 |
|
| 122 |
def _not_ready_message():
|
|
@@ -162,12 +171,24 @@ def stream(req: GenReq):
|
|
| 162 |
|
| 163 |
|
| 164 |
def _safe_generate(model, kwargs):
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
try:
|
| 167 |
with torch.no_grad():
|
| 168 |
model.generate(**kwargs)
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
|
| 173 |
@app.post("/api/generate")
|
|
|
|
| 115 |
|
| 116 |
@app.get("/api/health")
|
| 117 |
def health():
|
| 118 |
+
import torch
|
| 119 |
+
vram = None
|
| 120 |
+
try:
|
| 121 |
+
if torch.cuda.is_available():
|
| 122 |
+
free, total = torch.cuda.mem_get_info()
|
| 123 |
+
vram = {"free_gb": round(free / 1e9, 2), "total_gb": round(total / 1e9, 2),
|
| 124 |
+
"allocated_gb": round(torch.cuda.memory_allocated() / 1e9, 2)}
|
| 125 |
+
except Exception:
|
| 126 |
+
pass
|
| 127 |
return {"status": _state["status"], "device": _state["device"], "model": MODEL_ID,
|
| 128 |
+
"error": _state["error"], "gen_error": _state.get("gen_error"), "vram": vram}
|
| 129 |
|
| 130 |
|
| 131 |
def _not_ready_message():
|
|
|
|
| 171 |
|
| 172 |
|
| 173 |
def _safe_generate(model, kwargs):
|
| 174 |
+
"""Never raise into the request path — but DO record why, or failures are invisible."""
|
| 175 |
+
import torch, traceback
|
| 176 |
+
try:
|
| 177 |
+
torch.cuda.empty_cache()
|
| 178 |
+
except Exception:
|
| 179 |
+
pass
|
| 180 |
try:
|
| 181 |
with torch.no_grad():
|
| 182 |
model.generate(**kwargs)
|
| 183 |
+
_state["gen_error"] = None
|
| 184 |
+
except Exception as e:
|
| 185 |
+
_state["gen_error"] = "%s: %s" % (type(e).__name__, str(e)[:400])
|
| 186 |
+
traceback.print_exc()
|
| 187 |
+
finally:
|
| 188 |
+
try:
|
| 189 |
+
torch.cuda.empty_cache()
|
| 190 |
+
except Exception:
|
| 191 |
+
pass
|
| 192 |
|
| 193 |
|
| 194 |
@app.post("/api/generate")
|