Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,17 +179,6 @@ def load_hourly_data(file_path="data/final_hourly_feature_dataset.csv"):
|
|
| 179 |
st.error(f"An unexpected error occurred while loading hourly data: {e}")
|
| 180 |
return pd.DataFrame()
|
| 181 |
|
| 182 |
-
@st.cache_resource
|
| 183 |
-
def load_hourly_aggregate_model():
|
| 184 |
-
"""Tải mô hình Daily Champion (Hourly Aggregate) cho metric T+24h."""
|
| 185 |
-
file_path = "data/champion_Hourly_Aggregate_Champion_day_1.pkl"
|
| 186 |
-
try:
|
| 187 |
-
model = joblib.load(file_path)
|
| 188 |
-
return model
|
| 189 |
-
except FileNotFoundError as e:
|
| 190 |
-
st.warning(f"Warning: Daily Aggregate Model not found at: {e.filename}. Using dummy data for T+24h metric.")
|
| 191 |
-
return None
|
| 192 |
-
|
| 193 |
@st.cache_resource
|
| 194 |
def load_24_hourly_models():
|
| 195 |
"""Tải 24 mô hình LGBM chuyên biệt (T+1h đến T+24h) cho biểu đồ."""
|
|
@@ -313,8 +302,6 @@ HOURLY_TARGET_COLS = ['target_temp_next_24h', 'target_temp_next_48h', 'target_te
|
|
| 313 |
# Load models và data mới
|
| 314 |
hourly_data_df = load_hourly_data(file_path="data/final_hourly_feature_dataset.csv") # Dùng tên file features chính xác
|
| 315 |
hourly_models_24h = load_24_hourly_models() # Dùng 24 mô hình LGBM
|
| 316 |
-
hourly_agg_model = load_hourly_aggregate_model() # Dùng mô hình Aggregate T+24h
|
| 317 |
-
|
| 318 |
# Tạo input features cho Hourly
|
| 319 |
if not hourly_data_df.empty:
|
| 320 |
HOURLY_FEATURE_COLS = [col for col in hourly_data_df.columns if col not in HOURLY_TARGET_COLS]
|
|
@@ -793,38 +780,22 @@ with tab4:
|
|
| 793 |
# 1. Chạy Dự đoán Hourly (cho biểu đồ T+1h đến T+24h)
|
| 794 |
predictions_24h = predict_next_24_hours(input_features_hourly, hourly_models_24h)
|
| 795 |
|
| 796 |
-
# ---
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
selected_date_ts = pd.Timestamp(selected_date)
|
| 801 |
-
|
| 802 |
-
# Lấy input features cho DAILY AGGREGATE MODEL từ X_test (Daily Features DataFrame)
|
| 803 |
-
input_features_daily_agg = pd.DataFrame()
|
| 804 |
-
if selected_date_ts in X_test.index:
|
| 805 |
-
# Lấy single row từ Daily Features (đã được aggregated)
|
| 806 |
-
input_features_daily_agg = X_test.loc[[selected_date_ts]]
|
| 807 |
-
|
| 808 |
-
if hourly_agg_model and not input_features_daily_agg.empty:
|
| 809 |
-
# CHẠY MODEL TRÊN INPUT DAILY FEATURES (1036 features)
|
| 810 |
-
try:
|
| 811 |
-
t_plus_24h_metric = hourly_agg_model.predict(input_features_daily_agg)[0]
|
| 812 |
-
except Exception as e:
|
| 813 |
-
# Bắt lỗi dự đoán và trả về cảnh báo nếu vẫn còn lỗi feature mismatch khác
|
| 814 |
-
st.error(f"Error predicting T+24h metric (Daily Agg. Model). Mismatch likely: {e}")
|
| 815 |
-
t_plus_24h_metric = None # Đảm bảo giá trị là None nếu lỗi
|
| 816 |
-
|
| 817 |
# 2. Hiển thị Dự đoán T+24h (Tức là giờ đó ngày mai)
|
| 818 |
st.subheader(f"Summary Forecast for Next Day (Starting {latest_time_for_day.strftime('%H:%M')})")
|
| 819 |
-
|
| 820 |
forecast_start_ts = latest_time_for_day + pd.Timedelta(hours=1)
|
| 821 |
-
|
| 822 |
col_daily_pred = st.columns(4)
|
| 823 |
with col_daily_pred[0]:
|
| 824 |
-
#
|
| 825 |
-
display_value = t_plus_24h_metric if t_plus_24h_metric is not None else predictions_24h[0]
|
| 826 |
st.metric(
|
| 827 |
-
label=f"Forecast @ {forecast_start_ts.strftime('%H:%M')} Tomorrow (
|
|
|
|
|
|
|
| 828 |
value=f"{display_value:.1f}°C"
|
| 829 |
)
|
| 830 |
|
|
|
|
| 179 |
st.error(f"An unexpected error occurred while loading hourly data: {e}")
|
| 180 |
return pd.DataFrame()
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
@st.cache_resource
|
| 183 |
def load_24_hourly_models():
|
| 184 |
"""Tải 24 mô hình LGBM chuyên biệt (T+1h đến T+24h) cho biểu đồ."""
|
|
|
|
| 302 |
# Load models và data mới
|
| 303 |
hourly_data_df = load_hourly_data(file_path="data/final_hourly_feature_dataset.csv") # Dùng tên file features chính xác
|
| 304 |
hourly_models_24h = load_24_hourly_models() # Dùng 24 mô hình LGBM
|
|
|
|
|
|
|
| 305 |
# Tạo input features cho Hourly
|
| 306 |
if not hourly_data_df.empty:
|
| 307 |
HOURLY_FEATURE_COLS = [col for col in hourly_data_df.columns if col not in HOURLY_TARGET_COLS]
|
|
|
|
| 780 |
# 1. Chạy Dự đoán Hourly (cho biểu đồ T+1h đến T+24h)
|
| 781 |
predictions_24h = predict_next_24_hours(input_features_hourly, hourly_models_24h)
|
| 782 |
|
| 783 |
+
# --- FIX CUỐI CÙNG: Chỉ dùng T+24h của Hourly Model ---
|
| 784 |
+
# T+24h là index 23 (nếu có đủ 24 giá trị)
|
| 785 |
+
t_plus_24h_metric_value = predictions_24h[23] if len(predictions_24h) >= 24 else (predictions_24h[-1] if predictions_24h else float('nan'))
|
| 786 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 787 |
# 2. Hiển thị Dự đoán T+24h (Tức là giờ đó ngày mai)
|
| 788 |
st.subheader(f"Summary Forecast for Next Day (Starting {latest_time_for_day.strftime('%H:%M')})")
|
| 789 |
+
|
| 790 |
forecast_start_ts = latest_time_for_day + pd.Timedelta(hours=1)
|
| 791 |
+
|
| 792 |
col_daily_pred = st.columns(4)
|
| 793 |
with col_daily_pred[0]:
|
| 794 |
+
# Chỉ hiển thị T+24h từ Hourly Model (Không dùng model lỗi nữa)
|
|
|
|
| 795 |
st.metric(
|
| 796 |
+
label=f"Forecast @ {forecast_start_ts.strftime('%H:%M')} Tomorrow (T+24h)",
|
| 797 |
+
value=f"{t_plus_24h_metric_value:.1f}°C"
|
| 798 |
+
)start_ts.strftime('%H:%M')} Tomorrow (Day 1 Agg.)",
|
| 799 |
value=f"{display_value:.1f}°C"
|
| 800 |
)
|
| 801 |
|