Spaces:
Running
Running
Deploy clean API
Browse files- app.py +50 -78
- columns.pkl +3 -0
- model.pkl +3 -0
- requirements.txt +4 -4
app.py
CHANGED
|
@@ -1,87 +1,59 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import joblib
|
| 5 |
-
import numpy as np
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
|
| 8 |
-
# โโ
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 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 |
-
# โโ
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
# โโ
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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=
|
| 7 |
-
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
|
|
|
|
|
|
| 3 |
pandas
|
| 4 |
+
scikit-learn>=1.4
|
| 5 |
+
joblib==1.2.0
|
| 6 |
+
streamlit
|
| 7 |
+
|