Spaces:
Running
Running
init commit
Browse files- .gitignore +1 -0
- Dockerfile +12 -0
- api.py +78 -0
- model.joblib +3 -0
- preprocessor.joblib +3 -0
- requirements.txt +7 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
run.sh
|
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
|
api.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import joblib
|
| 3 |
+
import uvicorn
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import Literal, List, Union
|
| 6 |
+
from fastapi import FastAPI
|
| 7 |
+
from fastapi.encoders import jsonable_encoder
|
| 8 |
+
from fastapi.responses import RedirectResponse
|
| 9 |
+
|
| 10 |
+
description = """
|
| 11 |
+
Welcome to the GetAround Car Rental Price Predictor API!
|
| 12 |
+
|
| 13 |
+
Share your car's attributes and get an estimated daily rental price based on a
|
| 14 |
+
RandomForest model trained on real GetAround data.
|
| 15 |
+
|
| 16 |
+
**Use the `/predict` endpoint to get a price suggestion for your car.**
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
tags_metadata = [
|
| 20 |
+
{
|
| 21 |
+
"name": "Predictions",
|
| 22 |
+
"description": "Endpoint to predict the daily rental price of a car"
|
| 23 |
+
}
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
app = FastAPI(
|
| 27 |
+
title="GetAround — Car Rental Price Predictor",
|
| 28 |
+
description=description,
|
| 29 |
+
version="1.0",
|
| 30 |
+
openapi_tags=tags_metadata
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class Car(BaseModel):
|
| 35 |
+
model_key: Literal['Citroën', 'Peugeot', 'PGO', 'Renault', 'Audi', 'BMW', 'Mercedes',
|
| 36 |
+
'Opel', 'Volkswagen', 'Ferrari', 'Mitsubishi', 'Nissan', 'SEAT',
|
| 37 |
+
'Subaru', 'Toyota', 'other']
|
| 38 |
+
mileage: Union[int, float]
|
| 39 |
+
engine_power: Union[int, float]
|
| 40 |
+
fuel: Literal['diesel', 'petrol', 'other']
|
| 41 |
+
paint_color: Literal['black', 'grey', 'white', 'red', 'silver', 'blue', 'beige', 'brown', 'other']
|
| 42 |
+
car_type: Literal['convertible', 'coupe', 'estate', 'hatchback', 'sedan', 'subcompact', 'suv', 'van']
|
| 43 |
+
private_parking_available: bool
|
| 44 |
+
has_gps: bool
|
| 45 |
+
has_air_conditioning: bool
|
| 46 |
+
automatic_car: bool
|
| 47 |
+
has_getaround_connect: bool
|
| 48 |
+
has_speed_regulator: bool
|
| 49 |
+
winter_tires: bool
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Chargement du modèle et du preprocessor au démarrage (une seule fois)
|
| 53 |
+
preprocessor = joblib.load("preprocessor.joblib")
|
| 54 |
+
model = joblib.load("model.joblib")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@app.get("/", include_in_schema=False)
|
| 58 |
+
async def docs_redirect():
|
| 59 |
+
return RedirectResponse(url='/docs')
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.post("/predict", tags=["Predictions"])
|
| 63 |
+
async def predict(cars: List[Car]):
|
| 64 |
+
"""
|
| 65 |
+
Retourne le prix de location journalier estimé pour une ou plusieurs voitures.
|
| 66 |
+
|
| 67 |
+
**Input** : liste de voitures avec leurs caractéristiques
|
| 68 |
+
|
| 69 |
+
**Output** : `{"prediction": [prix_en_euros, ...]}`
|
| 70 |
+
"""
|
| 71 |
+
car_features = pd.DataFrame(jsonable_encoder(cars))
|
| 72 |
+
car_features_transformed = preprocessor.transform(car_features)
|
| 73 |
+
prediction = model.predict(car_features_transformed)
|
| 74 |
+
return {"prediction": prediction.tolist()}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True)
|
model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:137a16b3654076082c80396e67638800496ee2eefe913db17a786ba91aac84a7
|
| 3 |
+
size 44960641
|
preprocessor.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:132daf149c1c2f7a8e907e9a3988fd461425783c9d9c56037fa6c7f2465aaead
|
| 3 |
+
size 5708
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
pydantic
|
| 4 |
+
pandas
|
| 5 |
+
scikit-learn
|
| 6 |
+
numpy
|
| 7 |
+
joblib
|