ankitxraj21 commited on
Commit
d6f0062
ยท
verified ยท
1 Parent(s): 3990683

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +567 -213
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
- # DARK THEME STYLE FOR ALL CHARTS
12
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
13
- DARK = {
14
- "bg": "#0a0e1a",
15
- "panel": "#111827",
16
- "cyan": "#00e5ff",
17
- "green": "#00ff9d",
18
- "amber": "#ffb300",
19
- "red": "#ff4d6d",
20
- "muted": "#64748b",
21
- "text": "#e2e8f0",
 
 
22
  }
23
 
24
  def style_ax(ax, fig):
25
- fig.patch.set_facecolor(DARK["panel"])
26
- ax.set_facecolor(DARK["panel"])
27
- ax.tick_params(colors=DARK["muted"], labelsize=8)
28
- ax.xaxis.label.set_color(DARK["muted"])
29
- ax.yaxis.label.set_color(DARK["muted"])
30
- for spine in ax.spines.values():
31
- spine.set_edgecolor("#1e2d45")
32
- ax.grid(True, color="#1e2d45", linewidth=0.5, linestyle="--", alpha=0.6)
33
-
34
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
35
- # CHART 1 โ€” Traffic & Server Scaling (Line)
36
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
37
- def make_traffic_chart(user_load):
 
 
 
 
 
38
  steps = 90
39
- t = np.arange(steps)
40
- base_load = user_load * 8
41
- load = base_load + 200 * np.sin(t / 12) + np.random.normal(0, 30, steps)
42
  load = np.clip(load, 0, 1000)
43
- servers = load / 70 + np.random.normal(0, 0.5, steps)
44
- servers = np.clip(servers, 1, 20)
45
 
46
- fig, ax = plt.subplots(figsize=(5, 2.6))
 
 
 
47
  style_ax(ax, fig)
48
- ax.plot(t, load, color=DARK["green"], linewidth=1.5, label="Incoming load")
49
- ax.plot(t, servers * 40, color=DARK["cyan"], linewidth=1.5, label="Active servers (ร—40)", linestyle="--")
50
- ax.set_xlabel("Time (steps)", fontsize=8)
51
- ax.set_ylabel("Load", fontsize=8)
52
- ax.set_title("Traffic & Server Scaling", color=DARK["text"], fontsize=10, pad=8)
53
- p1 = mpatches.Patch(color=DARK["green"], label="Incoming load")
54
- p2 = mpatches.Patch(color=DARK["cyan"], label="Active servers")
55
- ax.legend(handles=[p1, p2], fontsize=7, facecolor=DARK["panel"],
56
- labelcolor=DARK["text"], edgecolor="#1e2d45", loc="upper right")
57
- plt.tight_layout(pad=0.8)
 
 
 
 
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
- t = np.arange(steps)
66
- budget_line = 0.50
67
- burn = 0.80 * np.exp(-t / 40) + 0.05 + np.random.normal(0, 0.02, steps)
 
68
  if budget_on:
69
- burn = np.clip(burn, None, budget_line + 0.05)
70
  if spot_avail > 50:
71
- burn *= 0.75
 
 
72
 
73
- fig, ax = plt.subplots(figsize=(5, 2.6))
74
  style_ax(ax, fig)
75
- ax.plot(t, burn, color=DARK["red"], linewidth=1.5, label="Hourly burn rate ($)")
76
- ax.axhline(budget_line, color=DARK["amber"], linewidth=1, linestyle="--", label="Target budget")
77
- ax.set_xlabel("Time (steps)", fontsize=8)
78
- ax.set_ylabel("Hourly burn rate ($)", fontsize=8)
79
- ax.set_title("Cost Efficiency", color=DARK["text"], fontsize=10, pad=8)
80
- p1 = mpatches.Patch(color=DARK["red"], label="Hourly burn rate ($)")
81
- p2 = mpatches.Patch(color=DARK["amber"], label="Target budget")
82
- ax.legend(handles=[p1, p2], fontsize=7, facecolor=DARK["panel"],
83
- labelcolor=DARK["text"], edgecolor="#1e2d45")
84
- plt.tight_layout(pad=0.8)
 
 
 
 
85
  return fig
86
 
87
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
88
- # CHART 3 โ€” Response Performance (Line)
89
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
90
- def make_perf_chart(chaos_monkey):
91
  steps = 90
92
- t = np.arange(steps)
93
- latency = 40 + 20 * np.sin(t / 15) + np.random.normal(0, 5, steps)
 
 
94
  if chaos_monkey:
95
- spikes = np.random.choice(steps, size=6, replace=False)
96
- latency[spikes] += np.random.uniform(80, 160, size=6)
97
- latency = np.clip(latency, 0, 200)
 
98
 
99
- fig, ax = plt.subplots(figsize=(5, 2.6))
100
  style_ax(ax, fig)
