singhina commited on
Commit
6adbfc5
·
verified ·
1 Parent(s): 170a058

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +5 -12
app.py CHANGED
@@ -6,13 +6,10 @@ from pydantic import BaseModel
6
  from typing import Dict, Any
7
  from huggingface_hub import hf_hub_download
8
 
9
- # Hugging Face model repo to pull artifacts from
10
  MODEL_REPO_ID = "singhina/tourism-tuned-model"
 
11
 
12
- # Read token from environment (set this in Space → Settings → Secrets)
13
- HF_TOKEN = os.environ.get("HF_TOKEN")
14
-
15
- # Download model + features (token may be None if repo is public)
16
  model_file = hf_hub_download(repo_id=MODEL_REPO_ID, filename="best_xgb.json",
17
  repo_type="model", token=HF_TOKEN)
18
  features_file = hf_hub_download(repo_id=MODEL_REPO_ID, filename="features.txt",
@@ -22,7 +19,7 @@ features_file = hf_hub_download(repo_id=MODEL_REPO_ID, filename="features.txt",
22
  with open(features_file, "r") as f:
23
  FEATURE_NAMES = [line.strip() for line in f if line.strip()]
24
 
25
- # Load XGBoost booster
26
  booster = xgb.Booster()
27
  booster.load_model(model_file)
28
 
@@ -43,18 +40,14 @@ class CustomerInput(BaseModel):
43
  def predict(input: CustomerInput):
44
  row = input.data
45
 
46
- # Build a full feature row (missing columns default to 0.0)
47
  full_row = {name: 0.0 for name in FEATURE_NAMES}
48
  for k, v in row.items():
49
  if k in full_row:
50
  try:
51
  full_row[k] = float(v)
52
  except Exception:
53
- # basic coercion for booleans / strings
54
- try:
55
- full_row[k] = float(str(v).strip().lower() in ["true","1","yes"])
56
- except Exception:
57
- full_row[k] = 0.0
58
 
59
  df = pd.DataFrame([full_row])[FEATURE_NAMES].astype(float)
60
  dmat = xgb.DMatrix(df)
 
6
  from typing import Dict, Any
7
  from huggingface_hub import hf_hub_download
8
 
 
9
  MODEL_REPO_ID = "singhina/tourism-tuned-model"
10
+ HF_TOKEN = os.environ.get("HF_TOKEN") # read token from Space secret
11
 
12
+ # Download model + features from HF Hub
 
 
 
13
  model_file = hf_hub_download(repo_id=MODEL_REPO_ID, filename="best_xgb.json",
14
  repo_type="model", token=HF_TOKEN)
15
  features_file = hf_hub_download(repo_id=MODEL_REPO_ID, filename="features.txt",
 
19
  with open(features_file, "r") as f:
20
  FEATURE_NAMES = [line.strip() for line in f if line.strip()]
21
 
22
+ # Load trained booster
23
  booster = xgb.Booster()
24
  booster.load_model(model_file)
25
 
 
40
  def predict(input: CustomerInput):
41
  row = input.data
42
 
43
+ # Fill missing features with 0.0
44
  full_row = {name: 0.0 for name in FEATURE_NAMES}
45
  for k, v in row.items():
46
  if k in full_row:
47
  try:
48
  full_row[k] = float(v)
49
  except Exception:
50
+ full_row[k] = float(str(v).strip().lower() in ["true","1","yes"])
 
 
 
 
51
 
52
  df = pd.DataFrame([full_row])[FEATURE_NAMES].astype(float)
53
  dmat = xgb.DMatrix(df)