Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,16 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
|
|
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
with open("apartment_price_model.pkl", mode="rb") as f:
|
| 8 |
model = pickle.load(f)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def predict_price(neighborhood, rooms, area, has_balcony, is_renovated):
|
| 11 |
# Default values for other features
|
| 12 |
pop = 420217
|
|
@@ -16,6 +21,9 @@ def predict_price(neighborhood, rooms, area, has_balcony, is_renovated):
|
|
| 16 |
tax_income = 85446
|
| 17 |
price_per_room = rooms / area if area != 0 else 0
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Create input dataframe
|
| 20 |
input_data = pd.DataFrame([{
|
| 21 |
'rooms': rooms,
|
|
@@ -28,7 +36,7 @@ def predict_price(neighborhood, rooms, area, has_balcony, is_renovated):
|
|
| 28 |
'price_per_room': price_per_room,
|
| 29 |
'has_balcony': 1 if has_balcony else 0,
|
| 30 |
'is_renovated': 1 if is_renovated else 0,
|
| 31 |
-
'neighborhood':
|
| 32 |
}])
|
| 33 |
|
| 34 |
# Define features in the correct order
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
+
from sklearn.preprocessing import LabelEncoder
|
| 6 |
|
| 7 |
# Load the model
|
| 8 |
with open("apartment_price_model.pkl", mode="rb") as f:
|
| 9 |
model = pickle.load(f)
|
| 10 |
|
| 11 |
+
# Load the label encoder for neighborhoods
|
| 12 |
+
with open("neighborhood_encoder.pkl", mode="rb") as f:
|
| 13 |
+
neighborhood_encoder = pickle.load(f)
|
| 14 |
+
|
| 15 |
def predict_price(neighborhood, rooms, area, has_balcony, is_renovated):
|
| 16 |
# Default values for other features
|
| 17 |
pop = 420217
|
|
|
|
| 21 |
tax_income = 85446
|
| 22 |
price_per_room = rooms / area if area != 0 else 0
|
| 23 |
|
| 24 |
+
# Encode the neighborhood
|
| 25 |
+
neighborhood_encoded = neighborhood_encoder.transform([neighborhood])[0]
|
| 26 |
+
|
| 27 |
# Create input dataframe
|
| 28 |
input_data = pd.DataFrame([{
|
| 29 |
'rooms': rooms,
|
|
|
|
| 36 |
'price_per_room': price_per_room,
|
| 37 |
'has_balcony': 1 if has_balcony else 0,
|
| 38 |
'is_renovated': 1 if is_renovated else 0,
|
| 39 |
+
'neighborhood': neighborhood_encoded
|
| 40 |
}])
|
| 41 |
|
| 42 |
# Define features in the correct order
|