| import gradio as gr |
| import pandas as pd |
| import joblib as jb |
|
|
| |
| |
| MODEL_PATH = "HousePricePredictorPipeline.pkl" |
| pipe = jb.load(MODEL_PATH) |
|
|
| |
| NUM_FEATURES = ["area","parking","bedrooms","bathrooms","stories"] |
| CAT_FEATURES = ["furnishingstatus","mainroad","guestroom","basement", |
| "hotwaterheating","airconditioning","prefarea"] |
|
|
| ALL_COLUMNS = NUM_FEATURES + CAT_FEATURES |
|
|
| YES_NO = ["yes","no"] |
| FURNISHING = ["unfurnished","semi-furnished","furnished"] |
|
|
| def predict_price(area, parking, bedrooms, bathrooms, stories, |
| furnishingstatus, mainroad, guestroom, basement, |
| hotwaterheating, airconditioning, prefarea): |
| |
| row = { |
| "area": area, |
| "parking": int(parking), |
| "bedrooms": int(bedrooms), |
| "bathrooms": int(bathrooms), |
| "stories": int(stories), |
| "furnishingstatus": furnishingstatus, |
| "mainroad": mainroad, |
| "guestroom": guestroom, |
| "basement": basement, |
| "hotwaterheating": hotwaterheating, |
| "airconditioning": airconditioning, |
| "prefarea": prefarea |
| } |
| X = pd.DataFrame([row], columns=ALL_COLUMNS) |
| pred = pipe.predict(X)[0] |
| return float(pred) |
|
|
| with gr.Blocks(title="House Price Predictor") as demo: |
| gr.Markdown("# 🏠 House Price Predictor") |
| gr.Markdown( |
| "Provide home features and get an estimated price. " |
| "This app uses your trained scikit-learn pipeline." |
| ) |
|
|
| with gr.Row(): |
| with gr.Column(): |
| area = gr.Number(label="Area (sq ft)", value=2000, precision=0) |
| parking = gr.Slider(label="Parking Spots", value=1, minimum=0, maximum=5, step=1) |
| bedrooms = gr.Slider(label="Bedrooms", value=3, minimum=0, maximum=10, step=1) |
| bathrooms = gr.Slider(label="Bathrooms", value=2, minimum=0, maximum=10, step=1) |
| stories = gr.Slider(label="Stories", value=2, minimum=0, maximum=10, step=1) |
|
|
| with gr.Column(): |
| furnishingstatus = gr.Dropdown(FURNISHING, value="semi-furnished", label="Furnishing Status") |
| mainroad = gr.Dropdown(YES_NO, value="yes", label="On Main Road?") |
| guestroom = gr.Dropdown(YES_NO, value="no", label="Guest Room?") |
| basement = gr.Dropdown(YES_NO, value="no", label="Basement?") |
| hotwaterheating = gr.Dropdown(YES_NO, value="no", label="Hot Water Heating?") |
| airconditioning = gr.Dropdown(YES_NO, value="yes", label="Air Conditioning?") |
| prefarea = gr.Dropdown(YES_NO, value="no", label="Preferred Area?") |
|
|
| btn = gr.Button("Predict Price") |
| output = gr.Number(label="Predicted Price (same units as your training data)") |
|
|
| btn.click( |
| fn=predict_price, |
| inputs=[area, parking, bedrooms, bathrooms, stories, |
| furnishingstatus, mainroad, guestroom, basement, |
| hotwaterheating, airconditioning, prefarea], |
| outputs=output |
| ) |
|
|
| gr.Markdown( |
| "Tip: Ensure **HousePricePredictorPipeline.pkl** is in the same folder.\n" |
| "Run with: `python gradio_app.py` and open the link in your browser." |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|