File size: 1,091 Bytes
4a63a7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import joblib
import gradio as gr

 # Load the trained model and preprocessing tools
model = joblib.load("knn_house_model.pkl")
scaler = joblib.load("scaler.pkl")
label_encoder = joblib.load("label_encoder.pkl")

 # Function to predict house price
def predict_price(num_rooms, distance, country, build_quality):
    country_encoded = label_encoder.transform([country])[0]
    features = np.array([[num_rooms, distance, country_encoded, build_quality]])
    features_scaled = scaler.transform(features)
    predicted_price = model.predict(features_scaled)[0]
    return f"Predicted House Price: ${predicted_price:,.2f}"

 # Gradio Interface
inputs = [
    gr.Number(label="Number of Rooms"),
    gr.Number(label="Distance to Center (km)"),
    gr.Dropdown(label="Country", choices=label_encoder.classes_.tolist()),
    gr.Slider(minimum=1, maximum=10, label="Build Quality")
]

 outputs = gr.Textbox(label="Prediction Result")

 # Create and launch Gradio app
app = gr.Interface(fn=predict_price, inputs=inputs, outputs=outputs, title="House Price Prediction")
app.launch()