RJuro commited on
Commit
cda9368
·
verified ·
1 Parent(s): 9aaa641

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +17 -0
  2. app.py +72 -0
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the current directory contents into the container at /app
8
+ COPY . /app
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Make port 7575 available to the world outside this container
14
+ EXPOSE 7575
15
+
16
+ # Run the application when the container launches
17
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7575"]
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import joblib
4
+ import pandas as pd
5
+ import numpy as np
6
+
7
+ app = FastAPI(title="Airbnb Price Prediction in Copenhagen")
8
+
9
+ # Load model and preprocessing objects
10
+ model_xgb = joblib.load('model_xgb.joblib')
11
+ scaler = joblib.load('scaler.joblib')
12
+ ohe = joblib.load('ohe.joblib')
13
+
14
+ class RoomFeatures(BaseModel):
15
+ neighbourhood_cleansed: str
16
+ room_type: str
17
+ instant_bookable: bool
18
+ accommodates: int
19
+ bedrooms: int
20
+ beds: int
21
+ minimum_nights_avg_ntm: int
22
+
23
+ @app.post("/predict")
24
+ async def predict_price(features: RoomFeatures):
25
+ try:
26
+ # Prepare categorical features
27
+ cat_features = pd.DataFrame({
28
+ 'neighbourhood_cleansed': [features.neighbourhood_cleansed],
29
+ 'room_type': [features.room_type]
30
+ })
31
+ cat_encoded = pd.DataFrame(
32
+ ohe.transform(cat_features).todense(),
33
+ columns=ohe.get_feature_names_out(['neighbourhood_cleansed', 'room_type'])
34
+ )
35
+
36
+ # Prepare numerical features
37
+ num_features = pd.DataFrame({
38
+ 'instant_bookable': [int(features.instant_bookable)],
39
+ 'accommodates': [features.accommodates],
40
+ 'bedrooms': [features.bedrooms],
41
+ 'beds': [features.beds],
42
+ 'minimum_nights_avg_ntm': [features.minimum_nights_avg_ntm]
43
+ })
44
+ num_scaled = pd.DataFrame(scaler.transform(num_features), columns=num_features.columns)
45
+
46
+ # Combine features
47
+ combined_features = pd.concat([num_scaled, cat_encoded], axis=1)
48
+
49
+ # Make prediction
50
+ predicted_price = model_xgb.predict(combined_features)[0]
51
+
52
+ # Calculate price range
53
+ lower_range = max(0, round(predicted_price - 350))
54
+ upper_range = round(predicted_price + 350)
55
+
56
+ return {
57
+ "predicted_price": round(predicted_price),
58
+ "suggested_price_range": {
59
+ "lower": lower_range,
60
+ "upper": upper_range
61
+ }
62
+ }
63
+ except Exception as e:
64
+ raise HTTPException(status_code=400, detail=str(e))
65
+
66
+ @app.get("/")
67
+ async def root():
68
+ return {"message": "Welcome to the Airbnb Price Prediction API for Copenhagen"}
69
+
70
+ if __name__ == "__main__":
71
+ import uvicorn
72
+ uvicorn.run(app, host="0.0.0.0", port=8000)