Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
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
|