Upload 3 files
Browse files- apartment_price_model.pkl +3 -0
- app.py +83 -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:3163528f3e621c77826714fcd0eba88e2fd33323c2ce77aec659b21d50983ce6
|
| 3 |
+
size 14735024
|
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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
|
| 13 |
+
pop_dens = 4778
|
| 14 |
+
frg_pct = 32.45
|
| 15 |
+
emp = 491193
|
| 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,
|
| 22 |
+
'area': area,
|
| 23 |
+
'pop': pop,
|
| 24 |
+
'pop_dens': pop_dens,
|
| 25 |
+
'frg_pct': frg_pct,
|
| 26 |
+
'emp': emp,
|
| 27 |
+
'tax_income': tax_income,
|
| 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 |
+
}])
|
| 32 |
+
|
| 33 |
+
# Define features in the correct order
|
| 34 |
+
features = [
|
| 35 |
+
'rooms', 'area', 'pop', 'pop_dens', 'frg_pct', 'emp', 'tax_income',
|
| 36 |
+
'price_per_room', 'has_balcony', 'is_renovated'
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# Make prediction
|
| 40 |
+
predicted_price = model.predict(input_data[features])[0]
|
| 41 |
+
|
| 42 |
+
# Format the result
|
| 43 |
+
result = f"Predicted Monthly Rent: CHF {predicted_price:.0f}"
|
| 44 |
+
result += f"\n\nProperty Details:"
|
| 45 |
+
result += f"\n- {rooms} rooms, {area} m²"
|
| 46 |
+
result += f"\n- {'Has balcony' if has_balcony else 'No balcony'}"
|
| 47 |
+
result += f"\n- {'Renovated' if is_renovated else 'Not renovated'}"
|
| 48 |
+
|
| 49 |
+
return result
|
| 50 |
+
|
| 51 |
+
def reset_inputs():
|
| 52 |
+
return [3.5, 75, True, False, ""]
|
| 53 |
+
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# Zurich Apartment Rent Prediction")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
rooms = gr.Number(label="Number of Rooms", value=3.5)
|
| 60 |
+
area = gr.Number(label="Area (m²)", value=75)
|
| 61 |
+
has_balcony = gr.Checkbox(label="Has Balcony", value=True)
|
| 62 |
+
is_renovated = gr.Checkbox(label="Is Renovated", value=False)
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
submit_button = gr.Button("Submit")
|
| 66 |
+
clear_button = gr.Button("Clear")
|
| 67 |
+
|
| 68 |
+
with gr.Column():
|
| 69 |
+
output = gr.Textbox(label="Output")
|
| 70 |
+
|
| 71 |
+
submit_button.click(
|
| 72 |
+
fn=predict_price,
|
| 73 |
+
inputs=[None, rooms, area, has_balcony, is_renovated],
|
| 74 |
+
outputs=output
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
clear_button.click(
|
| 78 |
+
fn=reset_inputs,
|
| 79 |
+
inputs=None,
|
| 80 |
+
outputs=[rooms, area, has_balcony, is_renovated, output]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|