File size: 6,634 Bytes
fdc8a15
 
 
 
 
 
 
bc62674
 
 
 
fdc8a15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42d4ae9
 
fdc8a15
 
 
 
7b1aef7
2f3d798
4d40ca4
fdc8a15
 
 
 
 
 
 
 
bc62674
fdc8a15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ff2380
d5c9f4f
fdc8a15
 
 
 
 
 
 
 
 
 
c85fbe2
 
fdc8a15
 
 
c85fbe2
 
bc62674
fdc8a15
 
 
 
bc62674
 
5fb71bf
bf55dba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import streamlit as st
from pydantic import BaseModel
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import requests
import logging

logger = logging.getLogger(__name__)

# ======================================================
# 🌐 API Pricing HF
# ======================================================

API_URL = "https://pradelf-getaround-api.hf.space/predict"


class RentalFeatures(BaseModel):
    model_key: str = "Peugeot"
    mileage: int = 0
    engine_power: int = 100
    fuel: str = "petrol"
    paint_color: str = "black"
    car_type: str = "sedan"
    private_parking_available: int = 1
    has_gps: int = 0
    has_air_conditioning: int = 0
    automatic_car: int = 0
    has_getaround_connect: int = 0
    has_speed_regulator: int = 0
    winter_tires: int = 0



def call_pricing_api(featRentalFeatures: RentalFeatures):
    """
    Appelle l'API FastAPI déployée sur Hugging Face.
    Retourne le prix prédit (float) ou None en cas d'erreur.
    """
    #payload = {"input": [featRentalFeatures.model_dump()]}
    payload = featRentalFeatures.model_dump()
    try:
        resp = requests.post(API_URL, json=payload, timeout=10)
        resp.raise_for_status()
        data = resp.json()
        logger.info(data)
        if "prediction" in data:
            return float(data["prediction"])
        else:
            st.error("Réponse API inattendue : clé 'prediction' absente.")
            return None, payload, data
    except Exception as e:
        st.error(f"Erreur lors de l'appel à l'API : {e}")
        return None, payload, None


logger.info('Started')
### CONFIG
st.set_page_config(page_title="Location Voiture", page_icon="💸", layout="wide")

### TITLE AND TEXT
st.title("Tableau de bord de Get Around")

st.markdown("""
    Ce site représente le **dashboard** destiné à aider l’équipe Produit pour appréhender l'introduction 
    de la nouvelle fonctionnalité consistant en l'implémentation d'un **délai minimum entre deux locations**.<br/> 
    Un véhicule ne sera pas affiché dans les résultats de recherche si les heures de check-in 
    ou de check-out demandées sont trop proches d’une location déjà existante.<br/>
             
    Grâce à ce site, l'équipe produit pourra répondre aux questions suivantes :<br/>
    - quelle doit être la durée minimale du délai entre deux locations ?
    - faut-il activer cette fonctionnalité pour tous les véhicules ou uniquement pour les véhicules Connect ?
    - Quelle part des revenus des propriétaires serait potentiellement affectée par cette fonctionnalité ?
    - Combien de locations seraient impactées en fonction du seuil et du périmètre choisis ?
    - À quelle fréquence les conducteurs sont-ils en retard pour le check-in suivant ? Quel est l’impact pour le conducteur suivant ?
    - Combien de situations problématiques seraient résolues selon le seuil et le périmètre retenus ?
            
    La documentation du web serveur de fonctions est déploiée sur l'url : [https://pradelf-getaround-api.hf.space/docs](https://pradelf-getaround-api.hf.space/docs)        """)
### LOAD DATA
DATA_PRICING = "/app/Data/get_around_pricing_project.csv"
DATA_PRICING_HF = "https://huggingface.co/datasets/pradelf/getaround-dataset/blob/main/get_around_pricing_project.csv"

DATA_DELAY = "/app/Data/get_around_delay_analysis.csv"
DATA_DELAY_HF = "https://huggingface.co/datasets/pradelf/getaround-dataset/blob/main/get_around_delay_analysis.csv"

# usage d'un décorateur python pour ajouter des fonctionnalité
# : st.cache_data et st.cache_resource qui remplace st.cache qui va devenir obsolète.
# https://docs.streamlit.io/develop/api-reference/caching-and-state/st.cache_data
# https://docs.streamlit.io/develop/api-reference/caching-and-state/st.cache_resource
@st.cache_data
def load_data(file, nrows, delimiter=","):
    data = pd.read_csv(file, nrows=nrows, delimiter=delimiter)
    # data["Date"] = data["Date"].apply(lambda x: pd.to_datetime(",".join(x.split(",")[-2:])))
    # data["currency"] = data["currency"].apply(lambda x: pd.to_numeric(x[1:]))
    return data


data_load_state = st.text("Chargement des données...")
data_pricing = load_data(DATA_PRICING, 1000)
data_analysis = load_data(DATA_DELAY, 1000, ";")
data_load_state.text(
    ""
)  # change text from "Loading data..." to "" once the the load_data function has run

## Run the below code if the check is checked ✅
if st.checkbox("Montrer les données brutes sur le prix de location."):
    st.subheader("Tarif de location")
    st.write(data_pricing)
## Run the below code if the check is checked ✅
if st.checkbox("Montrer les données brutes sur l'analyse des retards de check-in."):
    st.subheader("Retard de check-in")
    st.write(data_analysis)
### SHOW GRAPH STREAMLIT

# price_per_model = data_pricing["price"]
# st.bar_chart(price_per_model)


### SIDEBAR
st.sidebar.header("Tableau de bord GetAround")
st.sidebar.markdown("""
    * GetAround projet
""")
e = st.sidebar.empty()
e.write("")
st.sidebar.write("GetAround Projet")

### EXPANDER

with st.expander(
    "⏯️ Regardez cette vidéo d'une minute pour comprendre comment fonctionne Getaround."
):
    st.video("https://youtu.be/3LyzwpGSfzE")

st.markdown("---")
st.markdown("Estimation de la location")
with st.form("average_sales_per_country"):
        model = st.selectbox(
            "Sélectionnez un modèle de voiture pour voir le prix de location moyen",
            data_pricing["model_key"].sort_values().unique(),
        )
        power = st.selectbox(
            "Sélectionnez la puissance du moteur",
            data_pricing["engine_power"].sort_values().unique(),
        )
        submit = st.form_submit_button("submit")
        if submit:
            
            car_select=RentalFeatures(model_key=model, engine_power=power)
            # avg_rental_price = data_pricing[model_select & power_select][
            #    "rental_price_per_day"
            # ].mean()
            # model_select = data_pricing[data_pricing["model_key"] == model]
            # power_select = data_pricing[data_pricing["engine_power"] == power]
            logger.info(car_select)
            rental_price = call_pricing_api(
                car_select
            )
            # avg_rental_price = 0.0
            logger.info("##############")
            logger.info(rental_price)
            st.metric(f"Prix de location moyen (en $) :",f"{float(rental_price):.2f}")
# exemple de véhicule du site avec les données : https://fr.getaround.com/location-voiture/paris/citroen-c3-1608526