Spaces:
Sleeping
Sleeping
| import joblib | |
| import pandas as pd | |
| from fastapi import FastAPI | |
| from fastapi.responses import HTMLResponse | |
| from pydantic import BaseModel | |
| app = FastAPI( | |
| title="API de prédiction de prix Getaround", | |
| description="API permettant de prédire le prix de location par jour d’un véhicule", | |
| version="1.0", | |
| docs_url="/api-docs" | |
| ) | |
| model = joblib.load("model.joblib") | |
| class CarFeatures(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 | |
| def accueil(): | |
| return {"message": "Bienvenue sur l'API de prédiction de prix Getaround"} | |
| def predict(features: CarFeatures): | |
| data = pd.DataFrame([features.model_dump()]) | |
| prediction = model.predict(data)[0] | |
| return { | |
| "prix_location_par_jour": round(prediction, 2) | |
| } | |
| def documentation(): | |
| return """ | |
| <html> | |
| <head> | |
| <title>Documentation API Getaround</title> | |
| </head> | |
| <body> | |
| <h1>🚗 API de prédiction de prix Getaround</h1> | |
| <p> | |
| Cette API permet de prédire le prix de location par jour d’un véhicule | |
| en fonction de ses caractéristiques. | |
| </p> | |
| <h2>Endpoints disponibles</h2> | |
| <h3>GET /</h3> | |
| <p><strong>Description :</strong> retourne un message de bienvenue.</p> | |
| <p><strong>Entrée :</strong> aucune.</p> | |
| <p><strong>Sortie attendue :</strong></p> | |
| <pre> | |
| { | |
| "message": "Bienvenue sur l'API de prédiction de prix Getaround" | |
| } | |
| </pre> | |
| <h3>POST /predict</h3> | |
| <p><strong>Description :</strong> prédit le prix de location par jour d’un véhicule.</p> | |
| <p><strong>Entrée requise :</strong> un objet JSON contenant les caractéristiques du véhicule.</p> | |
| <pre> | |
| { | |
| "model_key": "Peugeot", | |
| "mileage": 50000, | |
| "engine_power": 120, | |
| "fuel": "diesel", | |
| "paint_color": "black", | |
| "car_type": "sedan", | |
| "private_parking_available": true, | |
| "has_gps": true, | |
| "has_air_conditioning": true, | |
| "automatic_car": false, | |
| "has_getaround_connect": true, | |
| "has_speed_regulator": true, | |
| "winter_tires": false | |
| } | |
| </pre> | |
| <p><strong>Sortie attendue :</strong></p> | |
| <pre> | |
| { | |
| "prix_location_par_jour": 100.35 | |
| } | |
| </pre> | |
| <h2>Tester l’API</h2> | |
| <p> | |
| Une documentation interactive Swagger est disponible ici : | |
| <a href="/api-docs">/api-docs</a> | |
| </p> | |
| </body> | |
| </html> | |
| """ |