licorne2lc commited on
Commit
dc51856
·
1 Parent(s): f892894

Correction endpoint racine et ajout gunicorn

Browse files
Files changed (1) hide show
  1. app.py +63 -55
app.py CHANGED
@@ -1,69 +1,77 @@
1
- import pandas as pd
 
2
  from pydantic import BaseModel
3
  from fastapi import FastAPI
4
- import numpy as np
5
- import joblib
6
- from typing import List
7
-
8
-
9
- # ==== FastAPI Description ====
10
 
11
  description = """
12
- # 🚗 GetAround Rental Price Predictor API
13
 
14
- This API predicts the **rental price per day (in €)** for a car based on various features.
 
 
 
 
15
  """
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  app = FastAPI(
18
- title="GetAround Price Prediction API",
19
  description=description,
20
- version="1.0",
21
- contact={"name": "Ton Nom"},
 
 
 
 
22
  )
23
 
24
- # ==== Input Data Models ====
25
-
26
- class CarCriteria(BaseModel):
27
- model_key: str
28
- mileage: int
29
- engine_power: int
30
- fuel: str
31
- paint_color: str
32
- car_type: str
33
- private_parking_available: bool
34
- has_gps: bool
35
- has_air_conditioning: bool
36
- automatic_car: bool
37
- has_getaround_connect: bool
38
- has_speed_regulator: bool
39
- winter_tires: bool
40
-
41
- class CarOptions(BaseModel):
42
- car_options: List[CarCriteria]
43
-
44
- # ==== Load pipeline (model + preprocessor ensemble) ====
45
-
46
- def load_model():
47
- model = joblib.load("model.joblib") # Pipeline: preprocessor + LinearRegression
48
- return model
49
-
50
- # ==== Predict endpoint ====
51
-
52
- @app.post("/predict", tags=["Machine Learning"])
53
- async def predict(car_options: CarOptions):
54
- model = load_model()
55
-
56
- # Convertir les données en DataFrame
57
- df_input = pd.DataFrame([option.dict() for option in car_options.car_options])
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
+