Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,137 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import joblib
|
| 4 |
-
import numpy as np
|
| 5 |
-
import plotly.graph_objects as go
|
| 6 |
-
|
| 7 |
-
# --- SAYFA AYARLARI ---
|
| 8 |
-
st.set_page_config(page_title="Energy AI Predictor", layout="wide", page_icon="⚡")
|
| 9 |
-
|
| 10 |
-
# --- MODELLERİ YÜKLE ---
|
| 11 |
-
@st.cache_resource
|
| 12 |
-
def load_assets():
|
| 13 |
-
try:
|
| 14 |
-
#
|
| 15 |
-
m1 = joblib.load('electricity_model.pkl')
|
| 16 |
-
m2 = joblib.load('model.pkl')
|
| 17 |
-
sc = joblib.load('scaler.pkl')
|
| 18 |
-
return m1, m2, sc
|
| 19 |
-
except Exception as e:
|
| 20 |
-
st.error(f"Dosya Yükleme Hatası (Loading Error): {e}")
|
| 21 |
-
return None, None, None
|
| 22 |
-
|
| 23 |
-
model1, model2, scaler = load_assets()
|
| 24 |
-
|
| 25 |
-
if model1 is None or model2 is None or scaler is None:
|
| 26 |
-
st.warning("Model dosyaları (.pkl) bulunamadı! Lütfen dosyaların app.py ile aynı klasörde olduğundan emin olun.")
|
| 27 |
-
st.stop()
|
| 28 |
-
|
| 29 |
-
# --- SIDEBAR / YAN PANEL ---
|
| 30 |
-
with st.sidebar:
|
| 31 |
-
st.title("🌐 Language / Dil")
|
| 32 |
-
lang = st.radio("", ["Türkçe", "English"])
|
| 33 |
-
ln = "tr" if lang == "Türkçe" else "en"
|
| 34 |
-
|
| 35 |
-
st.header("📊 " + ("Parametreler" if ln=="tr" else "Parameters"))
|
| 36 |
-
hour = st.slider("Saat (Hour)", 0, 23, 12)
|
| 37 |
-
sys_load = st.number_input("System Load (EP2)", value=3500.0)
|
| 38 |
-
wind_prod = st.number_input("Wind Production", value=800.0)
|
| 39 |
-
|
| 40 |
-
st.subheader("⏳ " + ("Trendler" if ln=="tr" else "Trends"))
|
| 41 |
-
lag1 = st.number_input("Lag 1 (1h ago)", value=75.0)
|
| 42 |
-
lag24 = st.number_input("Lag 24 (24h ago)", value=70.0)
|
| 43 |
-
lag168 = st.number_input("Lag 168 (1 week ago)", value=65.0)
|
| 44 |
-
|
| 45 |
-
# --- TAHMİN FONKSİYONU (23 SÜTUNA TAMAMLAYAN KRİTİK KISIM) ---
|
| 46 |
-
def make_prediction(load, wind, hr, l1, l24, l168):
|
| 47 |
-
# Scaler 23 sütun beklediği için eğitimdeki sütun sırasına göre diziyoruz
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
np.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
st.
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
|
| 7 |
+
# --- SAYFA AYARLARI ---
|
| 8 |
+
st.set_page_config(page_title="Energy AI Predictor", layout="wide", page_icon="⚡")
|
| 9 |
+
|
| 10 |
+
# --- MODELLERİ YÜKLE ---
|
| 11 |
+
@st.cache_resource
|
| 12 |
+
def load_assets():
|
| 13 |
+
try:
|
| 14 |
+
# Terminalindeki isimlerle birebir aynı
|
| 15 |
+
m1 = joblib.load('electricity_model.pkl')
|
| 16 |
+
m2 = joblib.load('model.pkl')
|
| 17 |
+
sc = joblib.load('scaler.pkl')
|
| 18 |
+
return m1, m2, sc
|
| 19 |
+
except Exception as e:
|
| 20 |
+
st.error(f"Dosya Yükleme Hatası (Loading Error): {e}")
|
| 21 |
+
return None, None, None
|
| 22 |
+
|
| 23 |
+
model1, model2, scaler = load_assets()
|
| 24 |
+
|
| 25 |
+
if model1 is None or model2 is None or scaler is None:
|
| 26 |
+
st.warning("Model dosyaları (.pkl) bulunamadı! Lütfen dosyaların app.py ile aynı klasörde olduğundan emin olun.")
|
| 27 |
+
st.stop()
|
| 28 |
+
|
| 29 |
+
# --- SIDEBAR / YAN PANEL ---
|
| 30 |
+
with st.sidebar:
|
| 31 |
+
st.title("🌐 Language / Dil")
|
| 32 |
+
lang = st.radio("", ["Türkçe", "English"])
|
| 33 |
+
ln = "tr" if lang == "Türkçe" else "en"
|
| 34 |
+
|
| 35 |
+
st.header("📊 " + ("Parametreler" if ln=="tr" else "Parameters"))
|
| 36 |
+
hour = st.slider("Saat (Hour)", 0, 23, 12)
|
| 37 |
+
sys_load = st.number_input("System Load (EP2)", value=3500.0)
|
| 38 |
+
wind_prod = st.number_input("Wind Production", value=800.0)
|
| 39 |
+
|
| 40 |
+
st.subheader("⏳ " + ("Trendler" if ln=="tr" else "Trends"))
|
| 41 |
+
lag1 = st.number_input("Lag 1 (1h ago)", value=75.0)
|
| 42 |
+
lag24 = st.number_input("Lag 24 (24h ago)", value=70.0)
|
| 43 |
+
lag168 = st.number_input("Lag 168 (1 week ago)", value=65.0)
|
| 44 |
+
|
| 45 |
+
# --- TAHMİN FONKSİYONU (23 SÜTUNA TAMAMLAYAN KRİTİK KISIM) ---
|
| 46 |
+
def make_prediction(load, wind, hr, l1, l24, l168):
|
| 47 |
+
# Scaler 23 sütun beklediği için eğitimdeki sütun sırasına göre diziyoruz
|
| 48 |
+
# Eğitimde kullanılan sütun sırasını simüle eder:
|
| 49 |
+
features = [
|
| 50 |
+
0, 1, 20, 15, 4, 2024, 24, # Dummy: Holiday, DayOfWeek, Week, Day, Month, Year, Period
|
| 51 |
+
wind * 1.05, # ForecastWindProduction
|
| 52 |
+
load * 1.1, # SystemLoadEA
|
| 53 |
+
70, 18, 12, 250, # SMPEA, Temp, Windspeed, CO2
|
| 54 |
+
wind, # ActualWindProduction (Input)
|
| 55 |
+
load, # SystemLoadEP2 (Input)
|
| 56 |
+
hr, # hour (Input)
|
| 57 |
+
15, 4, # day, month
|
| 58 |
+
np.sin(2*np.pi*hr/23), # hour_sin
|
| 59 |
+
np.cos(2*np.pi*hr/23), # hour_cos
|
| 60 |
+
l1, l24, l168 # Lag1, Lag24, Lag168 (Input)
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
feat_array = np.array([features])
|
| 64 |
+
scaled_feat = scaler.transform(feat_array)
|
| 65 |
+
p1 = model1.predict(scaled_feat)[0]
|
| 66 |
+
p2 = model2.predict(scaled_feat)[0]
|
| 67 |
+
return (p1 * 0.6) + (p2 * 0.4)
|
| 68 |
+
|
| 69 |
+
# --- ANA PANEL ---
|
| 70 |
+
# İki dilli başlık bölümü
|
| 71 |
+
if ln == "tr":
|
| 72 |
+
st.title("⚡ Enerji Fiyat Analiz & Tahmin Platformu")
|
| 73 |
+
st.info("Yapay Zeka Hibrit Model: CatBoost (%60) + HistGB (%40) Aktif")
|
| 74 |
+
else:
|
| 75 |
+
st.title("⚡ Energy Price Analysis & Prediction Platform")
|
| 76 |
+
st.info("AI Hybrid Model: CatBoost (60%) + HistGB (40%) Active")
|
| 77 |
+
|
| 78 |
+
col1, col2 = st.columns([2, 1])
|
| 79 |
+
|
| 80 |
+
with col1:
|
| 81 |
+
try:
|
| 82 |
+
final_price = make_prediction(sys_load, wind_prod, hour, lag1, lag24, lag168)
|
| 83 |
+
|
| 84 |
+
st.subheader("🎯 " + ("Model Tahmin Sonucu" if ln=="tr" else "Model Prediction Result"))
|
| 85 |
+
st.metric(label="Estimated SMPEP2", value=f"{final_price:.2f} TL", delta=f"{final_price - lag1:.2f} TL")
|
| 86 |
+
|
| 87 |
+
# DURUM KARTLARI VE TAVSİYELER
|
| 88 |
+
st.markdown("---")
|
| 89 |
+
st.subheader("📋 " + ("Stratejik Öneriler" if ln=="tr" else "Strategic Recommendations"))
|
| 90 |
+
|
| 91 |
+
if final_price < 65:
|
| 92 |
+
st.success("🔵 " + ("Düşük Fiyat - Normal Piyasa" if ln=="tr" else "Low Price - Normal Market"))
|
| 93 |
+
st.write(
|
| 94 |
+
"Enerji tüketimi için çok uygun zaman! Yüksek maliyetli işlerinizi şimdi planlayın."
|
| 95 |
+
if ln=="tr" else
|
| 96 |
+
"Perfect time for energy consumption! Schedule high-cost tasks now."
|
| 97 |
+
)
|
| 98 |
+
elif final_price < 95:
|
| 99 |
+
st.warning("🟡 " + ("Orta Seviye - Dengeli Piyasa" if ln=="tr" else "Medium Level - Balanced Market"))
|
| 100 |
+
st.write(
|
| 101 |
+
"Fiyatlar stabil. Gereksiz tüketimden kaçınarak maliyetleri kontrol altında tutun."
|
| 102 |
+
if ln=="tr" else
|
| 103 |
+
"Prices are stable. Keep costs under control by avoiding unnecessary consumption."
|
| 104 |
+
)
|
| 105 |
+
else:
|
| 106 |
+
st.error("🔴 " + ("Yüksek Fiyat - Riskli Dönem" if ln=="tr" else "High Price - Risky Period"))
|
| 107 |
+
st.write(
|
| 108 |
+
"KRİTİK! Enerji tasarrufuna geçin. Üretim veya tüketimi düşük fiyatlı saatlere kaydırın."
|
| 109 |
+
if ln=="tr" else
|
| 110 |
+
"CRITICAL! Switch to energy saving. Shift production/consumption to lower-priced hours."
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
st.error(f"Tahmin Hatası: {e}")
|
| 115 |
+
|
| 116 |
+
with col2:
|
| 117 |
+
st.subheader("💡 " + ("Parametre Rehberi" if ln=="tr" else "Parameter Guide"))
|
| 118 |
+
if ln == "tr":
|
| 119 |
+
st.write("**📌 Sistem Yükü:** Talep arttıkça üretim maliyeti artar ve fiyat yükselir.")
|
| 120 |
+
st.write("**📌 Rüzgar Üretimi:** Yenilenebilir enerji fiyatı aşağı çeker.")
|
| 121 |
+
st.write("**📌 Lag 1/24/168:** Piyasanın geçmişteki fiyat hafızasıdır.")
|
| 122 |
+
else:
|
| 123 |
+
st.write("**📌 System Load:** Price increases as demand goes up.")
|
| 124 |
+
st.write("**📌 Wind Prod:** Renewable energy pulls the price down.")
|
| 125 |
+
st.write("**📌 Lag 1/24/168:** Market's historical price memory.")
|
| 126 |
+
|
| 127 |
+
# --- SİMÜLASYON GRAFİĞİ ---
|
| 128 |
+
st.markdown("---")
|
| 129 |
+
st.subheader("📈 " + ("Yük Değişiminin Fiyata Etkisi" if ln=="tr" else "Impact of Load Change on Price"))
|
| 130 |
+
|
| 131 |
+
load_range = np.linspace(sys_load * 0.7, sys_load * 1.3, 15)
|
| 132 |
+
sim_preds = [make_prediction(l, wind_prod, hour, lag1, lag24, lag168) for l in load_range]
|
| 133 |
+
|
| 134 |
+
fig = go.Figure()
|
| 135 |
+
fig.add_trace(go.Scatter(x=load_range, y=sim_preds, mode='lines+markers', line=dict(color='#00ff00', width=3)))
|
| 136 |
+
fig.update_layout(xaxis_title="Load (MW)", yaxis_title="Price (TL)", template="plotly_dark", height=400)
|
| 137 |
st.plotly_chart(fig, use_container_width=True)
|