Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import joblib | |
| import numpy as np | |
| import plotly.graph_objects as go | |
| # --- SAYFA AYARLARI --- | |
| st.set_page_config(page_title="Energy AI Predictor", layout="wide", page_icon="⚡") | |
| # --- MODELLERİ YÜKLE --- | |
| def load_assets(): | |
| try: | |
| # Terminalindeki isimlerle birebir aynı | |
| m1 = joblib.load('electricity_model.pkl') | |
| m2 = joblib.load('model.pkl') | |
| sc = joblib.load('scaler.pkl') | |
| return m1, m2, sc | |
| except Exception as e: | |
| st.error(f"Dosya Yükleme Hatası (Loading Error): {e}") | |
| return None, None, None | |
| model1, model2, scaler = load_assets() | |
| if model1 is None or model2 is None or scaler is None: | |
| st.warning("Model dosyaları (.pkl) bulunamadı! Lütfen dosyaların app.py ile aynı klasörde olduğundan emin olun.") | |
| st.stop() | |
| # --- SIDEBAR / YAN PANEL --- | |
| with st.sidebar: | |
| st.title("🌐 Language / Dil") | |
| lang = st.radio("", ["Türkçe", "English"]) | |
| ln = "tr" if lang == "Türkçe" else "en" | |
| st.header("📊 " + ("Parametreler" if ln=="tr" else "Parameters")) | |
| hour = st.slider("Saat (Hour)", 0, 23, 12) | |
| sys_load = st.number_input("System Load (EP2)", value=3500.0) | |
| wind_prod = st.number_input("Wind Production", value=800.0) | |
| st.subheader("⏳ " + ("Trendler" if ln=="tr" else "Trends")) | |
| lag1 = st.number_input("Lag 1 (1h ago)", value=75.0) | |
| lag24 = st.number_input("Lag 24 (24h ago)", value=70.0) | |
| lag168 = st.number_input("Lag 168 (1 week ago)", value=65.0) | |
| # --- TAHMİN FONKSİYONU (23 SÜTUNA TAMAMLAYAN KRİTİK KISIM) --- | |
| def make_prediction(load, wind, hr, l1, l24, l168): | |
| # Scaler 23 sütun beklediği için eğitimdeki sütun sırasına göre diziyoruz | |
| # Eğitimde kullanılan sütun sırasını simüle eder: | |
| features = [ | |
| 0, 1, 20, 15, 4, 2024, 24, # Dummy: Holiday, DayOfWeek, Week, Day, Month, Year, Period | |
| wind * 1.05, # ForecastWindProduction | |
| load * 1.1, # SystemLoadEA | |
| 70, 18, 12, 250, # SMPEA, Temp, Windspeed, CO2 | |
| wind, # ActualWindProduction (Input) | |
| load, # SystemLoadEP2 (Input) | |
| hr, # hour (Input) | |
| 15, 4, # day, month | |
| np.sin(2*np.pi*hr/23), # hour_sin | |
| np.cos(2*np.pi*hr/23), # hour_cos | |
| l1, l24, l168 # Lag1, Lag24, Lag168 (Input) | |
| ] | |
| feat_array = np.array([features]) | |
| scaled_feat = scaler.transform(feat_array) | |
| p1 = model1.predict(scaled_feat)[0] | |
| p2 = model2.predict(scaled_feat)[0] | |
| return (p1 * 0.6) + (p2 * 0.4) | |
| # --- ANA PANEL --- | |
| # İki dilli başlık bölümü | |
| if ln == "tr": | |
| st.title("⚡ Enerji Fiyat Analiz & Tahmin Platformu") | |
| st.info("Yapay Zeka Hibrit Model: CatBoost (%60) + HistGB (%40) Aktif") | |
| else: | |
| st.title("⚡ Energy Price Analysis & Prediction Platform") | |
| st.info("AI Hybrid Model: CatBoost (60%) + HistGB (40%) Active") | |
| col1, col2 = st.columns([2, 1]) | |
| with col1: | |
| try: | |
| final_price = make_prediction(sys_load, wind_prod, hour, lag1, lag24, lag168) | |
| st.subheader("🎯 " + ("Model Tahmin Sonucu" if ln=="tr" else "Model Prediction Result")) | |
| st.metric(label="Estimated SMPEP2", value=f"{final_price:.2f} TL", delta=f"{final_price - lag1:.2f} TL") | |
| # DURUM KARTLARI VE TAVSİYELER | |
| st.markdown("---") | |
| st.subheader("📋 " + ("Stratejik Öneriler" if ln=="tr" else "Strategic Recommendations")) | |
| if final_price < 65: | |
| st.success("🔵 " + ("Düşük Fiyat - Normal Piyasa" if ln=="tr" else "Low Price - Normal Market")) | |
| st.write( | |
| "Enerji tüketimi için çok uygun zaman! Yüksek maliyetli işlerinizi şimdi planlayın." | |
| if ln=="tr" else | |
| "Perfect time for energy consumption! Schedule high-cost tasks now." | |
| ) | |
| elif final_price < 95: | |
| st.warning("🟡 " + ("Orta Seviye - Dengeli Piyasa" if ln=="tr" else "Medium Level - Balanced Market")) | |
| st.write( | |
| "Fiyatlar stabil. Gereksiz tüketimden kaçınarak maliyetleri kontrol altında tutun." | |
| if ln=="tr" else | |
| "Prices are stable. Keep costs under control by avoiding unnecessary consumption." | |
| ) | |
| else: | |
| st.error("🔴 " + ("Yüksek Fiyat - Riskli Dönem" if ln=="tr" else "High Price - Risky Period")) | |
| st.write( | |
| "KRİTİK! Enerji tasarrufuna geçin. Üretim veya tüketimi düşük fiyatlı saatlere kaydırın." | |
| if ln=="tr" else | |
| "CRITICAL! Switch to energy saving. Shift production/consumption to lower-priced hours." | |
| ) | |
| except Exception as e: | |
| st.error(f"Tahmin Hatası: {e}") | |
| with col2: | |
| st.subheader("💡 " + ("Parametre Rehberi" if ln=="tr" else "Parameter Guide")) | |
| if ln == "tr": | |
| st.write("**📌 Sistem Yükü:** Talep arttıkça üretim maliyeti artar ve fiyat yükselir.") | |
| st.write("**📌 Rüzgar Üretimi:** Yenilenebilir enerji fiyatı aşağı çeker.") | |
| st.write("**📌 Lag 1/24/168:** Piyasanın geçmişteki fiyat hafızasıdır.") | |
| else: | |
| st.write("**📌 System Load:** Price increases as demand goes up.") | |
| st.write("**📌 Wind Prod:** Renewable energy pulls the price down.") | |
| st.write("**📌 Lag 1/24/168:** Market's historical price memory.") | |
| # --- SİMÜLASYON GRAFİĞİ --- | |
| st.markdown("---") | |
| st.subheader("📈 " + ("Yük Değişiminin Fiyata Etkisi" if ln=="tr" else "Impact of Load Change on Price")) | |
| load_range = np.linspace(sys_load * 0.7, sys_load * 1.3, 15) | |
| sim_preds = [make_prediction(l, wind_prod, hour, lag1, lag24, lag168) for l in load_range] | |
| fig = go.Figure() | |
| fig.add_trace(go.Scatter(x=load_range, y=sim_preds, mode='lines+markers', line=dict(color='#00ff00', width=3))) | |
| fig.update_layout(xaxis_title="Load (MW)", yaxis_title="Price (TL)", template="plotly_dark", height=400) | |
| st.plotly_chart(fig, use_container_width=True) |