File size: 2,313 Bytes
50ce9c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import pandas as pd
import numpy as np

app = FastAPI(title="Airbnb Price Prediction in Copenhagen")

# Load model and preprocessing objects
model_xgb = joblib.load('model_xgb.joblib')
scaler = joblib.load('scaler.joblib')
ohe = joblib.load('ohe.joblib')

class RoomFeatures(BaseModel):
    neighbourhood_cleansed: str
    room_type: str
    instant_bookable: bool
    accommodates: int
    bedrooms: int
    beds: int
    minimum_nights_avg_ntm: int

@app.post("/predict")
async def predict_price(features: RoomFeatures):
    try:
        # Prepare categorical features
        cat_features = pd.DataFrame({
            'neighbourhood_cleansed': [features.neighbourhood_cleansed],
            'room_type': [features.room_type]
        })
        cat_encoded = pd.DataFrame(
            ohe.transform(cat_features).todense(),
            columns=ohe.get_feature_names_out(['neighbourhood_cleansed', 'room_type'])
        )

        # Prepare numerical features
        num_features = pd.DataFrame({
            'instant_bookable': [int(features.instant_bookable)],
            'accommodates': [features.accommodates],
            'bedrooms': [features.bedrooms],
            'beds': [features.beds],
            'minimum_nights_avg_ntm': [features.minimum_nights_avg_ntm]
        })
        num_scaled = pd.DataFrame(scaler.transform(num_features), columns=num_features.columns)

        # Combine features
        combined_features = pd.concat([num_scaled, cat_encoded], axis=1)

        # Make prediction
        predicted_price = model_xgb.predict(combined_features)[0]

        # Calculate price range
        lower_range = max(0, round(predicted_price - 350))
        upper_range = round(predicted_price + 350)

        return {
            "predicted_price": round(predicted_price),
            "suggested_price_range": {
                "lower": lower_range,
                "upper": upper_range
            }
        }
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@app.get("/")
async def root():
    return {"message": "Welcome to the Airbnb Price Prediction API for Copenhagen"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)