File size: 1,838 Bytes
a55fa88
a025988
 
 
 
 
 
fc5b359
a025988
 
 
 
 
fc5b359
a025988
 
 
9461aad
823b917
 
fc5b359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823b917
fc5b359
 
 
 
 
a025988
823b917
a025988
fc5b359
a025988
 
 
 
 
 
 
 
7ab55f2
a025988
 
823b917
 
a025988
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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()