Spaces:
Sleeping
Sleeping
| """ | |
| House Price Predictor - HuggingFace Space | |
| Standalone Gradio application for Seattle/King County house price prediction | |
| """ | |
| import gradio as gr | |
| import pandas as pd | |
| import plotly.express as px | |
| import joblib | |
| from pathlib import Path | |
| # Paths (files in same directory as app.py) | |
| MODEL_PATH = Path("house_price_model.joblib") | |
| DATA_PATH = Path("kc_house_data.csv") | |
| class HousePricePredictor: | |
| def __init__(self): | |
| self.model = joblib.load(MODEL_PATH) | |
| def predict(self, bedrooms, bathrooms, sqft, age): | |
| X = pd.DataFrame([[bedrooms, bathrooms, sqft, age]], | |
| columns=['bedrooms', 'bathrooms', 'sqft', 'age']) | |
| price = self.model.predict(X)[0] | |
| return f"${price:,.2f}" | |
| predictor = HousePricePredictor() | |
| def create_price_map(min_price, max_price, sample_size): | |
| """Create interactive map of house prices""" | |
| df = pd.read_csv(DATA_PATH) | |
| # Filter by price range | |
| df_filtered = df[(df['price'] >= min_price) & (df['price'] <= max_price)] | |
| # Limit sample size for performance | |
| if len(df_filtered) > sample_size: | |
| df_filtered = df_filtered.sample(n=sample_size, random_state=42) | |
| # Create the map | |
| fig = px.scatter_mapbox( | |
| df_filtered, | |
| lat='lat', | |
| lon='long', | |
| color='price', | |
| size='sqft_living', | |
| hover_data={ | |
| 'price': ':$,.0f', | |
| 'bedrooms': True, | |
| 'bathrooms': True, | |
| 'sqft_living': ':,', | |
| 'waterfront': True, | |
| 'lat': ':.4f', | |
| 'long': ':.4f' | |
| }, | |
| color_continuous_scale='Viridis', | |
| zoom=9, | |
| height=500, | |
| title=f'Seattle/King County House Prices ({len(df_filtered):,} houses shown)' | |
| ) | |
| fig.update_layout( | |
| mapbox_style="open-street-map", | |
| margin={"r":0,"t":40,"l":0,"b":0} | |
| ) | |
| return fig | |
| def predict_price(bedrooms, bathrooms, sqft, age): | |
| """Make a price prediction""" | |
| return predictor.predict(bedrooms, bathrooms, sqft, age) | |
| # Build Gradio Interface | |
| with gr.Blocks() as demo: | |
| # Custom CSS for Helvetica Neue font | |
| gr.HTML(""" | |
| <style> | |
| * { | |
| font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif !important; | |
| } | |
| </style> | |
| """) | |
| gr.Markdown( | |
| """ | |
| # ๐ House Price Predictor | |
| ### AI-Powered Real Estate Valuation - Seattle/King County | |
| Trained on **21,613 real house sales** from King County, Washington (2014-2015). | |
| """ | |
| ) | |
| # Map Visualization Section | |
| gr.Markdown("## ๐บ๏ธ Explore House Prices on Map") | |
| with gr.Row(): | |
| min_price_filter = gr.Slider( | |
| minimum=0, | |
| maximum=8000000, | |
| value=0, | |
| step=100000, | |
| label="Min Price ($)", | |
| info="Minimum price" | |
| ) | |
| max_price_filter = gr.Slider( | |
| minimum=0, | |
| maximum=8000000, | |
| value=2000000, | |
| step=100000, | |
| label="Max Price ($)", | |
| info="Maximum price" | |
| ) | |
| sample_slider = gr.Slider( | |
| minimum=100, | |
| maximum=2000, | |
| value=1000, | |
| step=100, | |
| label="Houses to Display", | |
| info="More = slower" | |
| ) | |
| update_map_btn = gr.Button("๐ Update Map", variant="secondary", size="sm") | |
| map_plot = gr.Plot(label="Interactive Price Map") | |
| # Load initial map | |
| demo.load( | |
| fn=lambda: create_price_map(0, 2000000, 1000), | |
| outputs=map_plot | |
| ) | |
| # Update map on button click | |
| update_map_btn.click( | |
| fn=create_price_map, | |
| inputs=[min_price_filter, max_price_filter, sample_slider], | |
| outputs=map_plot | |
| ) | |
| gr.Markdown("---") | |
| # Price Prediction Section | |
| gr.Markdown("## ๐ฎ Predict House Price") | |
| with gr.Row(): | |
| with gr.Column(): | |
| bedrooms = gr.Slider(1, 10, value=3, step=1, label="Bedrooms") | |
| bathrooms = gr.Slider(1, 10, value=2, step=1, label="Bathrooms") | |
| sqft = gr.Slider(500, 10000, value=2000, step=100, label="Square Feet") | |
| age = gr.Slider(0, 100, value=5, step=1, label="Age (years)") | |
| predict_btn = gr.Button("๐ฎ Predict Price", variant="primary", size="lg") | |
| with gr.Column(): | |
| output = gr.Textbox( | |
| label="Predicted Price", | |
| placeholder="Click 'Predict Price' to see the result", | |
| scale=2 | |
| ) | |
| gr.Markdown( | |
| """ | |
| ### Example Houses: | |
| - **Starter Home**: 2br, 1ba, 1200 sqft, 15 years | |
| - **Family Home**: 4br, 3ba, 2500 sqft, 10 years | |
| - **Luxury Home**: 5br, 4ba, 4000 sqft, 2 years | |
| """ | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| [2, 1, 1200, 15], # Starter | |
| [3, 2, 2000, 5], # Average | |
| [4, 3, 2500, 10], # Family | |
| [5, 4, 4000, 2], # Luxury | |
| ], | |
| inputs=[bedrooms, bathrooms, sqft, age], | |
| label="Quick Examples" | |
| ) | |
| predict_btn.click( | |
| fn=predict_price, | |
| inputs=[bedrooms, bathrooms, sqft, age], | |
| outputs=output | |
| ) | |
| gr.Markdown( | |
| """ | |
| --- | |
| **Dataset:** 21,613 King County house sales (2014-2015) | |
| **Model:** Random Forest (Rยฒ Score: 0.53 on test data) | |
| **Architecture:** Gradio Frontend โ ML Model (scikit-learn) | |
| """ | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |