Dreipfelt commited on
Commit
cead9f4
ยท
1 Parent(s): 1bfce3f

Deploy clean API

Browse files
Files changed (4) hide show
  1. app.py +50 -78
  2. columns.pkl +3 -0
  3. model.pkl +3 -0
  4. requirements.txt +4 -4
app.py CHANGED
@@ -1,87 +1,59 @@
1
- from fastapi import FastAPI
2
- from fastapi.responses import HTMLResponse
3
- from pydantic import BaseModel
4
- import joblib
5
- import numpy as np
6
  import pandas as pd
 
7
 
8
- # โ”€โ”€ Load model โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
9
- model = joblib.load("model.pkl")
10
-
11
- # โ”€โ”€ Initialize app โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
12
- app = FastAPI(
13
- title="GetAround Pricing API",
14
- description="Predicts the optimal rental price per day for a car",
15
- version="1.0.0"
16
- )
17
-
18
- # โ”€โ”€ Input schema โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
19
- class PredictInput(BaseModel):
20
- input: list
21
 
22
- # โ”€โ”€ Root route โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
23
- @app.get("/", response_class=HTMLResponse)
24
- def root():
25
- return """
26
- <html>
27
- <body style="font-family: Arial; text-align: center; padding: 50px;">
28
- <h1>๐Ÿš— GetAround Pricing API</h1>
29
- <p>API is running!</p>
30
- <a href="/docs">๐Ÿ“„ Go to Documentation</a>
31
- </body>
32
- </html>
33
- """
34
 
35
- # โ”€โ”€ /predict route โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
36
  @app.post("/predict")
37
  def predict(data: PredictInput):
38
- X = pd.DataFrame(data.input)
39
  predictions = model.predict(X)
40
  return {"prediction": [round(float(p), 2) for p in predictions]}
41
 
42
- # โ”€โ”€ /docs route โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
43
- @app.get("/docs", response_class=HTMLResponse)
44
- def documentation():
45
- return """
46
- <!DOCTYPE html>
47
- <html lang="en">
48
- <head>
49
- <meta charset="UTF-8">
50
- <title>GetAround API Documentation</title>
51
- <style>
52
- body { font-family: Arial, sans-serif; max-width: 800px; margin: 40px auto; padding: 0 20px; }
53
- h1 { color: #1a1a2e; }
54
- .endpoint { background: #f5f7fa; border-radius: 8px; padding: 20px; margin: 20px 0; }
55
- .badge { padding: 4px 12px; border-radius: 12px; font-weight: bold; font-size: 0.85em; }
56
- .post { background: #d4edda; color: #155724; }
57
- .get { background: #cce5ff; color: #004085; }
58
- pre { background: #1a1a2e; color: #00d4aa; padding: 15px; border-radius: 6px; overflow-x: auto; }
59
- </style>
60
- </head>
61
- <body>
62
- <h1>๐Ÿš— GetAround Pricing API</h1>
63
- <p>Predict the optimal rental price per day for any car.</p>
64
-
65
- <div class="endpoint">
66
- <h2><span class="badge post">POST</span> /predict</h2>
67
- <p>Returns a predicted rental price per day.</p>
68
- <h3>Request example</h3>
69
- <pre>curl -X POST "https://Dreipfelt-getaround-api.hf.space/predict" \
70
- -H "Content-Type: application/json" \
71
- -d '{"input": [[7.0, 0.27, 0.36, 20.7, 0.045, 45.0, 170.0, 1.001, 3.0, 0.45, 8.8]]}'</pre>
72
- <h3>Response example</h3>
73
- <pre>{"prediction": [89.5]}</pre>
74
- </div>
75
-
76
- <div class="endpoint">
77
- <h2><span class="badge get">GET</span> /</h2>
78
- <p>Health check โ€” confirms the API is running.</p>
79
- </div>
80
-
81
- <div class="endpoint">
82
- <h2><span class="badge get">GET</span> /docs</h2>
83
- <p>This documentation page.</p>
84
- </div>
85
- </body>
86
- </html>
87
- """
 
