niru-nny commited on
Commit
13c4e9f
·
verified ·
1 Parent(s): 161bacb

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
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
+
10
+ # Download model files
11
+ model_path = hf_hub_download(
12
+ repo_id="niru-nny/house-price-prediction",
13
+ filename="house_price_model.joblib"
14
+ )
15
+ pipeline_path = hf_hub_download(
16
+ repo_id="niru-nny/house-price-prediction",
17
+ filename="preprocessing_pipeline.joblib"
18
+ )
19
+
20
+ # Load model and pipeline
21
+ model = joblib.load(model_path)
22
+ pipeline = joblib.load(pipeline_path)
23
+
24
+ def predict_price(longitude, latitude, housing_median_age, total_rooms,
25
+ total_bedrooms, population, households, median_income,
26
+ ocean_proximity):
27
+ """Predict house price based on input features"""
28
+
29
+ # Create input dataframe
30
+ input_data = pd.DataFrame({
31
+ 'longitude': [longitude],
32
+ 'latitude': [latitude],
33
+ 'housing_median_age': [housing_median_age],
34
+ 'total_rooms': [total_rooms],
35
+ 'total_bedrooms': [total_bedrooms],
36
+ 'population': [population],
37
+ 'households': [households],
38
+ 'median_income': [median_income],
39
+ 'ocean_proximity': [ocean_proximity]
40
+ })
41
+
42
+ # Preprocess and predict
43
+ processed_data = pipeline.transform(input_data)
44
+ prediction = model.predict(processed_data)[0]
45
+
46
+ return f"${prediction:,.2f}"
47
+
48
+ # Create Gradio interface
49
+ demo = gr.Interface(
50
+ fn=predict_price,
51
+ inputs=[
52
+ gr.Slider(-124.5, -114.0, value=-122.23, label="Longitude"),
53
+ gr.Slider(32.5, 42.0, value=37.88, label="Latitude"),
54
+ gr.Slider(0, 52, value=41, step=1, label="Housing Median Age"),
55
+ gr.Slider(0, 40000, value=880, step=10, label="Total Rooms"),
56
+ gr.Slider(0, 6500, value=129, step=1, label="Total Bedrooms"),
57
+ gr.Slider(0, 35000, value=322, step=1, label="Population"),
58
+ gr.Slider(0, 6000, value=126, step=1, label="Households"),
59
+ gr.Slider(0, 15, value=8.3252, step=0.1, label="Median Income (in $10,000s)"),
60
+ gr.Dropdown(
61
+ choices=["NEAR BAY", "INLAND", "<1H OCEAN", "NEAR OCEAN", "ISLAND"],
62
+ value="NEAR BAY",
63
+ label="Ocean Proximity"
64
+ )
65
+ ],
66
+ outputs=gr.Textbox(label="Predicted House Price"),
67
+ title="🏠 California House Price Prediction",
68
+ description="Predict California house prices based on location and features",
69
+ examples=[
70
+ [-122.23, 37.88, 41, 880, 129, 322, 126, 8.3252, "NEAR BAY"],
71
+ [-121.22, 39.43, 7, 1430, 244, 515, 226, 3.8462, "INLAND"],
72
+ ]
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ demo.launch()