chrisis2 commited on
Commit
50fd5d6
·
verified ·
1 Parent(s): 4b0cd04

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -30
app.py CHANGED
@@ -8,9 +8,20 @@ from math import radians, cos, sin, asin, sqrt
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,9 +37,23 @@ 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
@@ -65,6 +90,7 @@ def predict_price(rooms, area, lat, lon, has_balcony, is_renovated):
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'}"
@@ -72,35 +98,71 @@ def predict_price(rooms, area, lat, lon, has_balcony, is_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 ()"),
81
- gr.Number(label="Latitude", value=47.3769),
82
- gr.Number(label="Longitude", value=8.5417),
83
- gr.Checkbox(label="Has Balcony"),
84
- gr.Checkbox(label="Is Renovated"),
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()
 
8
  with open("apartment_price_model.pkl", mode="rb") as f:
9
  model = pickle.load(f)
10
 
11
+ # Define Zurich neighborhoods with their approximate coordinates and distances
12
+ zurich_neighborhoods = {
13
+ "City Center (Altstadt)": {"lat": 47.3769, "lon": 8.5417, "distance": 0.0},
14
+ "Oerlikon": {"lat": 47.4111, "lon": 8.5458, "distance": 3.8},
15
+ "Altstetten": {"lat": 47.3908, "lon": 8.4889, "distance": 4.2},
16
+ "Wiedikon": {"lat": 47.3708, "lon": 8.5128, "distance": 2.3},
17
+ "Seefeld": {"lat": 47.3550, "lon": 8.5550, "distance": 2.7},
18
+ "Schwamendingen": {"lat": 47.4053, "lon": 8.5648, "distance": 3.5},
19
+ "Wollishofen": {"lat": 47.3517, "lon": 8.5304, "distance": 3.0},
20
+ "Enge": {"lat": 47.3656, "lon": 8.5267, "distance": 1.2},
21
+ "Fluntern": {"lat": 47.3797, "lon": 8.5611, "distance": 1.8},
22
+ "Hottingen": {"lat": 47.3683, "lon": 8.5584, "distance": 1.5},
23
+ "Custom Location": {"lat": 47.3769, "lon": 8.5417, "distance": 0.0}
24
+ }
25
 
26
  # Function to calculate distance between two points using Haversine formula
27
  def haversine_distance(lat1, lon1, lat2, lon2):
 
37
  r = 6371 # Radius of earth in kilometers
38
  return c * r
39
 
40
+ def predict_price(neighborhood, rooms, area, has_balcony, is_renovated, custom_lat=None, custom_lon=None):
41
+ # Get coordinates based on neighborhood selection
42
+ if neighborhood == "Custom Location" and custom_lat is not None and custom_lon is not None:
43
+ lat = custom_lat
44
+ lon = custom_lon
45
+ else:
46
+ lat = zurich_neighborhoods[neighborhood]["lat"]
47
+ lon = zurich_neighborhoods[neighborhood]["lon"]
48
+
49
+ # Calculate distance to city center
50
+ zurich_center_lat = zurich_neighborhoods["City Center (Altstadt)"]["lat"]
51
+ zurich_center_lon = zurich_neighborhoods["City Center (Altstadt)"]["lon"]
52
+
53
+ if neighborhood == "Custom Location" and custom_lat is not None and custom_lon is not None:
54
+ distance_to_center = haversine_distance(custom_lat, custom_lon, zurich_center_lat, zurich_center_lon)
55
+ else:
56
+ distance_to_center = zurich_neighborhoods[neighborhood]["distance"]
57
 
58
  # Default values for other features
59
  pop = 420217
 
90
  # Format the result
91
  result = f"Predicted Monthly Rent: CHF {predicted_price:.0f}"
92
  result += f"\n\nProperty Details:"
93
+ result += f"\n- Location: {neighborhood}"
94
  result += f"\n- {rooms} rooms, {area} m²"
95
  result += f"\n- {distance_to_center:.2f} km from city center"
96
  result += f"\n- {'Has balcony' if has_balcony else 'No balcony'}"
 
98
 
99
  return result
100
 
101
+ # Function to update visibility of lat/lon inputs based on neighborhood selection
102
+ def update_custom_location(neighborhood):
103
+ if neighborhood == "Custom Location":
104
+ return gr.update(visible=True), gr.update(visible=True)
105
+ else:
106
+ return gr.update(visible=False), gr.update(visible=False)
107
+
108
+ # Create Gradio interface with neighborhood dropdown
109
+ with gr.Blocks() as demo:
110
+ gr.Markdown("# Zurich Apartment Rent Prediction")
111
+ gr.Markdown("""
 
 
 
 
 
 
 
 
112
  This app predicts apartment rental prices in Zurich with a special feature: Distance to City Center.
113
 
114
  **Special Feature Description:**
115
+ The model automatically calculates how far the apartment is from Zurich city center.
116
  This distance is a critical factor in real estate pricing - properties closer to the city center typically
117
  command higher rents due to convenience and accessibility to urban amenities.
118
 
119
+ Simply select a neighborhood, and the app will use its distance from the city center to help
120
+ provide a more accurate rental price prediction.
121
+ """)
122
+
123
+ with gr.Row():
124
+ with gr.Column():
125
+ neighborhood = gr.Dropdown(
126
+ label="Neighborhood",
127
+ choices=list(zurich_neighborhoods.keys()),
128
+ value="City Center (Altstadt)"
129
+ )
130
+ custom_lat = gr.Number(label="Custom Latitude", value=47.3769, visible=False)
131
+ custom_lon = gr.Number(label="Custom Longitude", value=8.5417, visible=False)
132
+ rooms = gr.Number(label="Number of Rooms", value=3.5)
133
+ area = gr.Number(label="Area (m²)", value=75)
134
+ has_balcony = gr.Checkbox(label="Has Balcony", value=True)
135
+ is_renovated = gr.Checkbox(label="Is Renovated", value=False)
136
+
137
+ predict_button = gr.Button("Predict Rent")
138
+
139
+ with gr.Column():
140
+ output = gr.Textbox(label="Prediction Result")
141
+
142
+ # Connect the neighborhood dropdown to show/hide custom lat/lon
143
+ neighborhood.change(
144
+ fn=update_custom_location,
145
+ inputs=neighborhood,
146
+ outputs=[custom_lat, custom_lon]
147
+ )
148
+
149
+ # Connect the predict button
150
+ predict_button.click(
151
+ fn=predict_price,
152
+ inputs=[neighborhood, rooms, area, has_balcony, is_renovated, custom_lat, custom_lon],
153
+ outputs=output
154
+ )
155
+
156
+ # Add examples
157
+ gr.Examples(
158
+ examples=[
159
+ ["Oerlikon", 3.5, 75, True, True],
160
+ ["Seefeld", 2.0, 60, False, False],
161
+ ["Wiedikon", 4.5, 120, True, False],
162
+ ],
163
+ inputs=[neighborhood, rooms, area, has_balcony, is_renovated],
164
+ outputs=output,
165
+ fn=predict_price
166
+ )
167
 
168
  demo.launch()