101
- ax.plot(t, latency, color=DARK["amber"], linewidth=1.5)
102
- ax.fill_between(t, latency, alpha=0.15, color=DARK["amber"])
103
- ax.set_xlabel("Time (steps)", fontsize=8)
104
- ax.set_ylabel("Avg latency (ms)", fontsize=8)
105
- ax.set_title("Response Performance (99th pct SLA)", color=DARK["text"], fontsize=10, pad=8)
106
- plt.tight_layout(pad=0.8)
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  return fig
108
 
109
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
110
- # CHART 4 โ€” Cumulative Reward (Area)
111
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
112
- def make_reward_chart(auto_scaling):
113
- episodes = np.arange(1, 201)
114
  if auto_scaling:
115
- reward = 1000 * (1 - np.exp(-episodes / 60)) + np.random.normal(0, 20, 200)
116
  else:
117
- reward = 400 * (1 - np.exp(-episodes / 80)) + np.random.normal(0, 15, 200)
118
- reward = np.cumsum(np.clip(reward / 200, 0, None))
119
 
120
- fig, ax = plt.subplots(figsize=(5, 2.6))
121
  style_ax(ax, fig)
122
- color = DARK["cyan"] if auto_scaling else DARK["muted"]
123
- ax.plot(episodes, reward, color=color, linewidth=1.5)
124
- ax.fill_between(episodes, reward, alpha=0.15, color=color)
125
- ax.set_xlabel("Episodes", fontsize=8)
126
- ax.set_ylabel("Cumulative reward", fontsize=8)
127
- ax.set_title("Cumulative Reward over Episodes", color=DARK["text"], fontsize=10, pad=8)
128
- plt.tight_layout(pad=0.8)
 
 
 
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 = ["Static\n(Before)", "RL Agent\n(After)"]
136
- spend = [1250, 680 if auto_scaling else 1050]
137
- downtime = [35, 8 if auto_scaling else 22]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
- fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5, 2.6))
 
 
 
 
 
 
 
140
  for ax in (ax1, ax2):
141
  style_ax(ax, fig)
142
 
143
- colors = [DARK["muted"], DARK["green"] if auto_scaling else DARK["amber"]]
144
- ax1.bar(labels, spend, color=colors, width=0.5, edgecolor="#1e2d45")
145
- ax1.set_title("Total spend ($)", color=DARK["text"], fontsize=9)
146
- ax1.set_ylabel("$", fontsize=8)
147
- for i, v in enumerate(spend):
148
- ax1.text(i, v + 20, f"${v}", ha="center", color=DARK["text"], fontsize=8)
149
-
150
- ax2.bar(labels, downtime, color=colors, width=0.5, edgecolor="#1e2d45")
151
- ax2.set_title("Downtime risk (%)", color=DARK["text"], fontsize=9)
152
- ax2.set_ylabel("%", fontsize=8)
153
- for i, v in enumerate(downtime):
154
- ax2.text(i, v + 0.5, f"{v}%", ha="center", color=DARK["text"], fontsize=8)
155
-
156
- plt.suptitle("Before vs After RL", color=DARK["text"], fontsize=10, y=1.02)
157
- plt.tight_layout(pad=0.8)
 
 
 
 
 
 
 
 
 
 
 
158
  return fig
159
 
160
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
161
- # MAIN SIMULATION FUNCTION
162
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
163
- def run_simulation(scenario, user_load, spot_avail, chaos_monkey, budget_on, auto_scaling):
164
- t = datetime.now().strftime("%H:%M:%S")
165
-
166
- load = int(user_load * 10)
167
- servers = random.randint(8, 20)
168
- burn = round(random.uniform(0.2, 0.5 if budget_on else 0.8), 2)
169
- latency = random.randint(25, 80 if not chaos_monkey else 160)
170
- score = round(random.uniform(0.80, 1.0) if auto_scaling else random.uniform(0.55, 0.80), 2)
171
- spend = "$680" if auto_scaling else "$1,250"
172
- downtime = "Low" if auto_scaling else "Medium"
173
- savings = "45%" if auto_scaling else "0%"
174
-
175
- log = f"[{t}] Scenario : {scenario}\n"
176
- log += f"[{t}] Load : {load} req/s โ†’ Servers: {servers}\n"
177
- log += f"[{t}] Latency : {latency} ms\n"
178
- log += f"[{t}] Burn rate : ${burn}/hr\n"
179
- log += f"[{t}] ACTION : {'Scale up (RL agent)' if auto_scaling else 'Static allocation'}\n"
180
- log += f"[{t}] Score : {score} / 1.0"
181
-
182
- traffic_fig = make_traffic_chart(user_load)
183
- cost_fig = make_cost_chart(budget_on, spot_avail)
184
- perf_fig = make_perf_chart(chaos_monkey)
185
- reward_fig = make_reward_chart(auto_scaling)
186
- comparison_fig = make_comparison_chart(auto_scaling)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
  return (
189
- f"๐Ÿ“ˆ {load} req/s",
190
- f"๐Ÿ–ฅ๏ธ {servers} instances",
191
- f"๐Ÿ’ธ ${burn}/hr",
192
- f"โšก {latency} ms",
 
 
 
 
 
193
  log,
194
  spend,
195
- downtime,
196
- savings,
197
  score,
198
- traffic_fig,
199
- cost_fig,
200
- perf_fig,
201
- reward_fig,
202
- comparison_fig,
203
  )
