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

Deploy clean GetAround API

Browse files
Files changed (1) hide show
  1. app.py +25 -48
app.py CHANGED
@@ -1,59 +1,36 @@
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} โ‚ฌ")
 
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
+ """