NANA12A commited on
Commit
59cc5ec
Β·
verified Β·
1 Parent(s): 990e743

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +19 -33
api.py CHANGED
@@ -5,31 +5,30 @@ from typing import List, Any
5
  import joblib
6
  import pandas as pd
7
  import numpy as np
8
-
9
- app = FastAPI(docs_url=None, redoc_url=None)
10
-
11
  # ── Load model ────────────────────────────────────────────────────────────
12
  try:
13
  model = joblib.load("pricing_model.joblib")
14
  except:
15
  model = None
16
-
17
  # ── Input schema ──────────────────────────────────────────────────────────
18
  class PredictInput(BaseModel):
19
  input: List[List[Any]]
20
-
21
  COLUMNS = [
22
  "model_key", "mileage", "engine_power", "fuel", "paint_color",
23
  "car_type", "private_parking_available", "has_gps",
24
  "has_air_conditioning", "automatic_car", "has_getaround_connect",
25
  "has_speed_regulator", "winter_tires"
26
  ]
27
-
28
  # ── /predict ──────────────────────────────────────────────────────────────
29
  @app.post("/predict")
30
  def predict(data: PredictInput):
31
  df = pd.DataFrame(data.input, columns=COLUMNS)
32
- # Convert types
33
  for col in ["mileage", "engine_power"]:
34
  df[col] = df[col].astype(float)
