Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from sklearn.ensemble import RandomForestRegressor
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from gradio_calendar import Calendar
|
| 9 |
from datetime import datetime
|
|
|
|
| 10 |
|
| 11 |
REPO_ID = "Koaris/rf_france_07042024"
|
| 12 |
FILENAME = "random_forest_france_07_04_2024.pkl"
|
|
@@ -19,6 +20,16 @@ def get_lat_lon(address,api_key=API_KEY):
|
|
| 19 |
coordinates = resp_json_payload['results'][0]['geometry']['location']
|
| 20 |
return (coordinates['lat'], coordinates['lng'])
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def predict_price(date: datetime ,room_count: int, address:str, surface: float , property_type: str ):
|
| 23 |
date = int(datetime.timestamp(date))
|
| 24 |
print(date)
|
|
@@ -27,8 +38,18 @@ def predict_price(date: datetime ,room_count: int, address:str, surface: float ,
|
|
| 27 |
rf_input = pd.DataFrame([{"date_mutation": date, "nombre_pieces_principales": room_count, "longitude" : longitude,"latitude":latitude, "surface_batie_totale": surface, "type_local_Maison": isHouse}])
|
| 28 |
print(rf_input)
|
| 29 |
rf_pred = rf.predict(rf_input)[0]
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
|
|
@@ -41,8 +62,11 @@ if __name__ == '__main__':
|
|
| 41 |
surface = gr.Number(label='Surface', info='Saisissez la surface du bien',value=30)
|
| 42 |
property_type = gr.Dropdown(choices=['Maison','Appartement'], label='Type de bien', info='Choisissez le type de bien')
|
| 43 |
estimation_button = gr.Button("Estimez le bien")
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
demo.launch(share=True)
|
|
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from gradio_calendar import Calendar
|
| 9 |
from datetime import datetime
|
| 10 |
+
from datasets import load_dataset
|
| 11 |
|
| 12 |
REPO_ID = "Koaris/rf_france_07042024"
|
| 13 |
FILENAME = "random_forest_france_07_04_2024.pkl"
|
|
|
|
| 20 |
coordinates = resp_json_payload['results'][0]['geometry']['location']
|
| 21 |
return (coordinates['lat'], coordinates['lng'])
|
| 22 |
|
| 23 |
+
def compute_reliability(std_pred: float):
|
| 24 |
+
scores = load_dataset('Koaris/quantiles_predict/scores_13072024.csv')
|
| 25 |
+
scores = scores.tolist()
|
| 26 |
+
ct = 5
|
| 27 |
+
for std_set in scores:
|
| 28 |
+
if std_pred < std_set:
|
| 29 |
+
return ct
|
| 30 |
+
else:
|
| 31 |
+
ct-=1
|
| 32 |
+
|
| 33 |
def predict_price(date: datetime ,room_count: int, address:str, surface: float , property_type: str ):
|
| 34 |
date = int(datetime.timestamp(date))
|
| 35 |
print(date)
|
|
|
|
| 38 |
rf_input = pd.DataFrame([{"date_mutation": date, "nombre_pieces_principales": room_count, "longitude" : longitude,"latitude":latitude, "surface_batie_totale": surface, "type_local_Maison": isHouse}])
|
| 39 |
print(rf_input)
|
| 40 |
rf_pred = rf.predict(rf_input)[0]
|
| 41 |
+
predictions_all = np.array([tree.predict(rf_input) for tree in rf.estimators_])
|
| 42 |
+
std_predict = np.std(predictions_all)
|
| 43 |
+
reliability_index = compute_reliability(std_predict)
|
| 44 |
+
q1 = np.exp(np.quantile(predictions_all, 0.25))
|
| 45 |
+
q2 = np.exp(np.quantile(predictions_all, 0.5))
|
| 46 |
+
q3 = np.exp(np.quantile(predictions_all, 0.75))
|
| 47 |
+
if (rf_pred_exp <= q1) | (rf_pred_exp >= q3):
|
| 48 |
+
print(f"Estimated Price: {np.exp(q2)} /n Low Price: {q1}, High Price: {q3}")
|
| 49 |
+
return float(q1), float(q2), float(q3), reliability_index
|
| 50 |
+
else:
|
| 51 |
+
print(f"Estimated Price: {rf_pred_exp} /n Low Price: {q1}, High Price: {q3}")
|
| 52 |
+
return float(q1), float(rf_pred_exp), float(q3), reliability_index
|
| 53 |
|
| 54 |
|
| 55 |
|
|
|
|
| 62 |
surface = gr.Number(label='Surface', info='Saisissez la surface du bien',value=30)
|
| 63 |
property_type = gr.Dropdown(choices=['Maison','Appartement'], label='Type de bien', info='Choisissez le type de bien')
|
| 64 |
estimation_button = gr.Button("Estimez le bien")
|
| 65 |
+
output1 = gr.Number(label='Estimation basse du prix au m2')
|
| 66 |
+
output2 = gr.Number(label='Estimation du prix au m2')
|
| 67 |
+
output3 = gr.Number(label='Estimation haute du prix au m2')
|
| 68 |
+
output4 = gr.Number(label='Indice de fiabilite')
|
| 69 |
+
estimation_button.click(fn=predict_price, inputs=[date, room_count, address, surface, property_type], outputs=[output1,output2,output3,output4],api_name='Estimation')
|
| 70 |
|
| 71 |
|
| 72 |
demo.launch(share=True)
|