import numpy as np import joblib import gradio as gr from huggingface_hub import hf_hub_download # Download the model file if it doesn't exist locally 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 # Re-raise the exception to stop execution # Load the trained model and preprocessing tools model = joblib.load(model_path) # Load the model from the downloaded 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")) # 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()