Download src/predict.py from AL-mozahem/HRP-Stage06-Room-Rate-Prediction: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/AL-mozahem/HRP-Stage06-Room-Rate-Prediction/resolve/main/src/predict.py
- Command line
-
hf download hf://AL-mozahem/HRP-Stage06-Room-Rate-Prediction/src/predict.py
-
curl -L -o predict.py https://huggingface.co/AL-mozahem/HRP-Stage06-Room-Rate-Prediction/resolve/main/src/predict.py
1.83 kB
| from pathlib import Path | |
| import argparse,json | |
| import numpy as np | |
| import pandas as pd | |
| import joblib | |
| ROOT=Path(__file__).resolve().parents[1] | |
| MODELS=ROOT/"models" | |
| def load_artifacts(): | |
| metadata=json.loads((MODELS/"model_metadata.json").read_text(encoding="utf-8")) | |
| model=joblib.load(MODELS/"final_model.joblib") | |
| return metadata,model | |
| def predict_room_rate(features:dict)->dict: | |
| metadata,model=load_artifacts() | |
| required=metadata["feature_columns"] | |
| missing=[c for c in required if c not in features] | |
| if missing: | |
| raise ValueError(f"Missing required features: {missing}") | |
| row=pd.DataFrame([{c:features.get(c) for c in required}]) | |
| if metadata["model_input_kind"]=="simple_base_price_only": | |
| pred=float(model.predict(row[["base_price_per_night"]])[0]) | |
| else: | |
| pred=float(model.predict(row)[0]) | |
| pred=max(0.0,pred) | |
| base=float(features["base_price_per_night"]) | |
| delta=pred-base | |
| pct=(delta/base*100.0) if base>0 else None | |
| return { | |
| "suggested_price_per_night":round(pred,2), | |
| "currency":"USD", | |
| "base_price_per_night":round(base,2), | |
| "difference_from_base":round(delta,2), | |
| "percent_change_from_base":None if pct is None else round(pct,2), | |
| "model_name":metadata["selected_model"], | |
| "decision_support_only":True, | |
| "synthetic_training_data_warning":metadata["synthetic_training_data"], | |
| "production_warning":metadata["production_warning"] | |
| } | |
| def main(): | |
| parser=argparse.ArgumentParser() | |
| parser.add_argument("json_file") | |
| args=parser.parse_args() | |
| payload=json.loads(Path(args.json_file).read_text(encoding="utf-8")) | |
| print(json.dumps( | |
| predict_room_rate(payload.get("features",payload)), | |
| ensure_ascii=False,indent=2 | |
| )) | |
| if __name__=="__main__": | |
| main() | |