will702 commited on
Commit
5c3e365
Β·
1 Parent(s): ae54b1b

cleanup: remove debug tracing, restore clean health endpoint

Browse files
Files changed (2) hide show
  1. app/main.py +2 -48
  2. app/routers/predict.py +3 -2
app/main.py CHANGED
@@ -1,8 +1,6 @@
1
  """StockPro ML Backend β€” FastAPI app for Hugging Face Spaces."""
2
- import traceback
3
- from fastapi import FastAPI, Request
4
  from fastapi.middleware.cors import CORSMiddleware
5
- from fastapi.responses import JSONResponse
6
 
7
  from app.routers import predict, anomaly
8
 
@@ -23,10 +21,6 @@ app.include_router(predict.router, tags=["prediction"])
23
  app.include_router(anomaly.router, tags=["anomaly"])
24
 
25
 
26
- @app.exception_handler(Exception)
27
- async def global_exception_handler(request: Request, exc: Exception):
28
- return JSONResponse(status_code=500, content={"error": traceback.format_exc()})
29
-
30
 
31
  @app.get("/")
32
  def root():
@@ -35,44 +29,4 @@ def root():
35
 
36
  @app.get("/health")
37
  def health():
38
- import os
39
- import app.config as cfg
40
- from huggingface_hub import hf_hub_download
41
-
42
- model_path = os.path.join(cfg.MODEL_DIR, "tft_stock.ckpt")
43
- params_path = os.path.join(cfg.MODEL_DIR, "dataset_params.pt")
44
-
45
- download_error = None
46
- if not os.path.exists(model_path) and cfg.HF_TOKEN and cfg.MODEL_REPO:
47
- try:
48
- os.makedirs(cfg.MODEL_DIR, exist_ok=True)
49
- hf_hub_download(
50
- repo_id=cfg.MODEL_REPO,
51
- filename="tft_stock.ckpt",
52
- token=cfg.HF_TOKEN,
53
- local_dir=cfg.MODEL_DIR,
54
- )
55
- hf_hub_download(
56
- repo_id=cfg.MODEL_REPO,
57
- filename="dataset_params.pt",
58
- token=cfg.HF_TOKEN,
59
- local_dir=cfg.MODEL_DIR,
60
- )
61
- hf_hub_download(
62
- repo_id=cfg.MODEL_REPO,
63
- filename="ddg_da.pt",
64
- token=cfg.HF_TOKEN,
65
- local_dir=cfg.MODEL_DIR,
66
- )
67
- except Exception as e:
68
- download_error = str(e)
69
-
70
- return {
71
- "status": "ok",
72
- "hf_token_set": bool(cfg.HF_TOKEN),
73
- "hf_model_repo": cfg.MODEL_REPO or "(not set)",
74
- "model_exists": os.path.exists(model_path),
75
- "params_exists": os.path.exists(params_path),
76
- "model_dir": cfg.MODEL_DIR,
77
- "download_error": download_error,
78
- }
 
1
  """StockPro ML Backend β€” FastAPI app for Hugging Face Spaces."""
2
+ from fastapi import FastAPI
 
3
  from fastapi.middleware.cors import CORSMiddleware
 
4
 
5
  from app.routers import predict, anomaly
6
 
 
21
  app.include_router(anomaly.router, tags=["anomaly"])
22
 
23
 
 
 
 
 
24
 
25
  @app.get("/")
26
  def root():
 
29
 
30
  @app.get("/health")
31
  def health():
32
+ return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/routers/predict.py CHANGED
@@ -75,9 +75,10 @@ async def predict(req: PredictRequest, x_api_key: str = Header(default="")):
75
  dataset_params_path=DATASET_PARAMS_PATH,
76
  symbol=data["symbol"],
77
  )
 
 
78
  except Exception as e:
79
- import traceback
80
- raise HTTPException(status_code=500, detail=traceback.format_exc())
81
 
82
  # ── DDG-DA: concept drift detection ──
83
  # Drift score and flag are added to the response for transparency.
 
75
  dataset_params_path=DATASET_PARAMS_PATH,
76
  symbol=data["symbol"],
77
  )
78
+ except ValueError as e:
79
+ raise HTTPException(status_code=422, detail=str(e))
80
  except Exception as e:
81
+ raise HTTPException(status_code=500, detail=str(e))
 
82
 
83
  # ── DDG-DA: concept drift detection ──
84
  # Drift score and flag are added to the response for transparency.