Download app.py from Wewoo/ABC: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/spaces/Wewoo/ABC/resolve/main/app.py
- Command line
-
hf download hf://spaces/Wewoo/ABC/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Wewoo/ABC/resolve/main/app.py
2.26 kB
| import gradio as gr | |
| import numpy as np | |
| from sklearn.datasets import fetch_california_housing | |
| from sklearn.ensemble import RandomForestRegressor | |
| # ====================== | |
| # 1. Load data & train model | |
| # ====================== | |
| data = fetch_california_housing() | |
| X = data.data | |
| y = data.target # đơn vị: 100,000 USD | |
| model = RandomForestRegressor(n_estimators=100, random_state=42) | |
| model.fit(X, y) | |
| # ====================== | |
| # 2. Hàm điều chỉnh theo năm | |
| # ====================== | |
| BASE_YEAR = 2000 | |
| YEARLY_RATE = 0.04 # 4% / năm | |
| def adjust_by_year(price, year): | |
| years_diff = year - BASE_YEAR | |
| adjusted_price = price * ((1 + YEARLY_RATE)**years_diff) | |
| return adjusted_price | |
| # ====================== | |
| # 3. Hàm dự đoán chính | |
| # ====================== | |
| def predict_price( | |
| medinc, houseage, averooms, avebedrms, | |
| population, aveoccup, latitude, longitude, | |
| year | |
| ): | |
| features = np.array([[ | |
| medinc, houseage, averooms, avebedrms, | |
| population, aveoccup, latitude, longitude | |
| ]]) | |
| base_pred = model.predict(features)[0] # ×100,000$ | |
| adjusted = adjust_by_year(base_pred, year) # theo năm | |
| usd = adjusted * 100000 | |
| return f"💰 Giá nhà dự đoán năm {year}: {usd:,.2f} USD" | |
| # ====================== | |
| # 4. Giao diện Gradio (chỉ dùng form) | |
| # ====================== | |
| interface = gr.Interface( | |
| fn=predict_price, | |
| inputs=[ | |
| gr.Slider(0, 20, value=3, label="MedInc (Thu nhập trung vị)"), | |
| gr.Slider(0, 100, value=20, label="HouseAge (Tuổi nhà)"), | |
| gr.Slider(1, 10, value=5, label="AveRooms (Số phòng TB)"), | |
| gr.Slider(0.5, 5, value=1, label="AveBedrms (Phòng ngủ TB)"), | |
| gr.Number(value=800, label="Population (Dân số)"), | |
| gr.Slider(1, 10, value=3, label="AveOccup (Số người/hộ TB)"), | |
| gr.Number(value=34.05, label="Latitude (Vĩ độ)"), | |
| gr.Number(value=-118.24, label="Longitude (Kinh độ)"), | |
| gr.Slider(1990, 2050, value=2024, step=1, label="Năm muốn dự đoán") | |
| ], | |
| outputs="text", | |
| title="🏠 AI Dự đoán giá nhà theo năm (California)", | |
| description="Mô hình AI + điều chỉnh theo năm (4%/năm). Không sử dụng text input." | |
| ) | |
| interface.launch() | |