35
  for col in ["private_parking_available", "has_gps", "has_air_conditioning",
@@ -37,10 +36,10 @@ def predict(data: PredictInput):
37
  df[col] = df[col].astype(bool)
38
  predictions = model.predict(df).tolist()
39
  return {"prediction": predictions}
40
-
41
- # ── /docs ─────────────────────────────────────────────────────────────────
42
- @app.get("/docs", response_class=HTMLResponse)
43
- def docs():
44
  return """
45
  <!DOCTYPE html>
46
  <html lang="en">
@@ -81,21 +80,13 @@ def docs():
81
  <main>
82
  <h2>Getaround Pricing API</h2>
83
  <p class="subtitle">API de prΓ©diction de prix pour l'optimisation tarifaire des vΓ©hicules Getaround.</p>
84
-
85
- <!-- /predict -->
86
  <div class="endpoint">
87
  <div class="endpoint-header">
88
  <span class="method">POST</span>
89
  <span class="path">/predict</span>
90
  </div>
91
- <p class="desc">PrΓ©dit le prix optimal par jour pour un ou plusieurs vΓ©hicules en fonction de leurs caractΓ©ristiques.</p>
92
-
93
- <div class="section-label">Input β€” Body JSON</div>
94
- <table>
95
- <tr><th>Champ</th><th>Type</th><th>Description</th></tr>
96
- <tr><td>input</td><td>array of arrays</td><td>Liste de vΓ©hicules, chaque vΓ©hicule est un tableau de 13 valeurs</td></tr>
97
- </table>
98
-
99
  <div class="section-label">Ordre des valeurs par vΓ©hicule</div>
100
  <table>
101
  <tr><th>#</th><th>Champ</th><th>Type</th><th>Exemple</th></tr>
@@ -113,19 +104,15 @@ def docs():
113
  <tr><td>11</td><td>has_speed_regulator</td><td>bool</td><td>true</td></tr>
114
  <tr><td>12</td><td>winter_tires</td><td>bool</td><td>false</td></tr>
115
  </table>
116
-
117
  <div class="section-label">Exemple de requΓͺte</div>
118
- <pre>curl -X POST https://your-space.hf.space/predict \\
119
- -H "Content-Type: application/json" \\
120
  -d '{"input": [["Renault", 80000, 120, "diesel", "black", "sedan", true, true, true, false, true, true, false]]}'</pre>
121
-
122
  <div class="section-label">Exemple de rΓ©ponse</div>
123
- <pre>{"prediction": [145.0]}</pre>
124
-
125
- <div class="note">πŸ’‘ Vous pouvez passer plusieurs vΓ©hicules en mΓͺme temps dans le tableau <code>input</code>.</div>
126
  </div>
127
-
128
- <!-- /health -->
129
  <div class="endpoint">
130
  <div class="endpoint-header">
131
  <span class="method get">GET</span>
@@ -135,13 +122,12 @@ def docs():
135
  <div class="section-label">Exemple de rΓ©ponse</div>
136
  <pre>{"status": "ok", "model": "loaded"}</pre>
137
  </div>
138
-
139
  </main>
140
  </body>
141
  </html>
142
  """
143
-
144
  # ── /health ───────────────────────────────────────────────────────────────
145
  @app.get("/health")
146
  def health():
147
- return {"status": "ok", "model": "loaded" if model else "unavailable"}
 
5
  import joblib
6
  import pandas as pd
7
  import numpy as np
8
+
9
+ app = FastAPI(title="Getaround Pricing API")
10
+
11
  # ── Load model ────────────────────────────────────────────────────────────
12
  try:
13
  model = joblib.load("pricing_model.joblib")
14
  except:
15
  model = None
16
+
17
  # ── Input schema ──────────────────────────────────────────────────────────
18
  class PredictInput(BaseModel):
19
  input: List[List[Any]]
20
+
21
  COLUMNS = [
22
  "model_key", "mileage", "engine_power", "fuel", "paint_color",
23
  "car_type", "private_parking_available", "has_gps",
24
  "has_air_conditioning", "automatic_car", "has_getaround_connect",
25
  "has_speed_regulator", "winter_tires"
26
  ]
27
+
28
  # ── /predict ──────────────────────────────────────────────────────────────
29
  @app.post("/predict")
30
  def predict(data: PredictInput):
31
  df = pd.DataFrame(data.input, columns=COLUMNS)
 
32
  for col in ["mileage", "engine_power"]:
33
  df[col] = df[col].astype(float)
34
  for col in ["private_parking_available", "has_gps", "has_air_conditioning",
 
36
  df[col] = df[col].astype(bool)
37
  predictions = model.predict(df).tolist()
38
  return {"prediction": predictions}
39
+
40
+ # ── /documentation β€” page HTML custom ────────────────────────────────────
41
+ @app.get("/documentation", response_class=HTMLResponse)
42
+ def documentation():
43
  return """
44
  <!DOCTYPE html>
45
  <html lang="en">
 
80
  <main>
81
  <h2>Getaround Pricing API</h2>
82
  <p class="subtitle">API de prΓ©diction de prix pour l'optimisation tarifaire des vΓ©hicules Getaround.</p>
83
+
 
84
  <div class="endpoint">
85
  <div class="endpoint-header">
86
  <span class="method">POST</span>
87
  <span class="path">/predict</span>
88
  </div>
89
+ <p class="desc">PrΓ©dit le prix optimal par jour pour un ou plusieurs vΓ©hicules.</p>
 
 
 
 
 
 
 
90
  <div class="section-label">Ordre des valeurs par vΓ©hicule</div>
91
  <table>
92
  <tr><th>#</th><th>Champ</th><th>Type</th><th>Exemple</th></tr>
 
104
  <tr><td>11</td><td>has_speed_regulator</td><td>bool</td><td>true</td></tr>
105
  <tr><td>12</td><td>winter_tires</td><td>bool</td><td>false</td></tr>
106
  </table>
 
107
  <div class="section-label">Exemple de requΓͺte</div>
108
+ <pre>curl -X POST https://nana12a-getaround-api.hf.space/predict \
109
+ -H "Content-Type: application/json" \
110
  -d '{"input": [["Renault", 80000, 120, "diesel", "black", "sedan", true, true, true, false, true, true, false]]}'</pre>
 
111
  <div class="section-label">Exemple de rΓ©ponse</div>
112
+ <pre>{"prediction": [143.43]}</pre>
113
+ <div class="note">πŸ’‘ Swagger interactif disponible sur <a href="/docs">/docs</a></div>
 
114
  </div>
115
+
 
116
  <div class="endpoint">
117
  <div class="endpoint-header">
118
  <span class="method get">GET</span>
 
122
  <div class="section-label">Exemple de rΓ©ponse</div>
123
  <pre>{"status": "ok", "model": "loaded"}</pre>
124
  </div>
 
125
  </main>
126
  </body>
127
  </html>
128
  """
129
+
130
  # ── /health ───────────────────────────────────────────────────────────────
131
  @app.get("/health")
132
  def health():
133
+ return {"status": "ok", "model": "loaded" if model else "unavailable"}