Upload app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,13 @@ import numpy as np
|
|
| 4 |
import pickle
|
| 5 |
from math import radians, cos, sin, asin, sqrt
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
with open("apartment_price_model.pkl", mode="rb") as f:
|
| 9 |
model = pickle.load(f)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
'price_per_room', 'distance_to_center', 'has_balcony', 'is_renovated'
|
| 15 |
-
]
|
| 16 |
|
| 17 |
# Function to calculate distance between two points using Haversine formula
|
| 18 |
def haversine_distance(lat1, lon1, lat2, lon2):
|
|
@@ -28,65 +26,58 @@ def haversine_distance(lat1, lon1, lat2, lon2):
|
|
| 28 |
r = 6371 # Radius of earth in kilometers
|
| 29 |
return c * r
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
zurich_center_lon = 8.5417
|
| 34 |
-
|
| 35 |
-
def predict_price(rooms, area, population, pop_density, foreign_pct,
|
| 36 |
-
employment, tax_income, lat, lon, has_balcony, is_renovated):
|
| 37 |
-
|
| 38 |
-
# Calculate derived features
|
| 39 |
-
price_per_room = 0 # This will be estimated by the model
|
| 40 |
distance_to_center = haversine_distance(lat, lon, zurich_center_lat, zurich_center_lon)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Create input dataframe
|
| 43 |
input_data = pd.DataFrame([{
|
| 44 |
'rooms': rooms,
|
| 45 |
'area': area,
|
| 46 |
-
'pop':
|
| 47 |
-
'pop_dens':
|
| 48 |
-
'frg_pct':
|
| 49 |
-
'emp':
|
| 50 |
'tax_income': tax_income,
|
| 51 |
'price_per_room': price_per_room,
|
| 52 |
'distance_to_center': distance_to_center,
|
| 53 |
-
'has_balcony': has_balcony,
|
| 54 |
-
'is_renovated': is_renovated
|
| 55 |
}])
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
input_data['price_per_room'] = price_per_room
|
| 63 |
|
| 64 |
-
# Make
|
| 65 |
predicted_price = model.predict(input_data[features])[0]
|
| 66 |
|
| 67 |
# Format the result
|
| 68 |
result = f"Predicted Monthly Rent: CHF {predicted_price:.0f}"
|
| 69 |
-
|
| 70 |
-
# Additional insights
|
| 71 |
result += f"\n\nProperty Details:"
|
| 72 |
result += f"\n- {rooms} rooms, {area} m²"
|
| 73 |
result += f"\n- {distance_to_center:.2f} km from city center"
|
| 74 |
result += f"\n- {'Has balcony' if has_balcony else 'No balcony'}"
|
| 75 |
-
result += f"\n- {'Renovated' if is_renovated else 'Not
|
| 76 |
|
| 77 |
return result
|
| 78 |
|
| 79 |
-
# Create Gradio interface
|
| 80 |
demo = gr.Interface(
|
| 81 |
fn=predict_price,
|
| 82 |
inputs=[
|
| 83 |
gr.Number(label="Number of Rooms"),
|
| 84 |
gr.Number(label="Area (m²)"),
|
| 85 |
-
gr.Number(label="Population", value=420217),
|
| 86 |
-
gr.Number(label="Population Density", value=4778),
|
| 87 |
-
gr.Number(label="Foreign Percentage", value=32.45),
|
| 88 |
-
gr.Number(label="Employment", value=491193),
|
| 89 |
-
gr.Number(label="Tax Income", value=85446),
|
| 90 |
gr.Number(label="Latitude", value=47.3769),
|
| 91 |
gr.Number(label="Longitude", value=8.5417),
|
| 92 |
gr.Checkbox(label="Has Balcony"),
|
|
@@ -94,12 +85,22 @@ demo = gr.Interface(
|
|
| 94 |
],
|
| 95 |
outputs="text",
|
| 96 |
examples=[
|
| 97 |
-
[3.5, 75,
|
| 98 |
-
[2.0, 60,
|
| 99 |
-
[4.5, 120,
|
| 100 |
],
|
| 101 |
title="Zurich Apartment Rent Prediction",
|
| 102 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
demo.launch()
|
|
|
|
| 4 |
import pickle
|
| 5 |
from math import radians, cos, sin, asin, sqrt
|
| 6 |
|
| 7 |
+
# Load the model
|
| 8 |
with open("apartment_price_model.pkl", mode="rb") as f:
|
| 9 |
model = pickle.load(f)
|
| 10 |
|
| 11 |
+
# Zurich city center coordinates
|
| 12 |
+
zurich_center_lat = 47.3769
|
| 13 |
+
zurich_center_lon = 8.5417
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Function to calculate distance between two points using Haversine formula
|
| 16 |
def haversine_distance(lat1, lon1, lat2, lon2):
|
|
|
|
| 26 |
r = 6371 # Radius of earth in kilometers
|
| 27 |
return c * r
|
| 28 |
|
| 29 |
+
def predict_price(rooms, area, lat, lon, has_balcony, is_renovated):
|
| 30 |
+
# Calculate special feature: distance to city center
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
distance_to_center = haversine_distance(lat, lon, zurich_center_lat, zurich_center_lon)
|
| 32 |
|
| 33 |
+
# Default values for other features
|
| 34 |
+
pop = 420217
|
| 35 |
+
pop_dens = 4778
|
| 36 |
+
frg_pct = 32.45
|
| 37 |
+
emp = 491193
|
| 38 |
+
tax_income = 85446
|
| 39 |
+
price_per_room = 0 # This will be updated by the model
|
| 40 |
+
|
| 41 |
# Create input dataframe
|
| 42 |
input_data = pd.DataFrame([{
|
| 43 |
'rooms': rooms,
|
| 44 |
'area': area,
|
| 45 |
+
'pop': pop,
|
| 46 |
+
'pop_dens': pop_dens,
|
| 47 |
+
'frg_pct': frg_pct,
|
| 48 |
+
'emp': emp,
|
| 49 |
'tax_income': tax_income,
|
| 50 |
'price_per_room': price_per_room,
|
| 51 |
'distance_to_center': distance_to_center,
|
| 52 |
+
'has_balcony': 1 if has_balcony else 0,
|
| 53 |
+
'is_renovated': 1 if is_renovated else 0
|
| 54 |
}])
|
| 55 |
|
| 56 |
+
# Define features in the correct order
|
| 57 |
+
features = [
|
| 58 |
+
'rooms', 'area', 'pop', 'pop_dens', 'frg_pct', 'emp', 'tax_income',
|
| 59 |
+
'price_per_room', 'distance_to_center', 'has_balcony', 'is_renovated'
|
| 60 |
+
]
|
|
|
|
| 61 |
|
| 62 |
+
# Make prediction
|
| 63 |
predicted_price = model.predict(input_data[features])[0]
|
| 64 |
|
| 65 |
# Format the result
|
| 66 |
result = f"Predicted Monthly Rent: CHF {predicted_price:.0f}"
|
|
|
|
|
|
|
| 67 |
result += f"\n\nProperty Details:"
|
| 68 |
result += f"\n- {rooms} rooms, {area} m²"
|
| 69 |
result += f"\n- {distance_to_center:.2f} km from city center"
|
| 70 |
result += f"\n- {'Has balcony' if has_balcony else 'No balcony'}"
|
| 71 |
+
result += f"\n- {'Renovated' if is_renovated else 'Not renovated'}"
|
| 72 |
|
| 73 |
return result
|
| 74 |
|
| 75 |
+
# Create Gradio interface with fewer inputs
|
| 76 |
demo = gr.Interface(
|
| 77 |
fn=predict_price,
|
| 78 |
inputs=[
|
| 79 |
gr.Number(label="Number of Rooms"),
|
| 80 |
gr.Number(label="Area (m²)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
gr.Number(label="Latitude", value=47.3769),
|
| 82 |
gr.Number(label="Longitude", value=8.5417),
|
| 83 |
gr.Checkbox(label="Has Balcony"),
|
|
|
|
| 85 |
],
|
| 86 |
outputs="text",
|
| 87 |
examples=[
|
| 88 |
+
[3.5, 75, 47.41106, 8.54654, True, True],
|
| 89 |
+
[2.0, 60, 47.37624, 8.52814, False, False],
|
| 90 |
+
[4.5, 120, 47.36368, 8.54678, True, False],
|
| 91 |
],
|
| 92 |
title="Zurich Apartment Rent Prediction",
|
| 93 |
+
description="""
|
| 94 |
+
This app predicts apartment rental prices in Zurich with a special feature: Distance to City Center.
|
| 95 |
+
|
| 96 |
+
**Special Feature Description:**
|
| 97 |
+
The app automatically calculates the apartment's distance from Zurich city center using the Haversine formula.
|
| 98 |
+
This distance is a critical factor in real estate pricing - properties closer to the city center typically
|
| 99 |
+
command higher rents due to convenience and accessibility to urban amenities.
|
| 100 |
+
|
| 101 |
+
Simply enter the apartment's latitude and longitude, and the model will incorporate this distance
|
| 102 |
+
calculation to provide a more accurate rental price prediction.
|
| 103 |
+
"""
|
| 104 |
)
|
| 105 |
|
| 106 |
demo.launch()
|