ankitxraj21 commited on
Commit
9b2e770
ยท
verified ยท
1 Parent(s): e465212

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import time
4
+ from datetime import datetime
5
+
6
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
7
+ # Import your friend's code here (Step 3)
8
+ # from cloud_guardian import run_agent
9
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
10
+
11
+ def get_timestamp():
12
+ return datetime.now().strftime("%H:%M:%S")
13
+
14
+ def run_simulation(scenario, user_load, spot_avail, chaos_monkey, budget_on, auto_scaling):
15
+ """
16
+ RIGHT NOW: uses fake random data so you can see the UI work.
17
+ LATER: replace the inside with your friend's real function.
18
+ """
19
+ t = get_timestamp()
20
+
21
+ # Fake metrics โ€” replace with real ones later
22
+ load = int(user_load * 10)
23
+ servers = random.randint(8, 20)
24
+ burn_rate = round(random.uniform(0.2, 0.6), 2)
25
+ latency = random.randint(30, 120)
26
+ score = round(random.uniform(0.75, 1.0), 2)
27
+
28
+ spend = "$680" if auto_scaling else "$1,250"
29
+ downtime = "๐ŸŸข Low" if auto_scaling else "๐ŸŸก Medium"
30
+
31
+ log = f"[{t}] Load: {load} โ†’ ACTION: Add Instance (t3.medium)\n"
32
+ log += f"[{t}] Latency improved to {latency}ms. Budget Remaining: $12\n"
33
+ log += f"[{t}] Cost > Threshold โ†’ ACTION: Switch to Spot Instance\n"
34
+ log += f"[{t}] Final Score: {score} / 1.0"
35
+
36
+ return (
37
+ f"๐Ÿ”ฅ {load} req/sec",
38
+ f"๐Ÿ–ฅ๏ธ {servers} instances",
39
+ f"๐Ÿ’ธ ${burn_rate}/hr",
40
+ log,
41
+ spend,
42
+ downtime,
43
+ score
44
+ )
45
+
46
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
47
+ # BUILD THE UI
48
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
49
+ with gr.Blocks(
50
+ title="โ˜๏ธ Cloud-Cost Guardian AI",
51
+ theme=gr.themes.Base(
52
+ primary_hue="cyan",
53
+ neutral_hue="slate",
54
+ ),
55
+ css="""
56
+ .gradio-container { max-width: 1100px !important; }
57
+ .metric-box { text-align: center; font-size: 1.4em; font-weight: bold; }
58
+ """
59
+ ) as demo:
60
+
61
+ # โ”€โ”€ Header โ”€โ”€
62
+ gr.Markdown("""
63
+ # โ˜๏ธ Cloud-Cost Guardian AI
64
+ ### Live Simulation Dashboard
65
+ ---
66
+ """)
67
+
68
+ # โ”€โ”€ Metric cards row โ”€โ”€
69
+ with gr.Row():
70
+ load_out = gr.Textbox(label="๐Ÿ“ˆ Incoming Load", elem_classes="metric-box", interactive=False)
71
+ server_out = gr.Textbox(label="๐Ÿ–ฅ๏ธ Active Servers", elem_classes="metric-box", interactive=False)
72
+ burn_out = gr.Textbox(label="๐Ÿ’ธ Hourly Burn Rate", elem_classes="metric-box", interactive=False)
73
+
74
+ gr.Markdown("---")
75
+
76
+ # โ”€โ”€ Controls + Log side by side โ”€โ”€
77
+ with gr.Row():
78
+
79
+ # Left column โ€” controls
80
+ with gr.Column(scale=1):
81
+ gr.Markdown("### ๐ŸŽฎ Simulation Controls")
82
+
83
+ scenario = gr.Dropdown(
84
+ choices=[
85
+ "Scenario 1: Steady Load (Easy)",
86
+ "Scenario 2: Daily Peak Surges (Medium)",
87
+ "Scenario 3: Chaos Mode (Hard)"
88
+ ],
89
+ value="Scenario 2: Daily Peak Surges (Medium)",
90
+ label="Select Scenario"
91
+ )
92
+ user_load = gr.Slider(0, 100, value=75, step=1, label="User Load Scale (%)")
93
+ spot_avail = gr.Slider(0, 100, value=40, step=1, label="Spot Instance Availability (%)")
94
+ chaos_monkey = gr.Checkbox(value=False, label="๐Ÿ’ Chaos Monkey (Hard Tasks)")
95
+ budget_constr= gr.Checkbox(value=True, label="๐Ÿ’ฐ Budget Constraint")
96
+ auto_scaling = gr.Checkbox(value=True, label="๐Ÿค– Auto-Scaling Agent")
97
+
98
+ run_btn = gr.Button("๐Ÿš€ Reset Environment & Restart Agent", variant="primary", size="lg")
99
+
100
+ # Right column โ€” outputs
101
+ with gr.Column(scale=2):
102
+ gr.Markdown("### ๐Ÿ“‹ Agent Action Log")
103
+ log_out = gr.Textbox(lines=6, label="Live Log", interactive=False)
104
+
105
+ with gr.Row():
106
+ spend_out = gr.Textbox(label="Total Spend", interactive=False)
107
+ risk_out = gr.Textbox(label="Downtime Risk", interactive=False)
108
+
109
+ score_out = gr.Number(label="๐Ÿ† Final Agent Score (out of 1.0)", interactive=False)
110
+
111
+ # โ”€โ”€ Wire button to function โ”€โ”€
112
+ run_btn.click(
113
+ fn=run_simulation,
114
+ inputs=[scenario, user_load, spot_avail, chaos_monkey, budget_constr, auto_scaling],
115
+ outputs=[load_out, server_out, burn_out, log_out, spend_out, risk_out, score_out]
116
+ )
117
+
118
+ gr.Markdown("""
119
+ ---
120
+ > ๐Ÿ’ก Tip: Click **Reset Environment** after changing any setting to see updated results.
121
+ """)
122
+
123
+ # โ”€โ”€ Launch โ”€โ”€
124
+ if __name__ == "__main__":
125
+ demo.launch(share=True) # share=True gives you a public link