Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,258 +7,563 @@ import matplotlib.patches as mpatches
|
|
| 7 |
import numpy as np
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
"bg":
|
| 15 |
-
"panel":
|
| 16 |
-
"cyan":
|
| 17 |
-
"green":
|
| 18 |
-
"amber":
|
| 19 |
-
"red":
|
| 20 |
-
"
|
| 21 |
-
"
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
def style_ax(ax, fig):
|
| 25 |
-
fig.patch.set_facecolor(
|
| 26 |
-
ax.set_facecolor(
|
| 27 |
-
ax.tick_params(colors=
|
| 28 |
-
ax.xaxis.label.set_color(
|
| 29 |
-
ax.yaxis.label.set_color(
|
| 30 |
-
for
|
| 31 |
-
|
| 32 |
-
ax.grid(True, color="
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
steps = 90
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
load =
|
| 42 |
load = np.clip(load, 0, 1000)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
style_ax(ax, fig)
|
| 48 |
-
ax.
|
| 49 |
-
ax.plot(t,
|
| 50 |
-
ax.
|
| 51 |
-
|
| 52 |
-
ax.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return fig
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# CHART 2 โ Cost Efficiency (Line + target budget line)
|
| 62 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 63 |
-
def make_cost_chart(budget_on, spot_avail):
|
| 64 |
steps = 90
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
if budget_on:
|
| 69 |
-
burn = np.clip(burn, None,
|
| 70 |
if spot_avail > 50:
|
| 71 |
-
burn *= 0.
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
fig, ax = plt.subplots(figsize=(5,
|
| 74 |
style_ax(ax, fig)
|
| 75 |
-
ax.
|
| 76 |
-
ax.
|
| 77 |
-
ax.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
return fig
|
| 86 |
|
| 87 |
-
|
| 88 |
-
# CHART 3 โ Response Performance (Line)
|
| 89 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 90 |
-
def make_perf_chart(chaos_monkey):
|
| 91 |
steps = 90
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
if chaos_monkey:
|
| 95 |
-
spikes = np.random.choice(steps, size=
|
| 96 |
-
latency[spikes] += np.random.uniform(
|
| 97 |
-
latency = np.clip(latency, 0,
|
|
|
|
| 98 |
|
| 99 |
-
fig, ax = plt.subplots(figsize=(5,
|
| 100 |
style_ax(ax, fig)
|
| 101 |
-
ax.
|
| 102 |
-
ax.
|
| 103 |
-
ax.
|
| 104 |
-
ax.
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return fig
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def make_reward_chart(auto_scaling):
|
| 113 |
-
episodes = np.arange(1, 201)
|
| 114 |
if auto_scaling:
|
| 115 |
-
|
| 116 |
else:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
|
| 120 |
-
fig, ax = plt.subplots(figsize=(5,
|
| 121 |
style_ax(ax, fig)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
ax.
|
| 125 |
-
ax.
|
| 126 |
-
ax.
|
| 127 |
-
ax.
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
return fig
|
| 130 |
|
| 131 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 132 |
-
# CHART 5 โ Cost Comparison Bar (Before vs After RL)
|
| 133 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 134 |
def make_comparison_chart(auto_scaling):
|
| 135 |
-
labels
|
| 136 |
-
spend
|
| 137 |
-
downtime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
for ax in (ax1, ax2):
|
| 141 |
style_ax(ax, fig)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
ax1.
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
return fig
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
#
|
| 162 |
-
#
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
return (
|
| 189 |
-
f"๐
|
| 190 |
-
f"๐ฅ๏ธ
|
| 191 |
-
f"๐ธ
|
| 192 |
-
f"โก
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
log,
|
| 194 |
spend,
|
| 195 |
-
|
| 196 |
-
savings,
|
| 197 |
score,
|
| 198 |
-
|
| 199 |
-
cost_fig,
|
| 200 |
-
perf_fig,
|
| 201 |
-
reward_fig,
|
| 202 |
-
comparison_fig,
|
| 203 |
)
|
| 204 |
|
| 205 |
-
#
|
| 206 |
-
#
|
| 207 |
-
#
|
|
|
|
| 208 |
CSS = """
|
|
|
|
| 209 |
body, .gradio-container {
|
| 210 |
background: #0a0e1a !important;
|
| 211 |
color: #e2e8f0 !important;
|
| 212 |
-
font-family: '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
}
|
|
|
|
|
|
|
| 214 |
.gr-button-primary {
|
| 215 |
background: transparent !important;
|
| 216 |
-
border:
|
| 217 |
color: #00e5ff !important;
|
| 218 |
-
|
| 219 |
-
font-weight:
|
|
|
|
|
|
|
|
|
|
| 220 |
}
|
| 221 |
.gr-button-primary:hover {
|
| 222 |
-
background: rgba(0,229,255,0.
|
| 223 |
}
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
| 228 |
}
|
| 229 |
-
|
|
|
|
| 230 |
input, select, textarea {
|
| 231 |
background: #0d1525 !important;
|
| 232 |
color: #e2e8f0 !important;
|
| 233 |
border: 0.5px solid #1e2d45 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
}
|
| 235 |
-
.gr-plot { background: #111827 !important; border: 0.5px solid #1e2d45 !important; border-radius: 8px !important; }
|
| 236 |
"""
|
| 237 |
|
| 238 |
-
#
|
| 239 |
-
# BUILD
|
| 240 |
-
#
|
|
|
|
| 241 |
with gr.Blocks(css=CSS, title="โ๏ธ Cloud-Cost Guardian AI") as demo:
|
| 242 |
|
|
|
|
| 243 |
gr.Markdown("""
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
# โโ Row 1:
|
| 250 |
-
with gr.Row():
|
| 251 |
-
load_out = gr.Textbox(label="Incoming Load",
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
gr.Markdown("---")
|
| 257 |
|
| 258 |
-
# โโ Row 2: Controls +
|
| 259 |
with gr.Row():
|
| 260 |
-
|
|
|
|
|
|
|
| 261 |
gr.Markdown("### ๐ฎ Simulation Controls")
|
|
|
|
| 262 |
scenario = gr.Dropdown(
|
| 263 |
choices=[
|
| 264 |
"Scenario 1: Steady Load (Easy)",
|
|
@@ -268,49 +573,98 @@ with gr.Blocks(css=CSS, title="โ๏ธ Cloud-Cost Guardian AI") as demo:
|
|
| 268 |
value="Scenario 2: Daily Peak Surges (Medium)",
|
| 269 |
label="Scenario"
|
| 270 |
)
|
| 271 |
-
user_load = gr.Slider(0, 100, value=75, step=1,
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
gr.
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
gr.Markdown("---")
|
| 288 |
gr.Markdown("### ๐ Live Performance Graphs")
|
| 289 |
|
| 290 |
-
# โโ Row
|
| 291 |
with gr.Row():
|
| 292 |
-
traffic_plot
|
| 293 |
-
cost_plot
|
| 294 |
-
perf_plot
|
| 295 |
|
| 296 |
-
# โโ Row
|
| 297 |
with gr.Row():
|
| 298 |
-
reward_plot
|
| 299 |
-
|
|
|
|
| 300 |
|
| 301 |
gr.Markdown("""
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
| 305 |
|
| 306 |
-
# โโ Wire
|
| 307 |
run_btn.click(
|
| 308 |
fn=run_simulation,
|
| 309 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 310 |
outputs=[
|
| 311 |
-
load_out, server_out, burn_out,
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
]
|
| 315 |
)
|
| 316 |
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 11 |
+
# COLORS
|
| 12 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 13 |
+
C = {
|
| 14 |
+
"bg": "#0a0e1a",
|
| 15 |
+
"panel": "#111827",
|
| 16 |
+
"cyan": "#00e5ff",
|
| 17 |
+
"green": "#00ff9d",
|
| 18 |
+
"amber": "#ffb300",
|
| 19 |
+
"red": "#ff4d6d",
|
| 20 |
+
"purple":"#a78bfa",
|
| 21 |
+
"muted": "#64748b",
|
| 22 |
+
"text": "#e2e8f0",
|
| 23 |
+
"border":"#1e2d45",
|
| 24 |
}
|
| 25 |
|
| 26 |
def style_ax(ax, fig):
|
| 27 |
+
fig.patch.set_facecolor(C["panel"])
|
| 28 |
+
ax.set_facecolor(C["panel"])
|
| 29 |
+
ax.tick_params(colors=C["muted"], labelsize=9)
|
| 30 |
+
ax.xaxis.label.set_color(C["muted"])
|
| 31 |
+
ax.yaxis.label.set_color(C["muted"])
|
| 32 |
+
for sp in ax.spines.values():
|
| 33 |
+
sp.set_edgecolor(C["border"])
|
| 34 |
+
ax.grid(True, color=C["border"], linewidth=0.6,
|
| 35 |
+
linestyle="--", alpha=0.7)
|
| 36 |
+
ax.set_title(ax.get_title(),
|
| 37 |
+
color=C["text"], fontsize=11,
|
| 38 |
+
fontweight="bold", pad=10)
|
| 39 |
+
|
| 40 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 41 |
+
# CHART BUILDERS
|
| 42 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 43 |
+
|
| 44 |
+
def make_traffic_chart(user_load, time_step):
|
| 45 |
steps = 90
|
| 46 |
+
np.random.seed(time_step % 50)
|
| 47 |
+
t = np.arange(steps)
|
| 48 |
+
load = user_load * 8 + 200 * np.sin(t / 12) + np.random.normal(0, 30, steps)
|
| 49 |
load = np.clip(load, 0, 1000)
|
| 50 |
+
srvs = load / 70 + np.random.normal(0, 0.4, steps)
|
| 51 |
+
srvs = np.clip(srvs, 1, 20)
|
| 52 |
|
| 53 |
+
# mark spikes
|
| 54 |
+
spike_idx = np.where(load > np.percentile(load, 88))[0]
|
| 55 |
+
|
| 56 |
+
fig, ax = plt.subplots(figsize=(5.5, 3.0))
|
| 57 |
style_ax(ax, fig)
|
| 58 |
+
ax.set_title("Traffic & Server Scaling")
|
| 59 |
+
ax.plot(t, load, color=C["green"], lw=1.8, label="Incoming load")
|
| 60 |
+
ax.plot(t, srvs * 45, color=C["cyan"], lw=1.8,
|
| 61 |
+
linestyle="--", label="Active servers (ร45)")
|
| 62 |
+
ax.fill_between(t, load, alpha=0.08, color=C["green"])
|
| 63 |
+
# highlight spikes
|
| 64 |
+
ax.scatter(spike_idx, load[spike_idx],
|
| 65 |
+
color=C["red"], s=22, zorder=5, label="Load spike")
|
| 66 |
+
ax.set_xlabel("Time (steps)", fontsize=9)
|
| 67 |
+
ax.set_ylabel("Load / req/s", fontsize=9)
|
| 68 |
+
legend = ax.legend(fontsize=8, facecolor=C["panel"],
|
| 69 |
+
labelcolor=C["text"],
|
| 70 |
+
edgecolor=C["border"], loc="upper right")
|
| 71 |
+
plt.tight_layout(pad=1.0)
|
| 72 |
return fig
|
| 73 |
|
| 74 |
+
def make_cost_chart(budget_on, spot_avail, time_step):
|
|
|
|
|
|
|
|
|
|
| 75 |
steps = 90
|
| 76 |
+
np.random.seed(time_step % 50 + 1)
|
| 77 |
+
t = np.arange(steps)
|
| 78 |
+
budget = 0.50
|
| 79 |
+
burn = 0.85 * np.exp(-t / 38) + 0.06 + np.random.normal(0, 0.02, steps)
|
| 80 |
if budget_on:
|
| 81 |
+
burn = np.clip(burn, None, budget + 0.06)
|
| 82 |
if spot_avail > 50:
|
| 83 |
+
burn *= 0.72
|
| 84 |
+
|
| 85 |
+
over_budget = burn > budget
|
| 86 |
|
| 87 |
+
fig, ax = plt.subplots(figsize=(5.5, 3.0))
|
| 88 |
style_ax(ax, fig)
|
| 89 |
+
ax.set_title("Cost Efficiency")
|
| 90 |
+
ax.plot(t, burn, color=C["red"], lw=1.8, label="Hourly burn ($)")
|
| 91 |
+
ax.axhline(budget, color=C["amber"], lw=1.2,
|
| 92 |
+
linestyle="--", label=f"Budget ${budget:.2f}/hr")
|
| 93 |
+
# shade over-budget zone
|
| 94 |
+
ax.fill_between(t, burn, budget,
|
| 95 |
+
where=over_budget,
|
| 96 |
+
color=C["red"], alpha=0.18,
|
| 97 |
+
label="Over budget")
|
| 98 |
+
ax.set_xlabel("Time (steps)", fontsize=9)
|
| 99 |
+
ax.set_ylabel("$/hr", fontsize=9)
|
| 100 |
+
ax.legend(fontsize=8, facecolor=C["panel"],
|
| 101 |
+
labelcolor=C["text"], edgecolor=C["border"])
|
| 102 |
+
plt.tight_layout(pad=1.0)
|
| 103 |
return fig
|
| 104 |
|
| 105 |
+
def make_perf_chart(chaos_monkey, time_step):
|
|
|
|
|
|
|
|
|
|
| 106 |
steps = 90
|
| 107 |
+
np.random.seed(time_step % 50 + 2)
|
| 108 |
+
t = np.arange(steps)
|
| 109 |
+
latency = 40 + 18 * np.sin(t / 14) + np.random.normal(0, 5, steps)
|
| 110 |
+
spikes = []
|
| 111 |
if chaos_monkey:
|
| 112 |
+
spikes = np.random.choice(steps, size=7, replace=False)
|
| 113 |
+
latency[spikes] += np.random.uniform(70, 160, size=7)
|
| 114 |
+
latency = np.clip(latency, 0, 220)
|
| 115 |
+
sla = 100 # ms SLA line
|
| 116 |
|
| 117 |
+
fig, ax = plt.subplots(figsize=(5.5, 3.0))
|
| 118 |
style_ax(ax, fig)
|
| 119 |
+
ax.set_title("Response Performance (99th pct)")
|
| 120 |
+
ax.plot(t, latency, color=C["amber"], lw=1.8)
|
| 121 |
+
ax.fill_between(t, latency, alpha=0.12, color=C["amber"])
|
| 122 |
+
ax.axhline(sla, color=C["red"], lw=1.0,
|
| 123 |
+
linestyle=":", label=f"SLA {sla} ms")
|
| 124 |
+
if len(spikes):
|
| 125 |
+
ax.scatter(spikes, latency[spikes],
|
| 126 |
+
color=C["red"], s=30, zorder=5,
|
| 127 |
+
marker="x", linewidths=2,
|
| 128 |
+
label="Chaos spike")
|
| 129 |
+
ax.fill_between(t, latency, sla,
|
| 130 |
+
where=(latency > sla),
|
| 131 |
+
color=C["red"], alpha=0.20,
|
| 132 |
+
label="SLA breach")
|
| 133 |
+
ax.set_xlabel("Time (steps)", fontsize=9)
|
| 134 |
+
ax.set_ylabel("Latency (ms)", fontsize=9)
|
| 135 |
+
ax.legend(fontsize=8, facecolor=C["panel"],
|
| 136 |
+
labelcolor=C["text"], edgecolor=C["border"])
|
| 137 |
+
plt.tight_layout(pad=1.0)
|
| 138 |
return fig
|
| 139 |
|
| 140 |
+
def make_reward_chart(auto_scaling, time_step):
|
| 141 |
+
np.random.seed(time_step % 50 + 3)
|
| 142 |
+
eps = np.arange(1, 201)
|
|
|
|
|
|
|
| 143 |
if auto_scaling:
|
| 144 |
+
r = 900 * (1 - np.exp(-eps / 55)) + np.random.normal(0, 18, 200)
|
| 145 |
else:
|
| 146 |
+
r = 350 * (1 - np.exp(-eps / 90)) + np.random.normal(0, 12, 200)
|
| 147 |
+
r_cum = np.cumsum(np.clip(r / 180, 0, None))
|
| 148 |
|
| 149 |
+
fig, ax = plt.subplots(figsize=(5.5, 3.0))
|
| 150 |
style_ax(ax, fig)
|
| 151 |
+
ax.set_title("Cumulative Reward (RL Training)")
|
| 152 |
+
col = C["cyan"] if auto_scaling else C["muted"]
|
| 153 |
+
ax.plot(eps, r_cum, color=col, lw=1.8)
|
| 154 |
+
ax.fill_between(eps, r_cum, alpha=0.13, color=col)
|
| 155 |
+
ax.set_xlabel("Episodes", fontsize=9)
|
| 156 |
+
ax.set_ylabel("Cumulative reward", fontsize=9)
|
| 157 |
+
lbl = "RL Agent ON" if auto_scaling else "Static (no RL)"
|
| 158 |
+
ax.legend([lbl], fontsize=8, facecolor=C["panel"],
|
| 159 |
+
labelcolor=C["text"], edgecolor=C["border"])
|
| 160 |
+
plt.tight_layout(pad=1.0)
|
| 161 |
return fig
|
| 162 |
|
|
|
|
|
|
|
|
|
|
| 163 |
def make_comparison_chart(auto_scaling):
|
| 164 |
+
labels = ["Static\n(Before)", "RL Agent\n(After)"]
|
| 165 |
+
spend = [1250, 680 if auto_scaling else 1050]
|
| 166 |
+
downtime = [35, 8 if auto_scaling else 22]
|
| 167 |
+
colors = [C["muted"], C["green"] if auto_scaling else C["amber"]]
|
| 168 |
+
|
| 169 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5.5, 3.0))
|
| 170 |
+
for ax in (ax1, ax2):
|
| 171 |
+
style_ax(ax, fig)
|
| 172 |
+
|
| 173 |
+
bars1 = ax1.bar(labels, spend, color=colors,
|
| 174 |
+
width=0.5, edgecolor=C["border"], linewidth=0.5)
|
| 175 |
+
ax1.set_title("Total Spend ($)", color=C["text"], fontsize=10)
|
| 176 |
+
ax1.set_ylabel("USD ($)", fontsize=9)
|
| 177 |
+
for bar, v in zip(bars1, spend):
|
| 178 |
+
ax1.text(bar.get_x() + bar.get_width() / 2,
|
| 179 |
+
v + 18, f"${v}",
|
| 180 |
+
ha="center", color=C["text"], fontsize=9,
|
| 181 |
+
fontweight="bold")
|
| 182 |
+
|
| 183 |
+
bars2 = ax2.bar(labels, downtime, color=colors,
|
| 184 |
+
width=0.5, edgecolor=C["border"], linewidth=0.5)
|
| 185 |
+
ax2.set_title("Downtime Risk (%)", color=C["text"], fontsize=10)
|
| 186 |
+
ax2.set_ylabel("Risk %", fontsize=9)
|
| 187 |
+
for bar, v in zip(bars2, downtime):
|
| 188 |
+
ax2.text(bar.get_x() + bar.get_width() / 2,
|
| 189 |
+
v + 0.4, f"{v}%",
|
| 190 |
+
ha="center", color=C["text"], fontsize=9,
|
| 191 |
+
fontweight="bold")
|
| 192 |
+
|
| 193 |
+
if auto_scaling:
|
| 194 |
+
savings_pct = round((1 - 680 / 1250) * 100)
|
| 195 |
+
fig.suptitle(f"RL saved {savings_pct}% cost | {35-8}% less downtime",
|
| 196 |
+
color=C["green"], fontsize=10, y=1.03)
|
| 197 |
+
else:
|
| 198 |
+
fig.suptitle("Before vs After RL",
|
| 199 |
+
color=C["text"], fontsize=10, y=1.03)
|
| 200 |
+
plt.tight_layout(pad=1.0)
|
| 201 |
+
return fig
|
| 202 |
|
| 203 |
+
def make_whatif_chart(load_pct, spot_pct):
|
| 204 |
+
loads = np.arange(10, 101, 10)
|
| 205 |
+
cost_base = 0.08 * loads / 10
|
| 206 |
+
cost_spot = cost_base * (1 - spot_pct / 200)
|
| 207 |
+
lat_base = 20 + loads * 1.1
|
| 208 |
+
lat_spot = lat_base * 0.88
|
| 209 |
+
|
| 210 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5.5, 3.0))
|
| 211 |
for ax in (ax1, ax2):
|
| 212 |
style_ax(ax, fig)
|
| 213 |
|
| 214 |
+
ax1.plot(loads, cost_base, color=C["red"], lw=1.8,
|
| 215 |
+
label="On-demand")
|
| 216 |
+
ax1.plot(loads, cost_spot, color=C["green"], lw=1.8,
|
| 217 |
+
linestyle="--", label="With spot")
|
| 218 |
+
ax1.axvline(load_pct, color=C["amber"], lw=1.2,
|
| 219 |
+
linestyle=":", label=f"Current {load_pct}%")
|
| 220 |
+
ax1.set_title("Predicted Cost vs Load", color=C["text"], fontsize=10)
|
| 221 |
+
ax1.set_xlabel("Load (%)", fontsize=9)
|
| 222 |
+
ax1.set_ylabel("$/hr", fontsize=9)
|
| 223 |
+
ax1.legend(fontsize=7, facecolor=C["panel"],
|
| 224 |
+
labelcolor=C["text"], edgecolor=C["border"])
|
| 225 |
+
|
| 226 |
+
ax2.plot(loads, lat_base, color=C["amber"], lw=1.8,
|
| 227 |
+
label="No RL")
|
| 228 |
+
ax2.plot(loads, lat_spot, color=C["cyan"], lw=1.8,
|
| 229 |
+
linestyle="--", label="With RL")
|
| 230 |
+
ax2.axvline(load_pct, color=C["amber"], lw=1.2, linestyle=":")
|
| 231 |
+
ax2.axhline(100, color=C["red"], lw=1.0,
|
| 232 |
+
linestyle=":", label="SLA 100ms")
|
| 233 |
+
ax2.set_title("Predicted Latency vs Load", color=C["text"], fontsize=10)
|
| 234 |
+
ax2.set_xlabel("Load (%)", fontsize=9)
|
| 235 |
+
ax2.set_ylabel("ms", fontsize=9)
|
| 236 |
+
ax2.legend(fontsize=7, facecolor=C["panel"],
|
| 237 |
+
labelcolor=C["text"], edgecolor=C["border"])
|
| 238 |
+
|
| 239 |
+
plt.tight_layout(pad=1.0)
|
| 240 |
return fig
|
| 241 |
|
| 242 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 243 |
+
# AI DECISION ENGINE
|
| 244 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 245 |
+
|
| 246 |
+
DECISIONS = {
|
| 247 |
+
"scale_up": ("Scale Up (+2 servers)", "High latency spike detected"),
|
| 248 |
+
"scale_down": ("Scale Down (-1 server)", "Load dropping, saving cost"),
|
| 249 |
+
"spot_switch": ("Switch to Spot Instances", "Budget threshold approaching"),
|
| 250 |
+
"hold": ("Hold โ no change", "System within normal range"),
|
| 251 |
+
"chaos_resp": ("Emergency Scale Up (+4)", "Chaos event detected!"),
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
XAI_REASONS = {
|
| 255 |
+
"scale_up": ["Latency > 80ms threshold", "Load increasing trend (+12%)",
|
| 256 |
+
"Budget still under limit"],
|
| 257 |
+
"scale_down": ["Load dropped below 40%", "Servers underutilised",
|
| 258 |
+
"Cost optimisation triggered"],
|
| 259 |
+
"spot_switch": ["Burn rate > $0.45/hr", "Spot availability > 60%",
|
| 260 |
+
"Budget constraint active"],
|
| 261 |
+
"hold": ["All metrics nominal", "No threshold breached",
|
| 262 |
+
"Agent confidence high"],
|
| 263 |
+
"chaos_resp": ["Multiple server failures", "Latency > 150ms",
|
| 264 |
+
"Load spike > 200%"],
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
def get_decision(user_load, spot_avail, chaos_monkey,
|
| 268 |
+
budget_on, auto_scaling, latency):
|
| 269 |
+
if chaos_monkey and latency > 120:
|
| 270 |
+
key = "chaos_resp"
|
| 271 |
+
conf = random.randint(88, 99)
|
| 272 |
+
elif user_load > 80 and latency > 80:
|
| 273 |
+
key = "scale_up"
|
| 274 |
+
conf = random.randint(85, 97)
|
| 275 |
+
elif user_load < 35:
|
| 276 |
+
key = "scale_down"
|
| 277 |
+
conf = random.randint(78, 92)
|
| 278 |
+
elif budget_on and spot_avail > 50:
|
| 279 |
+
key = "spot_switch"
|
| 280 |
+
conf = random.randint(80, 94)
|
| 281 |
+
else:
|
| 282 |
+
key = "hold"
|
| 283 |
+
conf = random.randint(70, 88)
|
| 284 |
+
|
| 285 |
+
action, reason = DECISIONS[key]
|
| 286 |
+
xai = XAI_REASONS[key]
|
| 287 |
+
return action, reason, conf, xai
|
| 288 |
+
|
| 289 |
+
def health_status(latency, load, downtime_risk):
|
| 290 |
+
if latency > 120 or load > 85 or downtime_risk == "High":
|
| 291 |
+
return "๐ด CRITICAL", "System under stress โ immediate action needed"
|
| 292 |
+
elif latency > 70 or load > 65 or downtime_risk == "Medium":
|
| 293 |
+
return "๐ก WARNING", "Elevated load โ monitoring closely"
|
| 294 |
+
else:
|
| 295 |
+
return "๐ข HEALTHY", "All systems nominal"
|
| 296 |
+
|
| 297 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 298 |
+
# MAIN SIMULATION
|
| 299 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 300 |
+
|
| 301 |
+
_tick = 0
|
| 302 |
+
|
| 303 |
+
def run_simulation(scenario, user_load, spot_avail,
|
| 304 |
+
chaos_monkey, budget_on, auto_scaling):
|
| 305 |
+
global _tick
|
| 306 |
+
_tick += 1
|
| 307 |
+
|
| 308 |
+
t = datetime.now().strftime("%H:%M:%S")
|
| 309 |
+
load = int(user_load * 9.5)
|
| 310 |
+
servers = random.randint(8, 20)
|
| 311 |
+
burn = round(random.uniform(0.2,
|
| 312 |
+
0.45 if budget_on else 0.85), 2)
|
| 313 |
+
latency = random.randint(25,
|
| 314 |
+
75 if not chaos_monkey else 160)
|
| 315 |
+
score = round(random.uniform(0.82, 1.0)
|
| 316 |
+
if auto_scaling
|
| 317 |
+
else random.uniform(0.52, 0.78), 2)
|
| 318 |
+
spend = "$680" if auto_scaling else "$1,250"
|
| 319 |
+
savings = "45%" if auto_scaling else "0%"
|
| 320 |
+
drisk = ("Low" if latency < 60 and load < 600
|
| 321 |
+
else "Medium" if latency < 110
|
| 322 |
+
else "High")
|
| 323 |
+
|
| 324 |
+
action, reason, conf, xai = get_decision(
|
| 325 |
+
user_load, spot_avail, chaos_monkey,
|
| 326 |
+
budget_on, auto_scaling, latency)
|
| 327 |
+
|
| 328 |
+
health_badge, health_detail = health_status(
|
| 329 |
+
latency, load, drisk)
|
| 330 |
+
|
| 331 |
+
# โโ Decision card (Markdown) โโ
|
| 332 |
+
decision_md = f"""
|
| 333 |
+
### ๐ง AI Decision
|
| 334 |
+
**โ {action}**
|
| 335 |
+
|
| 336 |
+
| Field | Value |
|
| 337 |
+
|-------------|-------|
|
| 338 |
+
| Confidence | `{conf}%` |
|
| 339 |
+
| Reason | {reason} |
|
| 340 |
+
| Time | `{t}` |
|
| 341 |
+
"""
|
| 342 |
+
|
| 343 |
+
# โโ XAI panel โโ
|
| 344 |
+
xai_md = "### ๐ก Why did AI decide this?\n"
|
| 345 |
+
for r in xai:
|
| 346 |
+
xai_md += f"- โ {r}\n"
|
| 347 |
+
|
| 348 |
+
# โโ Health badge โโ
|
| 349 |
+
health_md = f"""
|
| 350 |
+
### {health_badge}
|
| 351 |
+
{health_detail}
|
| 352 |
+
|
| 353 |
+
| Metric | Value |
|
| 354 |
+
|----------|-------|
|
| 355 |
+
| Latency | `{latency} ms` |
|
| 356 |
+
| Load | `{load} req/s` |
|
| 357 |
+
| Risk | `{drisk}` |
|
| 358 |
+
| Score | `{score} / 1.0` |
|
| 359 |
+
"""
|
| 360 |
+
|
| 361 |
+
# โโ What-if prediction โโ
|
| 362 |
+
pred_cost = round(burn * (user_load / 50), 2)
|
| 363 |
+
pred_lat = round(latency * (user_load / 60), 1)
|
| 364 |
+
risk_lbl = ("HIGH" if pred_lat > 100 or pred_cost > 0.7
|
| 365 |
+
else "MEDIUM" if pred_lat > 60
|
| 366 |
+
else "LOW")
|
| 367 |
+
whatif_md = f"""
|
| 368 |
+
### ๐ฎ What-If Simulation
|
| 369 |
+
*If Load = {int(user_load)}%*
|
| 370 |
+
|
| 371 |
+
| Prediction | Value |
|
| 372 |
+
|------------------|-------|
|
| 373 |
+
| Predicted Cost | `${pred_cost}/hr` |
|
| 374 |
+
| Predicted Latency| `{pred_lat} ms` |
|
| 375 |
+
| Risk Level | `{risk_lbl}` |
|
| 376 |
+
"""
|
| 377 |
+
|
| 378 |
+
# โโ Agent log โโ
|
| 379 |
+
log = f"[{t}] Scenario : {scenario}\n"
|
| 380 |
+
log += f"[{t}] Load : {load} req/s | Servers: {servers}\n"
|
| 381 |
+
log += f"[{t}] Latency : {latency} ms | Burn: ${burn}/hr\n"
|
| 382 |
+
log += f"[{t}] Decision : {action}\n"
|
| 383 |
+
log += f"[{t}] Confidence : {conf}%\n"
|
| 384 |
+
log += f"[{t}] Agent Score : {score} / 1.0"
|
| 385 |
+
|
| 386 |
+
# โโ Charts โโ
|
| 387 |
+
tf = make_traffic_chart(user_load, _tick)
|
| 388 |
+
cf = make_cost_chart(budget_on, spot_avail, _tick)
|
| 389 |
+
pf = make_perf_chart(chaos_monkey, _tick)
|
| 390 |
+
rf = make_reward_chart(auto_scaling, _tick)
|
| 391 |
+
bf = make_comparison_chart(auto_scaling)
|
| 392 |
+
wf = make_whatif_chart(int(user_load), int(spot_avail))
|
| 393 |
|
| 394 |
return (
|
| 395 |
+
f"๐ {load} req/s",
|
| 396 |
+
f"๐ฅ๏ธ {servers} instances",
|
| 397 |
+
f"๐ธ ${burn}/hr",
|
| 398 |
+
f"โก {latency} ms",
|
| 399 |
+
f"๐ฐ {savings}",
|
| 400 |
+
decision_md,
|
| 401 |
+
xai_md,
|
| 402 |
+
health_md,
|
| 403 |
+
whatif_md,
|
| 404 |
log,
|
| 405 |
spend,
|
| 406 |
+
drisk,
|
|
|
|
| 407 |
score,
|
| 408 |
+
tf, cf, pf, rf, bf, wf,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
)
|
| 410 |
|
| 411 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโ
|
| 412 |
+
# CSS
|
| 413 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 414 |
+
|
| 415 |
CSS = """
|
| 416 |
+
/* Base */
|
| 417 |
body, .gradio-container {
|
| 418 |
background: #0a0e1a !important;
|
| 419 |
color: #e2e8f0 !important;
|
| 420 |
+
font-family: 'DM Sans','Segoe UI',sans-serif !important;
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
/* Panels */
|
| 424 |
+
.gr-panel, .gr-box, .gr-form, .gr-block {
|
| 425 |
+
background: #111827 !important;
|
| 426 |
+
border: 0.5px solid #1e2d45 !important;
|
| 427 |
+
border-radius: 12px !important;
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
/* Metric textboxes โ bigger numbers */
|
| 431 |
+
.metric-box textarea, .metric-box input {
|
| 432 |
+
font-size: 20px !important;
|
| 433 |
+
font-weight: 700 !important;
|
| 434 |
+
color: #00e5ff !important;
|
| 435 |
+
text-align: center !important;
|
| 436 |
+
background: #0d1525 !important;
|
| 437 |
+
border: 1px solid #1e2d45 !important;
|
| 438 |
+
border-radius: 10px !important;
|
| 439 |
+
padding: 14px !important;
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
/* Decision / XAI / Health cards */
|
| 443 |
+
.card-decision, .card-xai, .card-health, .card-whatif {
|
| 444 |
+
background: #111827 !important;
|
| 445 |
+
border-radius: 12px !important;
|
| 446 |
+
padding: 4px !important;
|
| 447 |
}
|
| 448 |
+
|
| 449 |
+
/* Primary button */
|
| 450 |
.gr-button-primary {
|
| 451 |
background: transparent !important;
|
| 452 |
+
border: 1.5px solid #00e5ff !important;
|
| 453 |
color: #00e5ff !important;
|
| 454 |
+
font-size: 15px !important;
|
| 455 |
+
font-weight: 700 !important;
|
| 456 |
+
letter-spacing: 1.5px !important;
|
| 457 |
+
border-radius: 10px !important;
|
| 458 |
+
transition: background 0.2s !important;
|
| 459 |
}
|
| 460 |
.gr-button-primary:hover {
|
| 461 |
+
background: rgba(0,229,255,0.12) !important;
|
| 462 |
}
|
| 463 |
+
|
| 464 |
+
/* Labels */
|
| 465 |
+
label, .gr-input-label {
|
| 466 |
+
color: #64748b !important;
|
| 467 |
+
font-size: 12px !important;
|
| 468 |
+
letter-spacing: 0.5px !important;
|
| 469 |
+
text-transform: uppercase !important;
|
| 470 |
}
|
| 471 |
+
|
| 472 |
+
/* Inputs / selects */
|
| 473 |
input, select, textarea {
|
| 474 |
background: #0d1525 !important;
|
| 475 |
color: #e2e8f0 !important;
|
| 476 |
border: 0.5px solid #1e2d45 !important;
|
| 477 |
+
border-radius: 8px !important;
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
/* Markdown headings */
|
| 481 |
+
.gr-markdown h3 {
|
| 482 |
+
color: #00e5ff !important;
|
| 483 |
+
font-size: 14px !important;
|
| 484 |
+
margin-bottom: 6px !important;
|
| 485 |
+
}
|
| 486 |
+
.gr-markdown table {
|
| 487 |
+
font-size: 13px !important;
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
/* Plot backgrounds */
|
| 491 |
+
.gr-plot {
|
| 492 |
+
background: #111827 !important;
|
| 493 |
+
border: 0.5px solid #1e2d45 !important;
|
| 494 |
+
border-radius: 10px !important;
|
| 495 |
+
padding: 4px !important;
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
/* Log box */
|
| 499 |
+
.log-box textarea {
|
| 500 |
+
font-family: 'Space Mono', monospace !important;
|
| 501 |
+
font-size: 12px !important;
|
| 502 |
+
color: #8fa8c8 !important;
|
| 503 |
+
background: #070c18 !important;
|
| 504 |
+
border: 0.5px solid #1e2d45 !important;
|
| 505 |
+
border-radius: 8px !important;
|
| 506 |
+
line-height: 1.8 !important;
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
/* Score number */
|
| 510 |
+
.score-box input {
|
| 511 |
+
font-size: 28px !important;
|
| 512 |
+
font-weight: 800 !important;
|
| 513 |
+
color: #00ff9d !important;
|
| 514 |
+
text-align: center !important;
|
| 515 |
+
background: #0d1525 !important;
|
| 516 |
+
border: 1px solid #1e2d45 !important;
|
| 517 |
+
border-radius: 10px !important;
|
| 518 |
+
}
|
| 519 |
+
|
| 520 |
+
/* Separators */
|
| 521 |
+
hr {
|
| 522 |
+
border-color: #1e2d45 !important;
|
| 523 |
+
margin: 12px 0 !important;
|
| 524 |
}
|
|
|
|
| 525 |
"""
|
| 526 |
|
| 527 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 528 |
+
# BUILD UI
|
| 529 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 530 |
+
|
| 531 |
with gr.Blocks(css=CSS, title="โ๏ธ Cloud-Cost Guardian AI") as demo:
|
| 532 |
|
| 533 |
+
# โโ Header โโ
|
| 534 |
gr.Markdown("""
|
| 535 |
+
# โ๏ธ Cloud-Cost Guardian AI
|
| 536 |
+
### RL-Powered Auto Scaling โ Live Intelligence Dashboard
|
| 537 |
+
---
|
| 538 |
+
""")
|
| 539 |
+
|
| 540 |
+
# โโ Row 1: 5 metric cards โโ
|
| 541 |
+
with gr.Row(equal_height=True):
|
| 542 |
+
load_out = gr.Textbox(label="Incoming Load",
|
| 543 |
+
interactive=False,
|
| 544 |
+
elem_classes="metric-box")
|
| 545 |
+
server_out = gr.Textbox(label="Active Servers",
|
| 546 |
+
interactive=False,
|
| 547 |
+
elem_classes="metric-box")
|
| 548 |
+
burn_out = gr.Textbox(label="Hourly Burn Rate",
|
| 549 |
+
interactive=False,
|
| 550 |
+
elem_classes="metric-box")
|
| 551 |
+
latency_out = gr.Textbox(label="Avg Latency",
|
| 552 |
+
interactive=False,
|
| 553 |
+
elem_classes="metric-box")
|
| 554 |
+
savings_out = gr.Textbox(label="Cost Savings",
|
| 555 |
+
interactive=False,
|
| 556 |
+
elem_classes="metric-box")
|
| 557 |
|
| 558 |
gr.Markdown("---")
|
| 559 |
|
| 560 |
+
# โโ Row 2: Controls | Decision + XAI + Health โโ
|
| 561 |
with gr.Row():
|
| 562 |
+
|
| 563 |
+
# Left: controls
|
| 564 |
+
with gr.Column(scale=1, min_width=260):
|
| 565 |
gr.Markdown("### ๐ฎ Simulation Controls")
|
| 566 |
+
|
| 567 |
scenario = gr.Dropdown(
|
| 568 |
choices=[
|
| 569 |
"Scenario 1: Steady Load (Easy)",
|
|
|
|
| 573 |
value="Scenario 2: Daily Peak Surges (Medium)",
|
| 574 |
label="Scenario"
|
| 575 |
)
|
| 576 |
+
user_load = gr.Slider(0, 100, value=75, step=1,
|
| 577 |
+
label="User Load Scale (%)")
|
| 578 |
+
spot_avail = gr.Slider(0, 100, value=40, step=1,
|
| 579 |
+
label="Spot Instance Availability (%)")
|
| 580 |
+
chaos_monkey = gr.Checkbox(value=False,
|
| 581 |
+
label="๐ Chaos Monkey (Hard Mode)")
|
| 582 |
+
budget_constr= gr.Checkbox(value=True,
|
| 583 |
+
label="๐ฐ Budget Constraint ON")
|
| 584 |
+
auto_scaling = gr.Checkbox(value=True,
|
| 585 |
+
label="๐ค RL Auto-Scaling Agent ON")
|
| 586 |
+
run_btn = gr.Button("๐ Reset & Restart Agent",
|
| 587 |
+
variant="primary", size="lg")
|
| 588 |
+
|
| 589 |
+
# Middle: AI Decision + XAI
|
| 590 |
+
with gr.Column(scale=1, min_width=260):
|
| 591 |
+
decision_out = gr.Markdown(
|
| 592 |
+
value="### ๐ง AI Decision\n*Click Reset to run...*",
|
| 593 |
+
elem_classes="card-decision"
|
| 594 |
+
)
|
| 595 |
+
xai_out = gr.Markdown(
|
| 596 |
+
value="### ๐ก Explainable AI\n*Waiting for simulation...*",
|
| 597 |
+
elem_classes="card-xai"
|
| 598 |
+
)
|
| 599 |
+
|
| 600 |
+
# Right: Health + What-If
|
| 601 |
+
with gr.Column(scale=1, min_width=260):
|
| 602 |
+
health_out = gr.Markdown(
|
| 603 |
+
value="### ๐ข Health Status\n*Click Reset to check...*",
|
| 604 |
+
elem_classes="card-health"
|
| 605 |
+
)
|
| 606 |
+
whatif_out = gr.Markdown(
|
| 607 |
+
value="### ๐ฎ What-If Simulation\n*Adjust sliders + click Reset*",
|
| 608 |
+
elem_classes="card-whatif"
|
| 609 |
+
)
|
| 610 |
+
|
| 611 |
+
gr.Markdown("---")
|
| 612 |
+
|
| 613 |
+
# โโ Row 3: Log + Score โโ
|
| 614 |
+
with gr.Row():
|
| 615 |
+
with gr.Column(scale=3):
|
| 616 |
+
log_out = gr.Textbox(
|
| 617 |
+
lines=5, label="๐ Agent Action Log",
|
| 618 |
+
interactive=False,
|
| 619 |
+
elem_classes="log-box"
|
| 620 |
+
)
|
| 621 |
+
with gr.Column(scale=1):
|
| 622 |
+
spend_out = gr.Textbox(label="Total Spend",
|
| 623 |
+
interactive=False)
|
| 624 |
+
risk_out = gr.Textbox(label="Downtime Risk",
|
| 625 |
+
interactive=False)
|
| 626 |
+
score_out = gr.Number(
|
| 627 |
+
label="๐ Agent Score / 1.0",
|
| 628 |
+
interactive=False,
|
| 629 |
+
elem_classes="score-box"
|
| 630 |
+
)
|
| 631 |
|
| 632 |
gr.Markdown("---")
|
| 633 |
gr.Markdown("### ๐ Live Performance Graphs")
|
| 634 |
|
| 635 |
+
# โโ Row 4: Top 3 charts โโ
|
| 636 |
with gr.Row():
|
| 637 |
+
traffic_plot = gr.Plot(label="Traffic & Server Scaling")
|
| 638 |
+
cost_plot = gr.Plot(label="Cost Efficiency")
|
| 639 |
+
perf_plot = gr.Plot(label="Response Performance")
|
| 640 |
|
| 641 |
+
# โโ Row 5: Bottom 3 charts โโ
|
| 642 |
with gr.Row():
|
| 643 |
+
reward_plot = gr.Plot(label="Cumulative RL Reward")
|
| 644 |
+
compare_plot = gr.Plot(label="Before vs After RL")
|
| 645 |
+
whatif_plot = gr.Plot(label="What-If: Cost & Latency")
|
| 646 |
|
| 647 |
gr.Markdown("""
|
| 648 |
+
---
|
| 649 |
+
> ๐ก **Tip:** Adjust any slider or toggle, then click **Reset & Restart Agent** to see all
|
| 650 |
+
> panels, cards and graphs update live. Try enabling **Chaos Monkey** for dramatic spikes!
|
| 651 |
+
""")
|
| 652 |
|
| 653 |
+
# โโ Wire up โโ
|
| 654 |
run_btn.click(
|
| 655 |
fn=run_simulation,
|
| 656 |
+
inputs=[
|
| 657 |
+
scenario, user_load, spot_avail,
|
| 658 |
+
chaos_monkey, budget_constr, auto_scaling
|
| 659 |
+
],
|
| 660 |
outputs=[
|
| 661 |
+
load_out, server_out, burn_out,
|
| 662 |
+
latency_out, savings_out,
|
| 663 |
+
decision_out, xai_out,
|
| 664 |
+
health_out, whatif_out,
|
| 665 |
+
log_out, spend_out, risk_out, score_out,
|
| 666 |
+
traffic_plot, cost_plot, perf_plot,
|
| 667 |
+
reward_plot, compare_plot, whatif_plot,
|
| 668 |
]
|
| 669 |
)
|
| 670 |
|