Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +45 -0
- random_forest_regression_with_maturity.pkl +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Load the trained model
|
| 6 |
+
model_path = "random_forest_regression_with_maturity.pkl"
|
| 7 |
+
with open(model_path, "rb") as file:
|
| 8 |
+
model = pickle.load(file)
|
| 9 |
+
|
| 10 |
+
# Load dataset to calculate average values for missing inputs
|
| 11 |
+
df = pd.read_csv("apartments_data_enriched_lat_lon_combined.csv")
|
| 12 |
+
|
| 13 |
+
# Compute average values for the non-input features
|
| 14 |
+
avg_pop = df["pop"].mean()
|
| 15 |
+
avg_pop_dens = df["pop_dens"].mean()
|
| 16 |
+
avg_frg_pct = df["frg_pct"].mean()
|
| 17 |
+
avg_emp = df["emp"].mean()
|
| 18 |
+
avg_tax_income = df["tax_income"].astype(str).str.replace("'", "").astype(float).mean()
|
| 19 |
+
|
| 20 |
+
# Define the prediction function with only three inputs
|
| 21 |
+
def predict_rent(rooms, area, maturity_rate):
|
| 22 |
+
# Use average values for missing features
|
| 23 |
+
features = [[rooms, area, avg_pop, avg_pop_dens, avg_frg_pct, avg_emp, avg_tax_income, maturity_rate]]
|
| 24 |
+
|
| 25 |
+
# Make prediction
|
| 26 |
+
predicted_price = model.predict(features)[0]
|
| 27 |
+
|
| 28 |
+
return f"Predicted Rent Price: CHF {predicted_price:,.2f}"
|
| 29 |
+
|
| 30 |
+
# Example prediction
|
| 31 |
+
example_prediction = predict_rent(3.5, 75, 20.314)
|
| 32 |
+
print(example_prediction)
|
| 33 |
+
|
| 34 |
+
# Gradio Interface (Simplified)
|
| 35 |
+
gr.Interface(
|
| 36 |
+
fn=predict_rent,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Number(label="Rooms"),
|
| 39 |
+
gr.Number(label="Area (sqm)"),
|
| 40 |
+
gr.Number(label="Maturity Rate"),
|
| 41 |
+
],
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="Simplified Apartment Rent Price Predictor",
|
| 44 |
+
description="Enter only the number of rooms, area, and maturity rate to estimate the rent price.",
|
| 45 |
+
).launch()
|
random_forest_regression_with_maturity.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bd7032ceb033efade98cd0b38108035458b3e1b500c38fc5022f0e0261687a6a
|
| 3 |
+
size 13780418
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
scikit-learn
|
| 3 |
+
pandas
|
| 4 |
+
numpy
|