Tahani1 commited on
Commit
653fcbc
·
verified ·
1 Parent(s): 4a63a7c

Create PricesHousesModel.py

Browse files
Files changed (1) hide show
  1. PricesHousesModel.py +40 -0
PricesHousesModel.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import joblib
3
+ !pip install gradio
4
+ import gradio as gr
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ # Download the model file if it doesn't exist locally
8
+ model_filename = "knn_house_model.pkl"
9
+ try:
10
+ model_path = hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename=model_filename)
11
+ except Exception as e:
12
+ print(f"Error downloading '{model_filename}' from Hugging Face Hub: {e}")
13
+ raise # Re-raise the exception to stop execution
14
+
15
+ # Load the trained model and preprocessing tools
16
+ model = joblib.load(model_path) # Load the model from the downloaded path
17
+ scaler = joblib.load(hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename="scaler.pkl"))
18
+ label_encoder = joblib.load(hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename="label_encoder.pkl"))
19
+
20
+ # Function to predict house price
21
+ def predict_price(num_rooms, distance, country, build_quality):
22
+ country_encoded = label_encoder.transform([country])[0]
23
+ features = np.array([[num_rooms, distance, country_encoded, build_quality]])
24
+ features_scaled = scaler.transform(features)
25
+ predicted_price = model.predict(features_scaled)[0]
26
+ return f"Predicted House Price: ${predicted_price:,.2f}"
27
+
28
+ # Gradio Interface
29
+ inputs = [
30
+ gr.Number(label="Number of Rooms"),
31
+ gr.Number(label="Distance to Center (km)"),
32
+ gr.Dropdown(label="Country", choices=label_encoder.classes_.tolist()),
33
+ gr.Slider(minimum=1, maximum=10, label="Build Quality")
34
+ ]
35
+
36
+ outputs = gr.Textbox(label="Prediction Result")
37
+
38
+ # Create and launch Gradio app
39
+ app = gr.Interface(fn=predict_price, inputs=inputs, outputs=outputs, title="House Price Prediction")
40
+ app.launch()