|
|
import gradio as gr
|
|
|
import pandas as pd
|
|
|
import pickle
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
try:
|
|
|
model_filename = "apartment_price_model.pkl"
|
|
|
with open(model_filename, mode="rb") as f:
|
|
|
model = pickle.load(f)
|
|
|
model_loaded = True
|
|
|
except:
|
|
|
print("Model file not found. App will still run but predictions will be simulated.")
|
|
|
model_loaded = False
|
|
|
|
|
|
def predict(
|
|
|
rooms,
|
|
|
area,
|
|
|
pop,
|
|
|
pop_dens,
|
|
|
frg_pct,
|
|
|
emp,
|
|
|
tax_income,
|
|
|
room_per_m2,
|
|
|
luxurious,
|
|
|
temporary,
|
|
|
furnished,
|
|
|
area_cat_ecoded,
|
|
|
zurich_city,
|
|
|
listing_complexity
|
|
|
):
|
|
|
|
|
|
input_data = pd.DataFrame([[
|
|
|
rooms,
|
|
|
area,
|
|
|
pop,
|
|
|
pop_dens,
|
|
|
frg_pct,
|
|
|
emp,
|
|
|
tax_income,
|
|
|
room_per_m2,
|
|
|
luxurious,
|
|
|
temporary,
|
|
|
furnished,
|
|
|
area_cat_ecoded,
|
|
|
zurich_city,
|
|
|
listing_complexity
|
|
|
]], columns=[
|
|
|
'rooms',
|
|
|
'area',
|
|
|
'pop',
|
|
|
'pop_dens',
|
|
|
'frg_pct',
|
|
|
'emp',
|
|
|
'tax_income',
|
|
|
'room_per_m2',
|
|
|
'luxurious',
|
|
|
'temporary',
|
|
|
'furnished',
|
|
|
'area_cat_ecoded',
|
|
|
'zurich_city',
|
|
|
'listing_complexity'
|
|
|
])
|
|
|
|
|
|
|
|
|
if model_loaded:
|
|
|
prediction = model.predict(input_data)[0]
|
|
|
else:
|
|
|
|
|
|
prediction = 1500 + (rooms * 500) + (area * 10)
|
|
|
|
|
|
return f"Predicted monthly rent: CHF {prediction:.2f}"
|
|
|
|
|
|
|
|
|
demo = gr.Interface(
|
|
|
fn=predict,
|
|
|
inputs=[
|
|
|
gr.Number(label="Number of Rooms", value=3),
|
|
|
gr.Number(label="Area (m²)", value=80),
|
|
|
gr.Number(label="Population", value=30000),
|
|
|
gr.Number(label="Population Density", value=5000),
|
|
|
gr.Number(label="Foreign Population Percentage", value=25),
|
|
|
gr.Number(label="Employment Rate", value=0.75),
|
|
|
gr.Number(label="Average Tax Income (CHF)", value=80000),
|
|
|
gr.Number(label="Room per m²", value=26.7),
|
|
|
gr.Number(label="Luxurious (0 or 1)", value=0),
|
|
|
gr.Number(label="Temporary (0 or 1)", value=0),
|
|
|
gr.Number(label="Furnished (0 or 1)", value=0),
|
|
|
gr.Number(label="Area Category Encoded (0, 1, or 2)", value=1),
|
|
|
gr.Number(label="Zurich City (0 or 1)", value=0),
|
|
|
gr.Number(label="Listing Complexity", value=0.5)
|
|
|
],
|
|
|
outputs="text",
|
|
|
title="Swiss Apartment Price Predictor",
|
|
|
description="Enter apartment details to predict the monthly rental price in Swiss Francs (CHF).",
|
|
|
examples=[
|
|
|
|
|
|
[2, 65, 39647, 2574, 34.5, 0.82, 92000, 32.5, 1, 0, 1, 1, 1, 0.65],
|
|
|
|
|
|
[3.5, 85, 25000, 3200, 22.3, 0.78, 75000, 24.3, 0, 0, 0, 1, 0, 0.5],
|
|
|
|
|
|
[1, 35, 15874, 7942, 38.2, 0.83, 88000, 35.0, 0, 1, 1, 0, 1, 0.45]
|
|
|
]
|
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
demo.launch() |