Spaces:
Running on Zero
Running on Zero
Download app.py from ANL2001/Housing_Price_API: direct link, hf CLI and curl.
- Browser
- Download file 7.65 kB
-
https://huggingface.co/spaces/ANL2001/Housing_Price_API/resolve/main/app.py
- Command line
-
hf download hf://spaces/ANL2001/Housing_Price_API/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ANL2001/Housing_Price_API/resolve/main/app.py
7.65 kB
| import os | |
| import numpy as np | |
| import pandas as pd | |
| import joblib | |
| import gradio as gr | |
| import spaces | |
| ARTIFACTS_DIR = os.path.join(os.path.dirname(__file__), "ml", "artifacts") | |
| processor = joblib.load(os.path.join(ARTIFACTS_DIR, "manual_processor.joblib")) | |
| stack = joblib.load(os.path.join(ARTIFACTS_DIR, "stack.joblib")) | |
| core_features = joblib.load(os.path.join(ARTIFACTS_DIR, "core_features.joblib")) | |
| def predict( | |
| GrLivArea, TotalBsmtSF, GarageArea, LotArea, LotFrontage, | |
| floor_1st, floor_2nd, WoodDeckSF, OpenPorchSF, | |
| OverallQual, OverallCond, KitchenQual, ExterQual, BsmtQual, | |
| HeatingQC, FireplaceQu, FullBath, HalfBath, BsmtFullBath, | |
| TotRmsAbvGrd, Fireplaces, YearBuilt, YearRemodAdd, YrSold, | |
| Neighborhood, MSZoning, BldgType, Foundation, GarageQual, GarageCond, Electrical, | |
| ): | |
| inputs = { | |
| "GrLivArea": float(GrLivArea), | |
| "TotalBsmtSF": float(TotalBsmtSF), | |
| "LotArea": float(LotArea), | |
| "GarageArea": float(GarageArea), | |
| "PoolArea": 0.0, | |
| "LotFrontage": float(LotFrontage) if LotFrontage else None, | |
| "2ndFlrSF": float(floor_2nd), | |
| "LowQualFinSF": 0.0, | |
| "BsmtUnfSF": 0.0, | |
| "1stFlrSF": float(floor_1st), | |
| "WoodDeckSF": float(WoodDeckSF), | |
| "OpenPorchSF": float(OpenPorchSF), | |
| "EnclosedPorch": 0.0, | |
| "3SsnPorch": 0.0, | |
| "ScreenPorch": 0.0, | |
| "FullBath": int(FullBath), | |
| "HalfBath": int(HalfBath), | |
| "BsmtFullBath": int(BsmtFullBath), | |
| "BsmtHalfBath": 0, | |
| "TotRmsAbvGrd": int(TotRmsAbvGrd), | |
| "Fireplaces": int(Fireplaces), | |
| "YearBuilt": int(YearBuilt), | |
| "YrSold": int(YrSold), | |
| "YearRemodAdd": int(YearRemodAdd), | |
| "OverallQual": int(OverallQual), | |
| "OverallCond": int(OverallCond), | |
| "HeatingQC": HeatingQC, | |
| "BsmtQual": None if BsmtQual == "None" else BsmtQual, | |
| "PoolQC": None, | |
| "ExterQual": ExterQual, | |
| "KitchenQual": KitchenQual, | |
| "Functional": "Typ", | |
| "FireplaceQu": None if FireplaceQu == "None" else FireplaceQu, | |
| "BsmtCond": "TA", | |
| "ExterCond": "TA", | |
| "Neighborhood": Neighborhood, | |
| "MSZoning": MSZoning, | |
| "MSSubClass": 20, | |
| "LandSlope": "Gtl", | |
| "Alley": np.nan, | |
| "LandContour": "Lvl", | |
| "BldgType": BldgType, | |
| "Condition1": "Norm", | |
| "RoofStyle": "Gable", | |
| "Foundation": Foundation, | |
| "SaleCondition": "Normal", | |
| "Exterior1st": "VinylSd", | |
| "Utilities": "AllPub", | |
| "Electrical": Electrical, | |
| "GarageQual": GarageQual, | |
| "GarageCond": GarageCond, | |
| } | |
| df = pd.DataFrame([inputs]) | |
| X = df[core_features].copy() | |
| X_processed = processor.transform(X) | |
| log_pred = stack.predict(X_processed) | |
| price = float(np.expm1(log_pred)[0]) | |
| return f"${price:,.0f}" | |
| with gr.Blocks(title="House Price Predictor") as demo: | |
| gr.Markdown("# 🏠 House Price Predictor") | |
| gr.Markdown("Stacking ensemble: GradientBoosting + CatBoost + KernelRidge · **Top 16 / 4,000+** on Kaggle Housing Prices Competition") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### Size") | |
| GrLivArea = gr.Slider(300, 6000, value=1500, step=50, label="Above-grade living area (sq ft)") | |
| TotalBsmtSF = gr.Slider(0, 3000, value=800, step=50, label="Total basement area (sq ft)") | |
| GarageArea = gr.Slider(0, 1500, value=400, step=50, label="Garage area (sq ft)") | |
| LotArea = gr.Slider(1000, 100000, value=8000, step=500, label="Lot area (sq ft)") | |
| LotFrontage = gr.Slider(0, 200, value=65, step=5, label="Lot frontage (ft) — 0 if unknown") | |
| gr.Markdown("### Floors & Extras") | |
| floor_1st = gr.Slider(0, 4000, value=856, step=50, label="1st floor area (sq ft)") | |
| floor_2nd = gr.Slider(0, 2000, value=0, step=50, label="2nd floor area (sq ft)") | |
| WoodDeckSF = gr.Slider(0, 800, value=0, step=10, label="Wood deck (sq ft)") | |
| OpenPorchSF = gr.Slider(0, 500, value=0, step=10, label="Open porch (sq ft)") | |
| with gr.Column(): | |
| gr.Markdown("### Quality & Condition") | |
| OverallQual = gr.Slider(1, 10, value=6, step=1, label="Overall quality (1–10)") | |
| OverallCond = gr.Slider(1, 9, value=5, step=1, label="Overall condition (1–9)") | |
| KitchenQual = gr.Dropdown(["Ex", "Gd", "TA", "Fa", "Po"], value="TA", label="Kitchen quality") | |
| ExterQual = gr.Dropdown(["Ex", "Gd", "TA", "Fa", "Po"], value="TA", label="Exterior quality") | |
| BsmtQual = gr.Dropdown(["Ex", "Gd", "TA", "Fa", "Po", "None"], value="TA", label="Basement quality") | |
| HeatingQC = gr.Dropdown(["Ex", "Gd", "TA", "Fa", "Po"], value="TA", label="Heating quality") | |
| FireplaceQu = gr.Dropdown(["None", "Ex", "Gd", "TA", "Fa", "Po"], value="None", label="Fireplace quality") | |
| gr.Markdown("### Bathrooms & Rooms") | |
| FullBath = gr.Slider(0, 4, value=2, step=1, label="Full baths (above grade)") | |
| HalfBath = gr.Slider(0, 2, value=0, step=1, label="Half baths (above grade)") | |
| BsmtFullBath = gr.Slider(0, 2, value=0, step=1, label="Basement full baths") | |
| TotRmsAbvGrd = gr.Slider(2, 14, value=7, step=1, label="Total rooms above grade") | |
| Fireplaces = gr.Slider(0, 4, value=0, step=1, label="Fireplaces") | |
| with gr.Column(): | |
| gr.Markdown("### Year & Location") | |
| YearBuilt = gr.Slider(1870, 2024, value=2000, step=1, label="Year built") | |
| YearRemodAdd = gr.Slider(1950, 2024, value=2000, step=1, label="Year remodelled") | |
| YrSold = gr.Slider(2006, 2030, value=2024, step=1, label="Year sold") | |
| Neighborhood = gr.Dropdown([ | |
| "NAmes", "CollgCr", "OldTown", "Edwards", "Somerst", "NridgHt", | |
| "Gilbert", "Sawyer", "NWAmes", "SawyerW", "BrkSide", "Crawfor", | |
| "Mitchel", "NoRidge", "Timber", "IDOTRR", "ClearCr", "StoneBr", | |
| "SWISU", "MeadowV", "Blmngtn", "BrDale", "Veenker", "NPkVill", "Blueste", | |
| ], value="NAmes", label="Neighborhood") | |
| MSZoning = gr.Dropdown(["RL", "RM", "FV", "RH", "C (all)"], value="RL", label="Zoning") | |
| BldgType = gr.Dropdown(["1Fam", "2fmCon", "Duplex", "TwnhsE", "Twnhs"], value="1Fam", label="Building type") | |
| Foundation = gr.Dropdown(["PConc", "CBlock", "BrkTil", "Wood", "Slab", "Stone"], value="PConc", label="Foundation") | |
| gr.Markdown("### Garage & Utilities") | |
| GarageQual = gr.Dropdown(["TA", "Gd", "Fa", "Ex", "Po"], value="TA", label="Garage quality") | |
| GarageCond = gr.Dropdown(["TA", "Gd", "Fa", "Ex", "Po"], value="TA", label="Garage condition") | |
| Electrical = gr.Dropdown(["SBrkr", "FuseA", "FuseF", "FuseP", "Mix"], value="SBrkr", label="Electrical system") | |
| predict_btn = gr.Button("Predict Price", variant="primary") | |
| output = gr.Text(label="Estimated Sale Price", text_align="center") | |
| predict_btn.click( | |
| fn=predict, | |
| inputs=[ | |
| GrLivArea, TotalBsmtSF, GarageArea, LotArea, LotFrontage, | |
| floor_1st, floor_2nd, WoodDeckSF, OpenPorchSF, | |
| OverallQual, OverallCond, KitchenQual, ExterQual, BsmtQual, | |
| HeatingQC, FireplaceQu, FullBath, HalfBath, BsmtFullBath, | |
| TotRmsAbvGrd, Fireplaces, YearBuilt, YearRemodAdd, YrSold, | |
| Neighborhood, MSZoning, BldgType, Foundation, GarageQual, GarageCond, Electrical, | |
| ], | |
| outputs=output, | |
| ) | |
| demo.launch() | |