204
 
205
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
206
- # CUSTOM CSS
207
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
208
  CSS = """
 
209
  body, .gradio-container {
210
  background: #0a0e1a !important;
211
  color: #e2e8f0 !important;
212
- font-family: 'Space Grotesk', 'Segoe UI', sans-serif !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  }
 
 
214
  .gr-button-primary {
215
  background: transparent !important;
216
- border: 1px solid #00e5ff !important;
217
  color: #00e5ff !important;
218
- letter-spacing: 1px;
219
- font-weight: 600;
 
 
 
220
  }
221
  .gr-button-primary:hover {
222
- background: rgba(0,229,255,0.1) !important;
223
  }
224
- .gr-panel, .gr-box {
225
- background: #111827 !important;
226
- border: 0.5px solid #1e2d45 !important;
227
- border-radius: 10px !important;
 
 
 
228
  }
229
- label, .gr-input-label { color: #64748b !important; font-size: 12px !important; }
 
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 THE UI
240
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
241
  with gr.Blocks(css=CSS, title="โ˜๏ธ Cloud-Cost Guardian AI") as demo:
242
 
 
243
  gr.Markdown("""
244
- # โ˜๏ธ Cloud-Cost Guardian AI
245
- ### Live Simulation Dashboard โ€” RL-Powered Auto Scaling
246
- ---
247
- """)
248
-
249
- # โ”€โ”€ Row 1: Metric cards โ”€โ”€
250
- with gr.Row():
251
- load_out = gr.Textbox(label="Incoming Load", interactive=False)
252
- server_out = gr.Textbox(label="Active Servers", interactive=False)
253
- burn_out = gr.Textbox(label="Hourly Burn Rate", interactive=False)
254
- latency_out = gr.Textbox(label="Avg Latency", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
255
 
256
  gr.Markdown("---")
257
 
258
- # โ”€โ”€ Row 2: Controls + Log โ”€โ”€
259
  with gr.Row():
260
- with gr.Column(scale=1):
 
 
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, label="User Load Scale (%)")
272
- spot_avail = gr.Slider(0, 100, value=40, step=1, label="Spot Instance Availability (%)")
273
- chaos_monkey = gr.Checkbox(value=False, label="๐Ÿ’ Chaos Monkey")
274
- budget_constr= gr.Checkbox(value=True, label="๐Ÿ’ฐ Budget Constraint")
275
- auto_scaling = gr.Checkbox(value=True, label="๐Ÿค– Auto-Scaling Agent (RL)")
276
- run_btn = gr.Button("๐Ÿš€ Reset & Restart Agent", variant="primary", size="lg")
277
-
278
- with gr.Column(scale=2):
279
- gr.Markdown("### ๐Ÿ“‹ Agent Action Log")
280
- log_out = gr.Textbox(lines=7, label="Live Log", interactive=False)
281
- with gr.Row():
282
- spend_out = gr.Textbox(label="Total Spend", interactive=False)
283
- risk_out = gr.Textbox(label="Downtime Risk", interactive=False)
284
- savings_out = gr.Textbox(label="Cost Savings", interactive=False)
285
- score_out = gr.Number(label="๐Ÿ† Final Agent Score (out of 1.0)", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
  gr.Markdown("---")
288
  gr.Markdown("### ๐Ÿ“Š Live Performance Graphs")
289
 
290
- # โ”€โ”€ Row 3: Top 3 charts โ”€โ”€
291
  with gr.Row():
292
- traffic_plot = gr.Plot(label="Traffic & Server Scaling")
293
- cost_plot = gr.Plot(label="Cost Efficiency")
294
- perf_plot = gr.Plot(label="Response Performance")
295
 
296
- # โ”€โ”€ Row 4: Bottom 2 charts โ”€โ”€
297
  with gr.Row():
298
- reward_plot = gr.Plot(label="Cumulative Reward (RL Training)")
299
- comparison_plot = gr.Plot(label="Before vs After RL")
 
300
 
301
  gr.Markdown("""
302
- ---
303
- > ๐Ÿ’ก Adjust any control above and click **Reset & Restart Agent** to see all graphs update live.
304
- """)
 
305
 
306
- # โ”€โ”€ Wire everything up โ”€โ”€
307
  run_btn.click(
308
  fn=run_simulation,
309
- inputs=[scenario, user_load, spot_avail, chaos_monkey, budget_constr, auto_scaling],
 
 
 
310
  outputs=[
311
- load_out, server_out, burn_out, latency_out,
312
- log_out, spend_out, risk_out, savings_out, score_out,
313
- traffic_plot, cost_plot, perf_plot, reward_plot, comparison_plot,
 
 
 
 
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