Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,23 +2,23 @@
|
|
| 2 |
Gradio app for House Price Prediction Model
|
| 3 |
Deploy this to Hugging Face Spaces for interactive inference
|
| 4 |
"""
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import joblib
|
| 7 |
import pandas as pd
|
| 8 |
-
from huggingface_hub import hf_hub_download
|
| 9 |
-
import
|
| 10 |
|
| 11 |
print("π Downloading model files...")
|
| 12 |
|
| 13 |
# Download model files
|
| 14 |
try:
|
| 15 |
-
model_path = hf_hub_download(
|
| 16 |
repo_id="niru-nny/house-price-prediction",
|
| 17 |
filename="house_price_model.joblib"
|
| 18 |
)
|
| 19 |
print(f"β
Model downloaded: {model_path}")
|
| 20 |
|
| 21 |
-
pipeline_path = hf_hub_download(
|
| 22 |
repo_id="niru-nny/house-price-prediction",
|
| 23 |
filename="preprocessing_pipeline.joblib"
|
| 24 |
)
|
|
@@ -26,17 +26,25 @@ try:
|
|
| 26 |
|
| 27 |
# Load model and pipeline
|
| 28 |
print("π Loading model and pipeline...")
|
| 29 |
-
model = joblib.load(model_path)
|
| 30 |
-
pipeline = joblib.load(pipeline_path)
|
| 31 |
print("β
Model and pipeline loaded successfully!")
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
print(f"β Error loading model: {e}")
|
| 35 |
raise
|
| 36 |
|
| 37 |
-
def predict_price(
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"""Predict house price based on input features"""
|
| 41 |
|
| 42 |
# Create input dataframe
|
|
@@ -53,13 +61,13 @@ def predict_price(longitude, latitude, housing_median_age, total_rooms,
|
|
| 53 |
})
|
| 54 |
|
| 55 |
# Preprocess and predict
|
| 56 |
-
processed_data = pipeline.transform(input_data)
|
| 57 |
-
prediction = model.predict(processed_data)[0]
|
| 58 |
|
| 59 |
return f"${prediction:,.2f}"
|
| 60 |
|
| 61 |
# Create Gradio interface
|
| 62 |
-
demo = gr.Interface(
|
| 63 |
fn=predict_price,
|
| 64 |
inputs=[
|
| 65 |
gr.Slider(-124.5, -114.0, value=-122.23, label="Longitude"),
|
|
|
|
| 2 |
Gradio app for House Price Prediction Model
|
| 3 |
Deploy this to Hugging Face Spaces for interactive inference
|
| 4 |
"""
|
| 5 |
+
import gradio as gr # type: ignore
|
| 6 |
+
import joblib # type: ignore
|
| 7 |
import pandas as pd
|
| 8 |
+
from huggingface_hub import hf_hub_download # type: ignore
|
| 9 |
+
from typing import Any
|
| 10 |
|
| 11 |
print("π Downloading model files...")
|
| 12 |
|
| 13 |
# Download model files
|
| 14 |
try:
|
| 15 |
+
model_path: str = hf_hub_download( # type: ignore
|
| 16 |
repo_id="niru-nny/house-price-prediction",
|
| 17 |
filename="house_price_model.joblib"
|
| 18 |
)
|
| 19 |
print(f"β
Model downloaded: {model_path}")
|
| 20 |
|
| 21 |
+
pipeline_path: str = hf_hub_download( # type: ignore
|
| 22 |
repo_id="niru-nny/house-price-prediction",
|
| 23 |
filename="preprocessing_pipeline.joblib"
|
| 24 |
)
|
|
|
|
| 26 |
|
| 27 |
# Load model and pipeline
|
| 28 |
print("π Loading model and pipeline...")
|
| 29 |
+
model: Any = joblib.load(model_path) # type: ignore
|
| 30 |
+
pipeline: Any = joblib.load(pipeline_path) # type: ignore
|
| 31 |
print("β
Model and pipeline loaded successfully!")
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
print(f"β Error loading model: {e}")
|
| 35 |
raise
|
| 36 |
|
| 37 |
+
def predict_price(
|
| 38 |
+
longitude: float,
|
| 39 |
+
latitude: float,
|
| 40 |
+
housing_median_age: int,
|
| 41 |
+
total_rooms: int,
|
| 42 |
+
total_bedrooms: int,
|
| 43 |
+
population: int,
|
| 44 |
+
households: int,
|
| 45 |
+
median_income: float,
|
| 46 |
+
ocean_proximity: str
|
| 47 |
+
) -> str:
|
| 48 |
"""Predict house price based on input features"""
|
| 49 |
|
| 50 |
# Create input dataframe
|
|
|
|
| 61 |
})
|
| 62 |
|
| 63 |
# Preprocess and predict
|
| 64 |
+
processed_data: Any = pipeline.transform(input_data) # type: ignore
|
| 65 |
+
prediction: float = model.predict(processed_data)[0] # type: ignore
|
| 66 |
|
| 67 |
return f"${prediction:,.2f}"
|
| 68 |
|
| 69 |
# Create Gradio interface
|
| 70 |
+
demo: Any = gr.Interface( # type: ignore
|
| 71 |
fn=predict_price,
|
| 72 |
inputs=[
|
| 73 |
gr.Slider(-124.5, -114.0, value=-122.23, label="Longitude"),
|