Spaces:
Runtime error
Runtime error
Upload PricesHousesModel1.py.txt
Browse files- PricesHousesModel1.py.txt +37 -0
PricesHousesModel1.py.txt
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import joblib
|
3 |
+
import gradio as gr # ✅ استيراد Gradio بدون `!pip install`
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
# تحميل النموذج والبيانات المسبقة
|
7 |
+
model_filename = "knn_house_model.pkl"
|
8 |
+
try:
|
9 |
+
model_path = hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename=model_filename)
|
10 |
+
except Exception as e:
|
11 |
+
print(f"Error downloading '{model_filename}' from Hugging Face Hub: {e}")
|
12 |
+
raise
|
13 |
+
|
14 |
+
# تحميل الأدوات المساعدة
|
15 |
+
scaler = joblib.load(hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename="scaler.pkl"))
|
16 |
+
label_encoder = joblib.load(hf_hub_download(repo_id="Tahani1/Houses-Prices-Prediction", filename="label_encoder.pkl"))
|
17 |
+
|
18 |
+
# دالة التنبؤ بالسعر
|
19 |
+
def predict_price(num_rooms, distance, country, build_quality):
|
20 |
+
country_encoded = label_encoder.transform([country])[0]
|
21 |
+
features = np.array([[num_rooms, distance, country_encoded, build_quality]])
|
22 |
+
features_scaled = scaler.transform(features)
|
23 |
+
predicted_price = model.predict(features_scaled)[0]
|
24 |
+
return f"Predicted House Price: ${predicted_price:,.2f}"
|
25 |
+
|
26 |
+
# واجهة Gradio
|
27 |
+
inputs = [
|
28 |
+
gr.Number(label="Number of Rooms"),
|
29 |
+
gr.Number(label="Distance to Center (km)"),
|
30 |
+
gr.Dropdown(label="Country", choices=label_encoder.classes_.tolist()),
|
31 |
+
gr.Slider(minimum=1, maximum=10, label="Build Quality")
|
32 |
+
]
|
33 |
+
|
34 |
+
outputs = gr.Textbox(label="Prediction Result")
|
35 |
+
|
36 |
+
app = gr.Interface(fn=predict_price, inputs=inputs, outputs=outputs, title="House Price Prediction")
|
37 |
+
app.launch()
|