File size: 3,272 Bytes
1d10686
 
 
 
 
 
 
 
 
e271498
1d10686
d75c03f
1d10686
 
 
 
 
 
 
 
 
 
e271498
e13e3c0
a0637de
5337c4a
e271498
 
9e7c60b
e271498
 
9e7c60b
 
e271498
1d10686
 
 
 
 
99bf7e7
e271498
6f86e87
e271498
 
 
 
99bf7e7
e271498
 
99bf7e7
1d10686
 
 
 
7a57b18
1d10686
 
 
 
 
37441ef
1d10686
e271498
 
 
 
 
1d10686
 
 
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
import pandas as pd
import numpy as np
import requests
import gradio as gr
import joblib
from sklearn.ensemble import RandomForestRegressor
from huggingface_hub import hf_hub_download
from gradio_calendar import Calendar
from datetime import datetime
from datasets import load_dataset

REPO_ID = "Koaris/rf_france_07042024"
FILENAME = "random_forest_france_07_04_2024.pkl"
API_KEY = "AIzaSyBtymzLFj2GfDr-zkGjdDa3FH_senLwNkw"

def get_lat_lon(address,api_key=API_KEY):
    response = requests.get(f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={api_key}')

    resp_json_payload = response.json()
    coordinates = resp_json_payload['results'][0]['geometry']['location']
    return (coordinates['lat'], coordinates['lng'])

def compute_reliability(std_pred: float):
    scores = pd.read_csv('scores_13072024.csv').iloc[:,1].tolist()
    print(scores)
    ct = 5
    for std_set in scores:
        if std_pred < std_set:
            reliability = ct
        else:
            ct-=1
            reliability = ct
    return reliability

def predict_price(date: datetime ,room_count: int, address:str, surface: float , property_type: str ):
    date = int(datetime.timestamp(date))
    latitude, longitude = get_lat_lon(address)
    isHouse = (property_type == 'Maison')
    rf_input = pd.DataFrame([{"date_mutation": date, "nombre_pieces_principales": room_count, "longitude" : longitude,"latitude":latitude, "surface_batie_totale": surface, "type_local_Maison": isHouse}])
    rf_pred = np.exp(rf.predict(rf_input)[0])
    predictions_all = np.array([tree.predict(rf_input) for tree in rf.estimators_])
    std_predict = np.std((predictions_all))
    reliability_index = compute_reliability(std_predict)
    q1 = np.exp(np.quantile(predictions_all, 0.25))
    q2 = np.exp(np.quantile(predictions_all, 0.5))
    q3 = np.exp(np.quantile(predictions_all, 0.75))
    if (rf_pred <= q1) | (rf_pred >= q3):
        return float(q1), float(q2), float(q3), reliability_index
    else:
        return float(q1), float(rf_pred), float(q3), reliability_index



if __name__ == '__main__':
    rf = joblib.load(hf_hub_download(repo_id=REPO_ID, filename= FILENAME))
    with gr.Blocks() as demo:
        date = Calendar(type='datetime', label='Date', info="Cliquez sur le calendrier pour choisir une date",value="2024-04-06")
        room_count = gr.Slider(minimum=1,maximum=15, step=1, label='Nombre de Pieces')
        address = gr.Text(value='8 Rue de la Boetie', placeholder="Saisissez l'addresse du bien", label = 'Addresse')
        surface = gr.Number(label='Surface', info='Saisissez la surface du bien',value=30)
        property_type = gr.Dropdown(choices=['Maison','Appartement'], label='Type de bien', info='Choisissez le type de bien')
        estimation_button = gr.Button("Estimez le bien")
        output1 = gr.Number(label='Estimation basse du prix au m2')
        output2 = gr.Number(label='Estimation du prix au m2')
        output3 = gr.Number(label='Estimation haute du prix au m2')
        output4 = gr.Number(label='Indice de fiabilite')
        estimation_button.click(fn=predict_price, inputs=[date, room_count, address, surface, property_type], outputs=[output1,output2,output3,output4],api_name='Estimation')


demo.launch(share=True)