Spaces:
Runtime error
Runtime error
| 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 ==== | |
| 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) | |