Distancement / app.py
loretyan's picture
Update app.py
fc5b359 verified
import gradio as gr
import pandas as pd
import joblib
# Modell laden
model = joblib.load("random_forest_model.pkl")
# Durchschnittswerte für fehlende Features
DEFAULT_VALUES = {
"pop": 12000,
"pop_dens": 1500,
"frg_pct": 25.0,
"emp": 90.0,
"tax_income": 65000,
}
# Vorhersagefunktion
def predict_price(rooms, area, luxurious, temporary, furnished, distance_to_center):
room_per_m2 = rooms / area if area > 0 else 0
# Alle möglichen Input-Features zusammenbauen
input_data_dict = {
"rooms": rooms,
"area": area,
"pop": DEFAULT_VALUES["pop"],
"pop_dens": DEFAULT_VALUES["pop_dens"],
"frg_pct": DEFAULT_VALUES["frg_pct"],
"emp": DEFAULT_VALUES["emp"],
"tax_income": DEFAULT_VALUES["tax_income"],
"room_per_m2": room_per_m2,
"luxurious": int(luxurious),
"temporary": int(temporary),
"furnished": int(furnished),
"distance_to_center": distance_to_center
}
# Exakt dieselbe Spaltenreihenfolge wie im Modell
ordered_features = model.feature_names_in_
input_data = pd.DataFrame([[input_data_dict[feature] for feature in ordered_features]], columns=ordered_features)
# Vorhersage
prediction = model.predict(input_data)[0]
return f"Predicted Price: CHF {prediction:,.2f}"
# Gradio Interface
demo = gr.Interface(
fn=predict_price,
inputs=[
gr.Number(label="Rooms"),
gr.Number(label="Area (m²)"),
gr.Checkbox(label="Luxurious"),
gr.Checkbox(label="Temporary"),
gr.Checkbox(label="Furnished"),
gr.Number(label="Distance to Center (km)")
],
outputs=gr.Textbox(label="Prediction"),
title="🏡 Apartment Price Prediction",
description="Enter apartment details to estimate the price in CHF."
)
demo.launch()