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 Pricing API
API de prédiction de prix pour l'optimisation tarifaire des véhicules Getaround.
Prédit le prix optimal par jour pour un ou plusieurs véhicules.
Ordre des valeurs par véhicule
| # | Champ | Type | Exemple |
| 0 | model_key | string | "Renault" |
| 1 | mileage | float | 80000 |
| 2 | engine_power | float | 120 |
| 3 | fuel | string | "diesel" |
| 4 | paint_color | string | "black" |
| 5 | car_type | string | "sedan" |
| 6 | private_parking_available | bool | true |
| 7 | has_gps | bool | true |
| 8 | has_air_conditioning | bool | true |
| 9 | automatic_car | bool | false |
| 10 | has_getaround_connect | bool | true |
| 11 | has_speed_regulator | bool | true |
| 12 | winter_tires | bool | false |
Exemple de requête
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]]}'
Exemple de réponse
{"prediction": [143.43]}
💡 Swagger interactif disponible sur
/docs
Vérifie que l'API est en ligne et que le modèle est bien chargé.
Exemple de réponse
{"status": "ok", "model": "loaded"}
"""
# ── /health ───────────────────────────────────────────────────────────────
@app.get("/health")
def health():
return {"status": "ok", "model": "loaded" if model else "unavailable"}