import requests import streamlit as st import joblib import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import load_model from datetime import date, timedelta import yfinance as yf from pathlib import Path import matplotlib.pyplot as plt import matplotlib.dates as mdates from matplotlib import rcParams # Set font untuk mendukung karakter Indonesia rcParams['font.family'] = 'DejaVu Sans' # --- Konfigurasi Halaman --- st.set_page_config( page_title="Prediksi Harga Ethereum (GRU)", page_icon="๐ช" ) # --- GLOBAL STYLES (UI only) --- st.markdown(""" """, unsafe_allow_html=True) # --- Fungsi-fungsi Bantuan --- @st.cache_data(ttl="1h") def load_eth_data(): ticker = "ETH-USD" df = None # 1. COBA ONLINE try: df = yf.download( ticker, start="2024-01-01", end=date.today() + timedelta(days=1), progress=False, auto_adjust=True, multi_level_index=False ) if df is not None and not df.empty: # Bersihkan Index & Kolom df = df.reset_index() new_cols = [] for col in df.columns: col_name = col[0] if isinstance(col, tuple) else str(col) new_cols.append(col_name) df.columns = new_cols # Kolom pertama adalah Date if 'Date' not in df.columns: df = df.rename(columns={df.columns[0]: 'Date'}) # Hapus Timezone df['Date'] = pd.to_datetime(df['Date']).dt.tz_localize(None) # Simpan Backup (Timpa file lama agar fresh) try: df.to_csv("eth_backup.csv", index=False) except: pass return df, "online" except Exception as e: print(f"Gagal Online: {e}") # 2. COBA BACKUP (JIKA ONLINE GAGAL) try: df_backup = pd.read_csv("eth_backup.csv") # Bersihkan kolom sampah jika ada if "Unnamed: 0" in df_backup.columns: df_backup = df_backup.drop(columns=["Unnamed: 0"]) # Pastikan kolom Date dikenali if "Date" in df_backup.columns: df_backup["Date"] = pd.to_datetime(df_backup["Date"]) elif df_backup.columns[0].lower() == "date": df_backup = df_backup.rename(columns={df_backup.columns[0]: "Date"}) df_backup["Date"] = pd.to_datetime(df_backup["Date"]) return df_backup, "backup" except FileNotFoundError: # 3. GAGAL TOTAL return None, "error" def validate_scaler(scaler): """Validasi scaler agar konsisten dengan training.""" issues = [] warnings = [] if not isinstance(scaler, MinMaxScaler): warnings.append(f"Scaler bukan MinMaxScaler (terdeteksi: {type(scaler).__name__}). Pastikan ini scaler training.") required_attrs = ["n_features_in_", "data_min_", "data_max_", "scale_", "min_"] for a in required_attrs: if not hasattr(scaler, a): issues.append(f"Scaler belum ter-fit atau tidak valid (atribut '{a}' tidak ada).") if hasattr(scaler, "n_features_in_"): if int(scaler.n_features_in_) != 1: issues.append(f"Scaler mengharapkan {scaler.n_features_in_} fitur, tapi aplikasi hanya pakai 1 fitur ('Close').") ok = (len(issues) == 0) return ok, issues, warnings def validate_model_input(model): """Pastikan input model bentuknya (None, time_step, 1).""" issues = [] try: shape = model.input_shape # biasanya (None, time_step, 1) if not (isinstance(shape, (list, tuple)) and len(shape) == 3): issues.append(f"input_shape tidak sesuai harapan: {shape} (harus 3 dimensi).") else: if shape[2] != 1: issues.append(f"Jumlah fitur input model = {shape[2]}, tapi aplikasi membentuk fitur=1.") if shape[1] is None or int(shape[1]) <= 1: issues.append(f"time_step tidak valid: {shape[1]}.") except Exception as e: issues.append(f"Gagal membaca input_shape model: {e}") ok = (len(issues) == 0) return ok, issues @st.cache_resource def load_gru_assets(): """Load model & scaler hasil training (wajib pakai scaler yang sama).""" model_path = Path("gru_model.h5") scaler_path = Path("scaler_gru.pkl") if not model_path.exists() or not scaler_path.exists(): st.warning( f"File tidak ditemukan.\n" f"Model: {model_path.resolve().name} ada? {model_path.exists()}\n" f"Scaler: {scaler_path.resolve().name} ada? {scaler_path.exists()}" ) return None, None try: model = load_model(model_path, compile=False) scaler = joblib.load(scaler_path) ok_scaler, scaler_issues, scaler_warnings = validate_scaler(scaler) for w in scaler_warnings: st.warning("โ ๏ธ " + w) if not ok_scaler: st.error("โ Scaler tidak kompatibel:\n- " + "\n- ".join(scaler_issues)) return None, None ok_model, model_issues = validate_model_input(model) if not ok_model: st.error("โ Model input tidak kompatibel:\n- " + "\n- ".join(model_issues)) return None, None return model, scaler except Exception as e: st.error(f"Gagal load aset: {e}") return None, None def predict_from_sequence_pure(model, initial_sequence_scaled, n_days): seq = np.array(initial_sequence_scaled, dtype="float32").reshape(-1) # (time_step,) preds = [] for _ in range(n_days): x = seq.reshape(1, -1, 1) # (1, time_step, 1) y = float(model.predict(x, verbose=0)[0, 0]) # output model (scaled) preds.append(y) seq = np.append(seq[1:], y) # autoregressive (murni) return np.array(preds, dtype="float32").reshape(-1, 1) def create_combined_chart(df, start_date, future_dates, future_predictions): """Membuat grafik gabungan dengan gaya yang sama seperti referensi.""" context_start_date = start_date - timedelta(days=609) recent_df = df[(df["Date"] >= context_start_date) & (df["Date"] <= start_date)].copy() window_size = 7 recent_df["Trend_Aktual"] = recent_df["Close"].rolling(window=window_size).mean() predictions_flat = future_predictions.flatten() fig, ax = plt.subplots(figsize=(14, 8)) ax.plot( recent_df["Date"], recent_df["Trend_Aktual"], color="#1f77b4", linewidth=2.5, label="Tren Harga Aktual", alpha=0.9 ) # Membuat tren prediksi menggunakan moving average 7 hari pred_trend = ( pd.Series(predictions_flat) .rolling(window=7, min_periods=1) .mean() ) ax.plot( future_dates, pred_trend, color="#d62728", linewidth=2.5, label="Tren Harga Prediksi (GRU)", alpha=0.9 ) ax.scatter( recent_df["Date"], recent_df["Close"], color="black", s=15, alpha=0.6, zorder=5, label="Data Harian Aktual" ) # garis penghubung trend terakhir ke prediksi pertama try: last_trend_date = recent_df["Date"].iloc[-1] last_trend_price = recent_df["Trend_Aktual"].dropna().iloc[-1] first_prediction_date = future_dates[0] first_prediction_price = predictions_flat[0] ax.plot( [last_trend_date, first_prediction_date], [last_trend_price, first_prediction_price], color="#d62728", linestyle="--", linewidth=2.0, alpha=0.7 ) except Exception: pass ax.set_xlabel("Waktu", fontsize=12, fontweight="bold") ax.set_ylabel("Harga (USD)", fontsize=12, fontweight="bold") ax.set_title("Analisis Tren Historis dan Prediksi Harga Ethereum", fontsize=16, fontweight="bold", pad=20) ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d, %Y")) plt.xticks(rotation=45) ax.grid(True, alpha=0.3, linestyle="--") handles, labels = ax.get_legend_handles_labels() order = [2, 0, 1, 3] if len(handles) == 4: ax.legend([handles[idx] for idx in order], [labels[idx] for idx in order], loc="upper left", fontsize=11) else: ax.legend(loc="upper left", fontsize=11) plt.tight_layout() ax.set_facecolor("#f8f9fa") fig.patch.set_facecolor("white") return fig # --- Tampilan Antarmuka Aplikasi --- st.markdown("