Spaces:
Sleeping
Sleeping
Commit ·
dc51856
1
Parent(s): f892894
Correction endpoint racine et ajout gunicorn
Browse files
app.py
CHANGED
|
@@ -1,69 +1,77 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from fastapi import FastAPI
|
| 4 |
-
|
| 5 |
-
import joblib
|
| 6 |
-
from typing import List
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# ==== FastAPI Description ====
|
| 10 |
|
| 11 |
description = """
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
app = FastAPI(
|
| 18 |
-
title="GetAround
|
| 19 |
description=description,
|
| 20 |
-
version="
|
| 21 |
-
contact={
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
# Prédiction directe (le modèle contient déjà le préprocesseur)
|
| 60 |
-
predictions = model.predict(df_input)
|
| 61 |
-
|
| 62 |
-
# Retourner les résultats formatés
|
| 63 |
-
formatted = [f"Option {i+1}: {round(pred)} €" for i, pred in enumerate(predictions)]
|
| 64 |
-
return {"predictions": formatted}
|
| 65 |
-
|
| 66 |
-
if __name__=="__main__":
|
| 67 |
uvicorn.run(app, host="0.0.0.0", port=4000)
|
| 68 |
|
| 69 |
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
import pandas as pd
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from fastapi import FastAPI
|
| 5 |
+
from joblib import load
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
description = """
|
| 8 |
+
Welcome to the GetAround API, designed to assist you in predicting the rental price for your car!
|
| 9 |
|
| 10 |
+
Here are the available endpoints:
|
| 11 |
+
|
| 12 |
+
* `/`: This endpoint is provided as an example. You can explore its functionality.
|
| 13 |
+
* `/predict`: This endpoint accepts a POST request with JSON input data. You can use this endpoint to make predictions by providing the necessary information about your car.
|
| 14 |
+
Feel free to use the `/predict` endpoint by sending a POST request with the required JSON data to obtain accurate rental price predictions for your vehicule
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
tags_metadata = [
|
| 18 |
+
{
|
| 19 |
+
"name": "Simple Endpoint",
|
| 20 |
+
"description": "Simple endpoint to try out!",
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"name": "Prediction",
|
| 24 |
+
"description": "Prediction of the rental price based"
|
| 25 |
+
}
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
app = FastAPI(
|
| 29 |
+
title="🚙 GetAround price prediction API ",
|
| 30 |
description=description,
|
| 31 |
+
version="0.1",
|
| 32 |
+
contact={
|
| 33 |
+
"name": "GetAround API - by Delphine Cesar",
|
| 34 |
+
"url": "https://github.com/delphinecesar",
|
| 35 |
+
},
|
| 36 |
+
openapi_tags=tags_metadata,
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Data types for prediction
|
| 40 |
+
class PredictionFeatures(BaseModel):
|
| 41 |
+
model_key: str = "Peugeot"
|
| 42 |
+
mileage: int = 13131
|
| 43 |
+
engine_power: int = 110
|
| 44 |
+
fuel: str = "diesel"
|
| 45 |
+
paint_color: str = "grey"
|
| 46 |
+
car_type: str = "convertible"
|
| 47 |
+
private_parking_available: bool = False
|
| 48 |
+
has_gps: bool = True
|
| 49 |
+
has_air_conditioning: bool = True
|
| 50 |
+
automatic_car: bool = False
|
| 51 |
+
has_getaround_connect: bool = True
|
| 52 |
+
has_speed_regulator: bool = False
|
| 53 |
+
winter_tires: bool = True
|
| 54 |
+
|
| 55 |
+
@app.get("/", tags=["Simple Endpoint"])
|
| 56 |
+
async def index():
|
| 57 |
+
return {"message": "Hello World!"}
|
| 58 |
+
|
| 59 |
+
@app.post("/predict", tags=["Prediction"])
|
| 60 |
+
async def predict(features: PredictionFeatures):
|
| 61 |
+
# Convert input data to DataFrame
|
| 62 |
+
information = pd.DataFrame(features.dict(), index=[0])
|
| 63 |
+
|
| 64 |
+
# Load model
|
| 65 |
+
model = load('model.joblib')
|
| 66 |
+
|
| 67 |
+
# Make prediction
|
| 68 |
+
prediction = model.predict(information)
|
| 69 |
+
|
| 70 |
+
# Return result
|
| 71 |
+
return {"prediction": prediction.tolist()[0]}
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
uvicorn.run(app, host="0.0.0.0", port=4000)
|
| 75 |
|
| 76 |
|
| 77 |
+
|