Tahani1 commited on
Commit
4a63a7c
·
verified ·
1 Parent(s): 78dffb0

Create HousePricesModel.py

Browse files
Files changed (1) hide show
  1. HousePricesModel.py +30 -0
HousePricesModel.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import joblib
3
+ import gradio as gr
4
+
5
+ # Load the trained model and preprocessing tools
6
+ model = joblib.load("knn_house_model.pkl")
7
+ scaler = joblib.load("scaler.pkl")
8
+ label_encoder = joblib.load("label_encoder.pkl")
9
+
10
+ # Function to predict house price
11
+ def predict_price(num_rooms, distance, country, build_quality):
12
+ country_encoded = label_encoder.transform([country])[0]
13
+ features = np.array([[num_rooms, distance, country_encoded, build_quality]])
14
+ features_scaled = scaler.transform(features)
15
+ predicted_price = model.predict(features_scaled)[0]
16
+ return f"Predicted House Price: ${predicted_price:,.2f}"
17
+
18
+ # Gradio Interface
19
+ inputs = [
20
+ gr.Number(label="Number of Rooms"),
21
+ gr.Number(label="Distance to Center (km)"),
22
+ gr.Dropdown(label="Country", choices=label_encoder.classes_.tolist()),
23
+ gr.Slider(minimum=1, maximum=10, label="Build Quality")
24
+ ]
25
+
26
+ outputs = gr.Textbox(label="Prediction Result")
27
+
28
+ # Create and launch Gradio app
29
+ app = gr.Interface(fn=predict_price, inputs=inputs, outputs=outputs, title="House Price Prediction")
30
+ app.launch()