Upload 2 files
Browse files- app.py +78 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load pretrained XGBoost model từ HF Hub
|
| 9 |
+
model_path = hf_hub_download(
|
| 10 |
+
repo_id="sdaoudi/house-price-regression-xgb",
|
| 11 |
+
filename="xgb_model.pkl"
|
| 12 |
+
)
|
| 13 |
+
model = joblib.load(model_path)
|
| 14 |
+
|
| 15 |
+
# Tạo folder lưu lịch sử nếu chưa tồn tại
|
| 16 |
+
if not os.path.exists("history"):
|
| 17 |
+
os.makedirs("history")
|
| 18 |
+
|
| 19 |
+
def predict_price(
|
| 20 |
+
bedrooms, bathrooms, sqft_living, sqft_lot, floors,
|
| 21 |
+
waterfront, view, condition, grade, yr_built, zipcode, lat, long
|
| 22 |
+
):
|
| 23 |
+
data = {
|
| 24 |
+
"bedrooms": [bedrooms],
|
| 25 |
+
"bathrooms": [bathrooms],
|
| 26 |
+
"sqft_living": [sqft_living],
|
| 27 |
+
"sqft_lot": [sqft_lot],
|
| 28 |
+
"floors": [floors],
|
| 29 |
+
"waterfront": [int(waterfront)],
|
| 30 |
+
"view": [view],
|
| 31 |
+
"condition": [condition],
|
| 32 |
+
"grade": [grade],
|
| 33 |
+
"yr_built": [yr_built],
|
| 34 |
+
"zipcode": [zipcode],
|
| 35 |
+
"lat": [lat],
|
| 36 |
+
"long": [long]
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
df = pd.DataFrame(data)
|
| 40 |
+
pred = model.predict(df)[0]
|
| 41 |
+
result_text = f"💰 Giá nhà dự đoán: {pred:,.0f} USD"
|
| 42 |
+
|
| 43 |
+
# Lưu lịch sử dự đoán
|
| 44 |
+
df['predicted_price'] = pred
|
| 45 |
+
df['timestamp'] = datetime.now()
|
| 46 |
+
history_file = "history/predictions.csv"
|
| 47 |
+
if os.path.exists(history_file):
|
| 48 |
+
df.to_csv(history_file, mode='a', header=False, index=False)
|
| 49 |
+
else:
|
| 50 |
+
df.to_csv(history_file, index=False)
|
| 51 |
+
|
| 52 |
+
return result_text
|
| 53 |
+
|
| 54 |
+
# Theme Gradio đẹp + tooltip
|
| 55 |
+
interface = gr.Interface(
|
| 56 |
+
fn=predict_price,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Number(label="Bedrooms", info="Số phòng ngủ"),
|
| 59 |
+
gr.Number(label="Bathrooms", info="Số phòng tắm"),
|
| 60 |
+
gr.Number(label="Sqft Living", info="Diện tích sử dụng (sqft)"),
|
| 61 |
+
gr.Number(label="Sqft Lot", info="Diện tích mảnh đất (sqft)"),
|
| 62 |
+
gr.Number(label="Floors", info="Số tầng của nhà"),
|
| 63 |
+
gr.Checkbox(label="Waterfront", info="Nhà có view ra nước? (tick nếu có)"),
|
| 64 |
+
gr.Number(label="View", info="Chất lượng view (0-4)"),
|
| 65 |
+
gr.Number(label="Condition", info="Tình trạng nhà (1-5)"),
|
| 66 |
+
gr.Number(label="Grade", info="Chất lượng xây dựng (1-13)"),
|
| 67 |
+
gr.Number(label="Year Built", info="Năm xây dựng"),
|
| 68 |
+
gr.Number(label="Zipcode", info="Mã bưu chính"),
|
| 69 |
+
gr.Number(label="Latitude", info="Vĩ độ"),
|
| 70 |
+
gr.Number(label="Longitude", info="Kinh độ"),
|
| 71 |
+
],
|
| 72 |
+
outputs="text",
|
| 73 |
+
title="🏡 House Price Predictor (Enhanced)",
|
| 74 |
+
description="Dự đoán giá nhà bằng XGBoost pretrained từ HuggingFace.\nLưu lịch sử dự đoán để xuất CSV.",
|
| 75 |
+
theme="default" # Gradio mới hỗ trợ theme: default, soft, compact, etc.
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
joblib
|
| 4 |
+
xgboost
|
| 5 |
+
huggingface_hub
|