|
|
|
|
|
|
|
|
import gradio as gr |
|
|
import joblib |
|
|
import pandas as pd |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
model = joblib.load("housing_model.joblib") |
|
|
model_columns = joblib.load("model_columns.joblib") |
|
|
except FileNotFoundError: |
|
|
|
|
|
model = joblib.load(os.path.join(os.path.dirname(__file__), "housing_model.joblib")) |
|
|
model_columns = joblib.load(os.path.join(os.path.dirname(__file__), "model_columns.joblib")) |
|
|
|
|
|
|
|
|
|
|
|
def predict_price(sqft, bedrooms, house_age, condition, year_sold, interest_rate, region, sub_type, style, has_garage, has_pool): |
|
|
|
|
|
|
|
|
input_data = { |
|
|
'SquareFootage': sqft, 'Bedrooms': bedrooms, 'HouseAge': house_age, |
|
|
'PropertyCondition': condition, 'HasGarage': has_garage, 'HasPool': has_pool, |
|
|
'YearSold': year_sold, 'InterestRate': interest_rate, |
|
|
'Region': region, 'SubType': sub_type, 'ArchitecturalStyle': style |
|
|
} |
|
|
input_df = pd.DataFrame([input_data]) |
|
|
|
|
|
|
|
|
input_processed = pd.get_dummies(input_df) |
|
|
final_input = input_processed.reindex(columns=model_columns, fill_value=0) |
|
|
|
|
|
|
|
|
predicted_price = model.predict(final_input)[0] |
|
|
|
|
|
|
|
|
return f"${predicted_price:,.0f}" |
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=predict_price, |
|
|
inputs=[ |
|
|
gr.Number(label="Square Footage", value=2500), |
|
|
gr.Number(label="Bedrooms", value=4), |
|
|
gr.Number(label="House Age (years)", value=15), |
|
|
gr.Slider(label="Property Condition", minimum=1, maximum=10, step=1, value=8), |
|
|
gr.Number(label="Year Sold", value=2024), |
|
|
gr.Number(label="Interest Rate (%)", value=5.5), |
|
|
gr.Radio(['Sunbelt', 'Pacific Northwest', 'Rust Belt', 'New England', 'Mountain West'], label="Region", value="Sunbelt"), |
|
|
gr.Radio(['Urban', 'Suburban', 'Rural', 'Historic District'], label="Sub-Type", value="Suburban"), |
|
|
gr.Radio(['Modern', 'Ranch', 'Colonial', 'Craftsman', 'Victorian'], label="Architectural Style", value="Colonial"), |
|
|
gr.Checkbox(label="Has Garage?", value=True), |
|
|
gr.Checkbox(label="Has Pool?", value=False) |
|
|
], |
|
|
outputs=gr.Textbox(label="Predicted Price"), |
|
|
title="AI House Price Predictor", |
|
|
description="Describe a property, and our AI will estimate its market value. Powered by a model trained on 9.2GB of simulated data." |
|
|
) |
|
|
|
|
|
|
|
|
demo.launch() |