Spaces:
Sleeping
Sleeping
Download app.py from sgampasani/Lab_04: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/sgampasani/Lab_04/resolve/main/app.py
- Command line
-
hf download hf://spaces/sgampasani/Lab_04/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sgampasani/Lab_04/resolve/main/app.py
3.31 kB
| import os | |
| import numpy as np | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.impute import SimpleImputer | |
| from sklearn.ensemble import RandomForestRegressor | |
| from sklearn.pipeline import Pipeline | |
| import gradio as gr | |
| # -------- 1) Load data -------- | |
| DATA_PATH = os.path.join(os.path.dirname(__file__), "housing.csv") | |
| df = pd.read_csv(DATA_PATH) | |
| # Features & target | |
| FEATURES = [ | |
| "longitude", "latitude", "housing_median_age", | |
| "total_rooms", "total_bedrooms", "population", | |
| "households", "median_income" | |
| ] | |
| TARGET = "median_house_value" | |
| X = df[FEATURES].copy() | |
| y = df[TARGET].astype(float).copy() | |
| # -------- 2) Train simple pipeline once at startup -------- | |
| # Impute missing numeric values with median, then RandomForest | |
| pipe = Pipeline([ | |
| ("imputer", SimpleImputer(strategy="median")), | |
| ("rf", RandomForestRegressor( | |
| n_estimators=200, | |
| random_state=42, | |
| n_jobs=-1 | |
| )) | |
| ]) | |
| # (Optional) Keep a small holdout just to avoid training on 100% of rows | |
| X_train, X_hold, y_train, y_hold = train_test_split(X, y, test_size=0.1, random_state=42) | |
| pipe.fit(X_train, y_train) | |
| # -------- 3) Predict function for Gradio -------- | |
| def predict_value(longitude, latitude, housing_median_age, | |
| total_rooms, total_bedrooms, population, | |
| households, median_income): | |
| row = np.array([[longitude, latitude, housing_median_age, | |
| total_rooms, total_bedrooms, population, | |
| households, median_income]], dtype=float) | |
| pred = float(pipe.predict(row)[0]) | |
| # Return both raw and formatted outputs for clarity | |
| return pred, f"${pred:,.0f}" | |
| # -------- 4) Build Gradio UI -------- | |
| inputs = [ | |
| gr.Slider(-125.0, -113.0, value=float(X["longitude"].median()), step=0.01, label="Longitude"), | |
| gr.Slider( 32.0, 43.0, value=float(X["latitude"].median()), step=0.01, label="Latitude"), | |
| gr.Slider( 1.0, 52.0, value=float(X["housing_median_age"].median()), step=1.0, label="Housing Median Age (years)"), | |
| gr.Number(value=float(X["total_rooms"].median()), label="Total Rooms"), | |
| gr.Number(value=float(X["total_bedrooms"].median()), label="Total Bedrooms"), | |
| gr.Number(value=float(X["population"].median()), label="Population"), | |
| gr.Number(value=float(X["households"].median()), label="Households"), | |
| gr.Slider(0.0, 15.0, value=float(X["median_income"].median()), step=0.1, label="Median Income (10k$ units)"), | |
| ] | |
| outputs = [ | |
| gr.Number(label="Predicted Median House Value (raw)"), | |
| gr.Textbox(label="Predicted Median House Value (formatted)") | |
| ] | |
| demo = gr.Interface( | |
| fn=predict_value, | |
| inputs=inputs, | |
| outputs=outputs, | |
| title="California Housing Price Prediction (Lab04)", | |
| description=( | |
| "Inputs map to the original California Housing dataset features. " | |
| "Model: RandomForestRegressor with median imputation. " | |
| "Prediction is the median house value in US dollars." | |
| ), | |
| examples=[ | |
| [-122.23, 37.88, 41, 880, 129, 322, 126, 8.3252], | |
| [-118.30, 34.10, 20, 1500, 300, 800, 350, 4.5000], | |
| ] | |
| ) | |
| if __name__ == "__main__": | |
| # share=True is handy in Colab; on Spaces it's not required. | |
| demo.launch() | |