from fastapi import FastAPI from fastapi.responses import HTMLResponse from pydantic import BaseModel from typing import List, Any import joblib import pandas as pd import numpy as np app = FastAPI(title="Getaround Pricing API") # ── Load model ──────────────────────────────────────────────────────────── try: model = joblib.load("pricing_model.joblib") except: model = None # ── Input schema ────────────────────────────────────────────────────────── class PredictInput(BaseModel): input: List[List[Any]] 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) 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} # ── /documentation — page HTML custom ──────────────────────────────────── @app.get("/documentation", response_class=HTMLResponse) def documentation(): 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.

#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://nana12a-getaround-api.hf.space/predict \
  -H "Content-Type: application/json" \
  -d '{"input": [["Renault", 80000, 120, "diesel", "black", "sedan", true, true, true, false, true, true, false]]}'
{"prediction": [143.43]}
💡 Swagger interactif disponible sur /docs
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"}