Spaces:
Runtime error
Runtime error
File size: 1,770 Bytes
3295edd | 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 60 61 62 63 64 65 66 67 68 69 70 71 | import uvicorn
import pandas as pd
from pydantic import BaseModel
from fastapi import FastAPI
import numpy as np
import joblib
from typing import List
# ==== FastAPI Description ====
description = """
# 🚗 GetAround Rental Price Predictor API
This API predicts the **rental price per day (in €)** for a car based on various features.
"""
app = FastAPI(
title="GetAround Price Prediction API",
description=description,
version="1.0",
contact={"name": "Ton Nom"},
)
# ==== Input Data Models ====
class CarCriteria(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 CarOptions(BaseModel):
car_options: List[CarCriteria]
# ==== Load pipeline (model + preprocessor ensemble) ====
def load_model():
model = joblib.load("model.joblib") # Pipeline: preprocessor + LinearRegression
return model
# ==== Predict endpoint ====
@app.post("/predict", tags=["Machine Learning"])
async def predict(car_options: CarOptions):
model = load_model()
# Convertir les données en DataFrame
df_input = pd.DataFrame([option.dict() for option in car_options.car_options])
# Prédiction directe (le modèle contient déjà le préprocesseur)
predictions = model.predict(df_input)
# Retourner les résultats formatés
formatted = [f"Option {i+1}: {round(pred)} €" for i, pred in enumerate(predictions)]
return {"predictions": formatted}
# ==== Exécution locale ====
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=4000)
|