dvf-estimation / app.py
Koaris's picture
Update app.py
5337c4a verified
raw
history blame
3.27 kB
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)