|
import numpy as np |
|
import joblib |
|
!pip install gradio |
|
import gradio as gr |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
model_filename = "knn_house_model.pkl" |
|
try: |
|
model_path = hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename=model_filename) |
|
except Exception as e: |
|
print(f"Error downloading '{model_filename}' from Hugging Face Hub: {e}") |
|
raise |
|
|
|
|
|
model = joblib.load(model_path) |
|
scaler = joblib.load(hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename="scaler.pkl")) |
|
label_encoder = joblib.load(hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename="label_encoder.pkl")) |
|
|
|
|
|
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}" |
|
|
|
|
|
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") |
|
|
|
|
|
app = gr.Interface(fn=predict_price, inputs=inputs, outputs=outputs, title="House Price Prediction") |
|
app.launch() |