from fastapi import FastAPI from fastapi.responses import HTMLResponse from pydantic import BaseModel from typing import List import joblib import pandas as pd import numpy as np app = FastAPI() # ── Load model ──────────────────────────────────────────────────────────── try: model = joblib.load("pricing_model.joblib") except: model = None # ── Input schema ────────────────────────────────────────────────────────── class PredictInput(BaseModel): input: List[List[float]] COLUMNS = [ "model_key", "mileage", "engine_power", "fuel", "paint_color", "car_type", "private_parking_available", "has_gps", "has_air_conditioning", "automatic_car", "has_getaround_connect", "has_speed_regulator", "winter_tires" ] # ── /predict ────────────────────────────────────────────────────────────── @app.post("/predict") def predict(data: PredictInput): df = pd.DataFrame(data.input, columns=COLUMNS) # Convert types for col in ["mileage", "engine_power"]: df[col] = df[col].astype(float) for col in ["private_parking_available", "has_gps", "has_air_conditioning", "automatic_car", "has_getaround_connect", "has_speed_regulator", "winter_tires"]: df[col] = df[col].astype(bool) predictions = model.predict(df).tolist() return {"prediction": predictions} # ── /docs ───────────────────────────────────────────────────────────────── @app.get("/docs", response_class=HTMLResponse) def docs(): return """ Getaround API Documentation

getaround

API DOCS

Getaround Pricing API

API de prédiction de prix pour l'optimisation tarifaire des véhicules Getaround.

POST /predict

Prédit le prix optimal par jour pour un ou plusieurs véhicules en fonction de leurs caractéristiques.

ChampTypeDescription
inputarray of arraysListe de véhicules, chaque véhicule est un tableau de 13 valeurs
#ChampTypeExemple
0model_keystring"Renault"
1mileagefloat80000
2engine_powerfloat120
3fuelstring"diesel"
4paint_colorstring"black"
5car_typestring"sedan"
6private_parking_availablebooltrue
7has_gpsbooltrue
8has_air_conditioningbooltrue
9automatic_carboolfalse
10has_getaround_connectbooltrue
11has_speed_regulatorbooltrue
12winter_tiresboolfalse
curl -X POST https://your-space.hf.space/predict \\
  -H "Content-Type: application/json" \\
  -d '{"input": [["Renault", 80000, 120, "diesel", "black", "sedan", true, true, true, false, true, true, false]]}'
{"prediction": [145.0]}
💡 Vous pouvez passer plusieurs véhicules en même temps dans le tableau input.
GET /health

Vérifie que l'API est en ligne et que le modèle est bien chargé.

{"status": "ok", "model": "loaded"}
""" # ── /health ─────────────────────────────────────────────────────────────── @app.get("/health") def health(): return {"status": "ok", "model": "loaded" if model else "unavailable"}