File size: 4,231 Bytes
71770e2
 
 
 
2ffd960
71770e2
 
 
 
 
2ffd960
 
 
 
 
 
 
 
 
 
 
 
 
 
20eafed
5119fbe
 
 
 
 
 
 
 
 
2ffd960
 
 
 
 
 
 
71770e2
2ffd960
20eafed
71770e2
 
 
2ffd960
 
 
 
 
 
 
71770e2
47eaa26
2ffd960
71770e2
 
 
 
2ffd960
71770e2
 
 
 
 
 
2ffd960
71770e2
2ffd960
71770e2
 
2ffd960
71770e2
2ffd960
71770e2
 
 
 
 
 
2ffd960
 
 
71770e2
 
 
 
2ffd960
 
71770e2
 
2ffd960
 
 
 
 
71770e2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gradio as gr
import pandas as pd
import numpy as np
import pickle
from math import radians, cos, sin, asin, sqrt

# Load the model
with open("apartment_price_model.pkl", mode="rb") as f:
    model = pickle.load(f)

# Zurich neighborhoods with coordinates and distances
zurich_neighborhoods = {
    "City Center (Altstadt)": {"lat": 47.3769, "lon": 8.5417, "distance": 0.0},
    "Oerlikon": {"lat": 47.4111, "lon": 8.5458, "distance": 3.8},
    "Altstetten": {"lat": 47.3908, "lon": 8.4889, "distance": 4.2},
    "Wiedikon": {"lat": 47.3708, "lon": 8.5128, "distance": 2.3},
    "Seefeld": {"lat": 47.3550, "lon": 8.5550, "distance": 2.7},
    "Schwamendingen": {"lat": 47.4053, "lon": 8.5648, "distance": 3.5},
    "Wollishofen": {"lat": 47.3517, "lon": 8.5304, "distance": 3.0},
    "Enge": {"lat": 47.3656, "lon": 8.5267, "distance": 1.2},
    "Fluntern": {"lat": 47.3797, "lon": 8.5611, "distance": 1.8},
    "Hottingen": {"lat": 47.3683, "lon": 8.5584, "distance": 1.5},
    "Custom Location": {"lat": 47.3769, "lon": 8.5417, "distance": 0.0}
}

def haversine_distance(lat1, lon1, lat2, lon2):
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a))
    r = 6371  # Radius of earth in kilometers
    return c * r

def predict_price(neighborhood, rooms, area, has_balcony, is_renovated, proximity_to_transport, custom_lat=None, custom_lon=None):
    if neighborhood == "Custom Location" and custom_lat is not None and custom_lon is not None:
        lat = custom_lat
        lon = custom_lon
    else:
        lat = zurich_neighborhoods[neighborhood]["lat"]
        lon = zurich_neighborhoods[neighborhood]["lon"]

    distance_to_center = haversine_distance(lat, lon, 47.3769, 8.5417)

    input_data = pd.DataFrame([{
        'rooms': rooms,
        'area': area,
        'pop': 420217,
        'pop_dens': 4778,
        'frg_pct': 32.45,
        'emp': 491193,
        'tax_income': 85446,
        'price_per_room': 0,
        'distance_to_center': distance_to_center,
        'has_balcony': 1 if has_balcony else 0,
        'is_renovated': 1 if is_renovated else 0,
        'proximity_to_transport': 1 if proximity_to_transport else 0
    }])

    features = [
        'rooms', 'area', 'pop', 'pop_dens', 'frg_pct', 'emp', 'tax_income',
        'price_per_room', 'distance_to_center', 'has_balcony', 'is_renovated', 'proximity_to_transport'
    ]

    predicted_price = model.predict(input_data[features])[0]

    result = f"Predicted Monthly Rent: CHF {predicted_price:.0f}"
    result += f"\n\nProperty Details:"
    result += f"\n- Location: {neighborhood}"
    result += f"\n- {rooms} rooms, {area} m²"
    result += f"\n- {distance_to_center:.2f} km from city center"
    result += f"\n- {'Has balcony' if has_balcony else 'No balcony'}"
    result += f"\n- {'Renovated' if is_renovated else 'Not renovated'}"
    result += f"\n- {'Close to transport' if proximity_to_transport else 'Far from transport'}"

    return result

with gr.Blocks() as demo:
    gr.Markdown("# Zurich Apartment Rent Prediction")

    with gr.Row():
        with gr.Column():
            neighborhood = gr.Dropdown(label="Neighborhood", choices=list(zurich_neighborhoods.keys()), value="City Center (Altstadt)")
            custom_lat = gr.Number(label="Custom Latitude", value=47.3769, visible=False)
            custom_lon = gr.Number(label="Custom Longitude", value=8.5417, visible=False)
            rooms = gr.Number(label="Number of Rooms", value=3.5)
            area = gr.Number(label="Area (m²)", value=75)
            has_balcony = gr.Checkbox(label="Has Balcony", value=True)
            is_renovated = gr.Checkbox(label="Is Renovated", value=False)
            proximity_to_transport = gr.Checkbox(label="Proximity to Transport", value=False)
            submit_button = gr.Button("Submit")
            output = gr.Textbox(label="Output")

        submit_button.click(
            fn=predict_price,
            inputs=[neighborhood, rooms, area, has_balcony, is_renovated, proximity_to_transport, custom_lat, custom_lon],
            outputs=output
        )

demo.launch()