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 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 en fonction de leurs caractéristiques.
Input — Body JSON
| Champ | Type | Description |
| input | array of arrays | Liste de véhicules, chaque véhicule est un tableau de 13 valeurs |
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://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]]}'
Exemple de réponse
{"prediction": [145.0]}
💡 Vous pouvez passer plusieurs véhicules en même temps dans le tableau input.
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"}