1
+ # app.py
2
+ import os
3
+ import streamlit as st
 
 
4
  import pandas as pd
5
+ import joblib
6
 
7
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 1๏ธโƒฃ Lancement automatique du modรจle โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
8
+ MODEL_PATH = "model.pkl"
9
+ if not os.path.exists(MODEL_PATH):
10
+ st.info("Le modรจle n'existe pas, lancement de l'entraรฎnement...")
11
+ st.success("Modรจle entraรฎnรฉ et sauvegardรฉ โœ…")
 
 
 
 
 
 
 
 
12
 
13
+ # โ”€โ”€ Load model and columns โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
14
+ model = joblib.load("model.pkl")
15
+ columns = joblib.load("columns.pkl")
 
 
 
 
 
 
 
 
 
16
 
 
17
  @app.post("/predict")
18
  def predict(data: PredictInput):
19
+ X = pd.DataFrame(data.input, columns=columns)
20
  predictions = model.predict(X)
21
  return {"prediction": [round(float(p), 2) for p in predictions]}
22
 
23
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 3๏ธโƒฃ Dรฉfinir les colonnes/features utilisรฉes โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
24
+ features = [
25
+ 'mileage', 'engine_power', 'fuel', 'paint_color', 'car_type',
26
+ 'private_parking_available', 'has_gps', 'has_air_conditioning',
27
+ 'automatic_car', 'has_getaround_connect', 'has_speed_regulator', 'winter_tires'
28
+ ]
29
+
30
+ categorical_options = {
31
+ 'fuel': ['diesel', 'gasoline', 'electric', 'hybrid'],
32
+ 'paint_color': ['white', 'black', 'grey', 'blue', 'red', 'green'],
33
+ 'car_type': ['sedan', 'suv', 'convertible', 'coupe', 'van']
34
+ }
35
+
36
+ st.title("GetAround Pricing Prediction ๐Ÿš—")
37
+
38
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 4๏ธโƒฃ Collecte des inputs utilisateur โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
39
+ input_data = {}
40
+ for col in features:
41
+ if col in categorical_options:
42
+ input_data[col] = st.selectbox(f"{col}", categorical_options[col])
43
+ elif col in ["private_parking_available", "has_gps", "has_air_conditioning", "automatic_car",
44
+ "has_getaround_connect", "has_speed_regulator", "winter_tires"]:
45
+ input_data[col] = st.checkbox(col)
46
+ else:
47
+ input_data[col] = st.number_input(col, min_value=0, value=0)
48
+
49
+ df_input = pd.DataFrame([input_data])
50
+ df_input = pd.get_dummies(df_input)
51
+ for c in model.feature_names_in_:
52
+ if c not in df_input.columns:
53
+ df_input[c] = 0
54
+ df_input = df_input[model.feature_names_in_]
55
+
56
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 5๏ธโƒฃ Faire la prรฉdiction โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
57
+ if st.button("Prรฉdire le prix"):
58
+ prediction = model.predict(df_input)[0]
59
+ st.success(f"Prix estimรฉ par jour : {prediction:.2f} โ‚ฌ")
 
 
 
 
 
 
 
 
 
columns.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3c98f3c5b807c62a5797213ad0a1732274ad49eb6b427c4ffa6a310fa0c0e80d
3
+ size 532
model.pkl CHANGED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:55176cd9b7b23ea78995547f223f074b11b9583e3dcd20272f923e505ce66efd
3
+ size 288033
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
  fastapi
2
  uvicorn
3
- joblib
4
- numpy
5
  pandas
6
- scikit-learn==1.7.2
7
- xgboost==3.1.2
 
 
 
1
  fastapi
2
  uvicorn
 
 
3
  pandas
4
+ scikit-learn>=1.4
5
+ joblib==1.2.0
6
+ streamlit
7
+