-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (71 loc) · 2.56 KB
/
Copy pathapp.py
File metadata and controls
94 lines (71 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
93
94
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import sys
import os
# ---------------------------------------------------
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Add src folder to PYTHONPATH
SRC_DIR = os.path.join(BASE_DIR, "src")
if SRC_DIR not in sys.path:
sys.path.append(SRC_DIR)
# Model path (absolute, safe, cross-platform)
model_path = os.path.join(BASE_DIR, "models", "energy_optimizer_dqn.keras")
# ---------------------------------------------------
from src.environment import Environment
from tensorflow.keras.models import load_model
st.set_page_config(page_title="Energy Optimizer AI", layout="wide")
def main():
st.title("⚡ AI-Powered Energy Consumption Optimizer")
st.markdown("""
This UI simulates how an AI agent optimizes cooling energy usage compared
to a traditional (non-AI) baseline.
""")
# Check if model exists
if not os.path.exists(model_path):
st.error("🚨 Model not found! Please train the model first.")
return
# Load model
model = load_model(model_path)
# Initialize environment
env = Environment()
# Simulation config
timesteps = st.slider("Simulation Steps", 20, 200, 50)
if st.button("Run Simulation"):
state = env.reset()
temps_ai = []
temps_noai = []
energy_ai = []
energy_noai = []
for _ in range(timesteps):
# Predict action
q_values = model.predict(state, verbose=0)[0]
action = int(np.argmax(q_values))
# Step environment
next_state, reward, done, info = env.step(action)
state = next_state
# Log values
temps_ai.append(info["temp_ai"])
temps_noai.append(info["temp_noai"])
energy_ai.append(info["energy_ai"])
energy_noai.append(info["energy_noai"])
if done:
break
# Display Results
col1, col2 = st.columns(2)
with col1:
st.subheader("Temperature Comparison")
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(temps_ai, label="AI Temp")
ax.plot(temps_noai, label="Baseline Temp")
ax.legend()
st.pyplot(fig)
with col2:
st.subheader("Energy Usage Comparison")
fig2, ax2 = plt.subplots(figsize=(6, 4))
ax2.plot(energy_ai, label="AI Energy")
ax2.plot(energy_noai, label="Baseline Energy")
ax2.legend()
st.pyplot(fig2)
if __name__ == "__main__":
main()