Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import pickle
|
|
| 7 |
with open("apartment_price_model.pkl", mode="rb") as f:
|
| 8 |
model = pickle.load(f)
|
| 9 |
|
| 10 |
-
def predict_price(rooms, area, has_balcony, is_renovated):
|
| 11 |
# Default values for other features
|
| 12 |
pop = 420217
|
| 13 |
pop_dens = 4778
|
|
@@ -27,13 +27,14 @@ def predict_price(rooms, area, has_balcony, is_renovated):
|
|
| 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
|
|
@@ -42,6 +43,7 @@ def predict_price(rooms, area, has_balcony, is_renovated):
|
|
| 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'}"
|
|
@@ -49,13 +51,22 @@ def predict_price(rooms, area, has_balcony, is_renovated):
|
|
| 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)
|
|
@@ -70,14 +81,14 @@ with gr.Blocks() as demo:
|
|
| 70 |
|
| 71 |
submit_button.click(
|
| 72 |
fn=predict_price,
|
| 73 |
-
inputs=[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()
|
|
|
|
| 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
|
|
|
|
| 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 |
+
'neighborhood': neighborhood
|
| 32 |
}])
|
| 33 |
|
| 34 |
# Define features in the correct order
|
| 35 |
features = [
|
| 36 |
'rooms', 'area', 'pop', 'pop_dens', 'frg_pct', 'emp', 'tax_income',
|
| 37 |
+
'price_per_room', 'has_balcony', 'is_renovated', 'neighborhood'
|
| 38 |
]
|
| 39 |
|
| 40 |
# Make prediction
|
|
|
|
| 43 |
# Format the result
|
| 44 |
result = f"Predicted Monthly Rent: CHF {predicted_price:.0f}"
|
| 45 |
result += f"\n\nProperty Details:"
|
| 46 |
+
result += f"\n- Neighborhood: {neighborhood}"
|
| 47 |
result += f"\n- {rooms} rooms, {area} m²"
|
| 48 |
result += f"\n- {'Has balcony' if has_balcony else 'No balcony'}"
|
| 49 |
result += f"\n- {'Renovated' if is_renovated else 'Not renovated'}"
|
|
|
|
| 51 |
return result
|
| 52 |
|
| 53 |
def reset_inputs():
|
| 54 |
+
return ["City Center (Altstadt)", 3.5, 75, True, False, ""]
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("# Zurich Apartment Rent Prediction")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column():
|
| 61 |
+
neighborhood = gr.Dropdown(
|
| 62 |
+
label="Neighborhood",
|
| 63 |
+
choices=[
|
| 64 |
+
"City Center (Altstadt)", "Oerlikon", "Altstetten", "Wiedikon",
|
| 65 |
+
"Seefeld", "Schwamendingen", "Wollishofen", "Enge",
|
| 66 |
+
"Fluntern", "Hottingen"
|
| 67 |
+
],
|
| 68 |
+
value="City Center (Altstadt)"
|
| 69 |
+
)
|
| 70 |
rooms = gr.Number(label="Number of Rooms", value=3.5)
|
| 71 |
area = gr.Number(label="Area (m²)", value=75)
|
| 72 |
has_balcony = gr.Checkbox(label="Has Balcony", value=True)
|
|
|
|
| 81 |
|
| 82 |
submit_button.click(
|
| 83 |
fn=predict_price,
|
| 84 |
+
inputs=[neighborhood, rooms, area, has_balcony, is_renovated],
|
| 85 |
outputs=output
|
| 86 |
)
|
| 87 |
|
| 88 |
clear_button.click(
|
| 89 |
fn=reset_inputs,
|
| 90 |
inputs=None,
|
| 91 |
+
outputs=[neighborhood, rooms, area, has_balcony, is_renovated, output]
|
| 92 |
)
|
| 93 |
|
| 94 |
demo.launch()
|