Spaces:
Running on Zero
Running on Zero
File size: 7,650 Bytes
e5bb0bb 9c8483c e5bb0bb 9c8483c e5bb0bb c4aaa2b e5bb0bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 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"))
@spaces.GPU
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()
|