Amaro2a commited on
Commit
3df54e5
·
verified ·
1 Parent(s): 24ff554

Update app.py

Browse files

in safetensors

Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -11,6 +11,7 @@ import torch.nn as nn
11
  from fastapi import FastAPI, File, UploadFile, HTTPException
12
  from fastapi.middleware.cors import CORSMiddleware
13
  from pydantic import BaseModel
 
14
 
15
  # -----------------------------
16
  # Config
@@ -30,10 +31,10 @@ TARGETS = [
30
  ]
31
 
32
  MODEL_PATHS = {
33
- "jit": "pd_model.ts", # optional
34
- "state": "pd_model.pt", # state_dict or full Module
35
- "sx": "scaler_x.pkl",
36
- "sy": "scaler_y.pkl",
37
  }
38
 
39
  # -----------------------------
@@ -84,28 +85,27 @@ class PDTabTransformer(nn.Module):
84
  def load_model_and_scalers():
85
  # Scalers
86
  if not (os.path.exists(MODEL_PATHS["sx"]) and os.path.exists(MODEL_PATHS["sy"])):
87
- raise RuntimeError("Missing scalers. Expected scaler_x.pkl and scaler_y.pkl in working dir.")
88
  scaler_x = joblib.load(MODEL_PATHS["sx"])
89
  scaler_y = joblib.load(MODEL_PATHS["sy"])
90
 
91
- # Model (jit preferred)
92
  model = None
93
  if os.path.exists(MODEL_PATHS["jit"]):
94
  model = torch.jit.load(MODEL_PATHS["jit"], map_location="cpu")
95
  elif os.path.exists(MODEL_PATHS["state"]):
96
- state = torch.load(MODEL_PATHS["state"], map_location="cpu")
97
- if isinstance(state, nn.Module):
98
- model = state
99
- else:
100
- model = PDTabTransformer(num_features=len(FEATURES), output_dim=len(TARGETS))
101
- model.load_state_dict(state)
102
  else:
103
- raise RuntimeError("Model file not found. Provide pd_model.ts or pd_model.pt")
104
  model.eval()
105
  return model, scaler_x, scaler_y
106
 
107
  MODEL, SCALER_X, SCALER_Y = load_model_and_scalers()
108
 
 
109
  # -----------------------------
110
  # Utilities
111
  # -----------------------------
 
11
  from fastapi import FastAPI, File, UploadFile, HTTPException
12
  from fastapi.middleware.cors import CORSMiddleware
13
  from pydantic import BaseModel
14
+ from safetensors.torch import load_file
15
 
16
  # -----------------------------
17
  # Config
 
31
  ]
32
 
33
  MODEL_PATHS = {
34
+ "jit": "mode/pd_model.ts",
35
+ "state": "mode/pd_model.safetensors",
36
+ "sx": "mode/scaler_x.pkl",
37
+ "sy": "mode/scaler_y.pkl",
38
  }
39
 
40
  # -----------------------------
 
85
  def load_model_and_scalers():
86
  # Scalers
87
  if not (os.path.exists(MODEL_PATHS["sx"]) and os.path.exists(MODEL_PATHS["sy"])):
88
+ raise RuntimeError("Missing scalers. Expected scaler_x.pkl and scaler_y.pkl in mode/ directory.")
89
  scaler_x = joblib.load(MODEL_PATHS["sx"])
90
  scaler_y = joblib.load(MODEL_PATHS["sy"])
91
 
92
+ # Model
93
  model = None
94
  if os.path.exists(MODEL_PATHS["jit"]):
95
  model = torch.jit.load(MODEL_PATHS["jit"], map_location="cpu")
96
  elif os.path.exists(MODEL_PATHS["state"]):
97
+ # Load safetensors
98
+ state_dict = load_file(MODEL_PATHS["state"], device="cpu")
99
+ model = PDTabTransformer(num_features=len(FEATURES), output_dim=len(TARGETS))
100
+ model.load_state_dict(state_dict)
 
 
101
  else:
102
+ raise RuntimeError("Model file not found. Provide pd_model.ts or pd_model.safetensors")
103
  model.eval()
104
  return model, scaler_x, scaler_y
105
 
106
  MODEL, SCALER_X, SCALER_Y = load_model_and_scalers()
107
 
108
+
109
  # -----------------------------
110
  # Utilities
111
  # -----------------------------