Spaces:
Running
Running
Deploy clean GetAround API
Browse files
app.py
CHANGED
|
@@ -1,59 +1,36 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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} โฌ")
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from pydantic import BaseModel
|
|
|
|
| 4 |
import joblib
|
| 5 |
+
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
model = joblib.load("model.pkl")
|
| 8 |
columns = joblib.load("columns.pkl")
|
| 9 |
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
class PredictInput(BaseModel):
|
| 13 |
+
input: list
|
| 14 |
+
|
| 15 |
+
@app.get("/", response_class=HTMLResponse)
|
| 16 |
+
def root():
|
| 17 |
+
return "<h1>๐ GetAround Pricing API</h1><a href='/docs'>๐ Documentation</a>"
|
| 18 |
+
|
| 19 |
@app.post("/predict")
|
| 20 |
def predict(data: PredictInput):
|
| 21 |
X = pd.DataFrame(data.input, columns=columns)
|
| 22 |
predictions = model.predict(X)
|
| 23 |
return {"prediction": [round(float(p), 2) for p in predictions]}
|
| 24 |
|
| 25 |
+
@app.get("/docs", response_class=HTMLResponse)
|
| 26 |
+
def documentation():
|
| 27 |
+
return """
|
| 28 |
+
<html><body style="font-family:Arial;max-width:800px;margin:40px auto">
|
| 29 |
+
<h1>๐ GetAround Pricing API</h1>
|
| 30 |
+
<h2>POST /predict</h2>
|
| 31 |
+
<pre>curl -X POST "https://Dreipfelt-getaround-api.hf.space/predict" \
|
| 32 |
+
-H "Content-Type: application/json" \
|
| 33 |
+
-d '{"input": [[150000, 120, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]]}'</pre>
|
| 34 |
+
<h2>GET /</h2><p>Health check</p>
|
| 35 |
+
</body></html>
|
| 36 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|