Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
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 |
-
#
|
| 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
|
| 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 |
-
#
|
| 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 |
-
|
| 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)
|