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

Update app.py

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