File size: 1,351 Bytes
e57d341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
import pandas as pd
import joblib
from pydantic import BaseModel
from typing import List
import os
import boto3

app = FastAPI()

MODEL_LOCAL_PATH = "/tmp/rfr_tuned.joblib"

def download_model_from_s3():
    bucket = os.getenv("S3_BUCKET")
    model_key = os.getenv("S3_MODEL_KEY")
    region = os.getenv("AWS_DEFAULT_REGION")

    s3_client = boto3.client("s3", region_name=region)
    s3_client.download_file(bucket, model_key, MODEL_LOCAL_PATH)

download_model_from_s3()
model = joblib.load(MODEL_LOCAL_PATH)

@app.get("/")
async def root():
    return {"message": "Api is running",
            "docs": "/docs"}

class Health(BaseModel):
    status: str
    model_loaded: bool


class CarFeatures(BaseModel):
    model_key: str
    mileage: int
    engine_power: int
    fuel: str
    paint_color: str
    car_type: str
    private_parking_available: bool
    has_gps: bool
    has_air_conditioning: bool
    automatic_car: bool
    has_getaround_connect: bool
    has_speed_regulator: bool
    winter_tires: bool


class PredictRequest(BaseModel):
    input: List[CarFeatures]

@app.post("/predict")
async def predict(payload: PredictRequest):
    records = [item.model_dump() for item in payload.input]
    df = pd.DataFrame(records)
    predictions = model.predict(df)

    return {"prediction": predictions.tolist()}