Upload 3 files
Browse files- apartment_price_model.pkl +3 -0
- app.py +105 -0
- requirements.txt +1 -0
apartment_price_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fca4899edfa139b54aedb3ed936a350a1dd51ea9326f84bdefb9b3d1138a6553
|
| 3 |
+
size 14830517
|
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pickle
|
| 5 |
+
from math import radians, cos, sin, asin, sqrt
|
| 6 |
+
|
| 7 |
+
# Load only the model
|
| 8 |
+
with open("apartment_price_model.pkl", mode="rb") as f:
|
| 9 |
+
model = pickle.load(f)
|
| 10 |
+
|
| 11 |
+
# Define features directly in the app
|
| 12 |
+
features = [
|
| 13 |
+
'rooms', 'area', 'pop', 'pop_dens', 'frg_pct', 'emp', 'tax_income',
|
| 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):
|
| 19 |
+
"""Calculate the great circle distance between two points on earth."""
|
| 20 |
+
# Convert decimal degrees to radians
|
| 21 |
+
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
|
| 22 |
+
|
| 23 |
+
# Haversine formula
|
| 24 |
+
dlon = lon2 - lon1
|
| 25 |
+
dlat = lat2 - lat1
|
| 26 |
+
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
|
| 27 |
+
c = 2 * asin(sqrt(a))
|
| 28 |
+
r = 6371 # Radius of earth in kilometers
|
| 29 |
+
return c * r
|
| 30 |
+
|
| 31 |
+
# Zurich city center coordinates
|
| 32 |
+
zurich_center_lat = 47.3769
|
| 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': population,
|
| 47 |
+
'pop_dens': pop_density,
|
| 48 |
+
'frg_pct': foreign_pct,
|
| 49 |
+
'emp': employment,
|
| 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 |
+
# Make prediction
|
| 58 |
+
predicted_price = model.predict(input_data[features])[0]
|
| 59 |
+
|
| 60 |
+
# Update price_per_room with the predicted price
|
| 61 |
+
price_per_room = predicted_price / rooms
|
| 62 |
+
input_data['price_per_room'] = price_per_room
|
| 63 |
+
|
| 64 |
+
# Make another prediction with updated price_per_room
|
| 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 mentioned as renovated'}"
|
| 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"),
|
| 93 |
+
gr.Checkbox(label="Is Renovated"),
|
| 94 |
+
],
|
| 95 |
+
outputs="text",
|
| 96 |
+
examples=[
|
| 97 |
+
[3.5, 75, 420217, 4778, 32.45, 491193, 85446, 47.41106, 8.54654, True, True],
|
| 98 |
+
[2.0, 60, 420217, 4778, 32.45, 491193, 85446, 47.37624, 8.52814, False, False],
|
| 99 |
+
[4.5, 120, 420217, 4778, 32.45, 491193, 85446, 47.36368, 8.54678, True, False],
|
| 100 |
+
],
|
| 101 |
+
title="Zurich Apartment Rent Prediction",
|
| 102 |
+
description="Enter apartment details to predict the monthly rent in Swiss Francs (CHF)."
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|