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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from typing import Literal
|
| 3 |
+
|
| 4 |
+
# --- CONFIG ---
|
| 5 |
+
LOAD_SIGNATURE_LIMIT_MIN = 15
|
| 6 |
+
CRITICAL_TASK = "Water Pump"
|
| 7 |
+
|
| 8 |
+
def execute_autonomous_logic(
|
| 9 |
+
power_source: Literal["Grid_ON", "Inverter_ON", "Grid_OFF"],
|
| 10 |
+
time_elapsed_min: float
|
| 11 |
+
):
|
| 12 |
+
action = "IDLE"
|
| 13 |
+
reason = "Awaiting command."
|
| 14 |
+
status_color = "#000000"
|
| 15 |
+
|
| 16 |
+
# RULE 1 β Battery Protection
|
| 17 |
+
if power_source == "Inverter_ON":
|
| 18 |
+
action = "SHUTDOWN_PUMP"
|
| 19 |
+
reason = "CRITICAL FAILURE: Inverter detected. Protecting battery by shutting down pump."
|
| 20 |
+
status_color = "#ff3b3b"
|
| 21 |
+
|
| 22 |
+
# RULE 2 β Grid ON
|
| 23 |
+
elif power_source == "Grid_ON":
|
| 24 |
+
if time_elapsed_min < LOAD_SIGNATURE_LIMIT_MIN:
|
| 25 |
+
action = "RUN_PUMP"
|
| 26 |
+
remaining = LOAD_SIGNATURE_LIMIT_MIN - time_elapsed_min
|
| 27 |
+
reason = f"AUTO-START: Grid ON. Running for {remaining:.1f} min."
|
| 28 |
+
status_color = "#21c55d"
|
| 29 |
+
else:
|
| 30 |
+
action = "SHUTDOWN_PUMP"
|
| 31 |
+
reason = f"LOAD SIGNATURE EXCEEDED: Ran more than {LOAD_SIGNATURE_LIMIT_MIN} min."
|
| 32 |
+
status_color = "#facc15"
|
| 33 |
+
|
| 34 |
+
# RULE 3 β Blackout
|
| 35 |
+
else:
|
| 36 |
+
action = "IDLE"
|
| 37 |
+
reason = "Blackout detected. System idle."
|
| 38 |
+
status_color = "#6b7280"
|
| 39 |
+
|
| 40 |
+
# Output with internal color
|
| 41 |
+
action_html = f"""
|
| 42 |
+
<div style="font-size:22px; font-weight:900; color:{status_color};">
|
| 43 |
+
COMMAND: {action}
|
| 44 |
+
</div>
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
reason_html = f"""
|
| 48 |
+
<div style="font-size:18px; font-weight:600; color:{status_color};">
|
| 49 |
+
{reason}
|
| 50 |
+
</div>
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
return action_html, reason_html, status_color
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# --- NEW UNIQUE OUTPUT BOX STYLE + COLOURED TEXT ---
|
| 57 |
+
custom_css = """
|
| 58 |
+
|
| 59 |
+
/* MAIN BACKGROUND */
|
| 60 |
+
.gradio-container {
|
| 61 |
+
background: linear-gradient(135deg, #ddeaff 0%, #ffffff 50%, #e9ffe9 100%);
|
| 62 |
+
border-radius: 20px;
|
| 63 |
+
padding: 30px !important;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
/* TITLES */
|
| 67 |
+
h1, h2 {
|
| 68 |
+
font-weight: 800 !important;
|
| 69 |
+
text-align: center;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/* RADIO CARD STYLE */
|
| 73 |
+
.gr-radio div {
|
| 74 |
+
border-radius: 16px !important;
|
| 75 |
+
padding: 12px !important;
|
| 76 |
+
background: #f8fbff !important;
|
| 77 |
+
border: 2.7px solid #cce5ff !important;
|
| 78 |
+
transition: 0.3s;
|
| 79 |
+
margin-bottom: 7px !important;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.gr-radio div:hover {
|
| 83 |
+
background: #e4f1ff !important;
|
| 84 |
+
transform: translateY(-3px);
|
| 85 |
+
box-shadow: 0 4px 14px rgba(0, 120, 255, 0.25);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.gr-radio label {
|
| 89 |
+
font-size: 17px !important;
|
| 90 |
+
font-weight: 700 !important;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
/* SLIDER BOX */
|
| 94 |
+
.gr-slider {
|
| 95 |
+
background: #ffffff;
|
| 96 |
+
padding: 14px;
|
| 97 |
+
border-radius: 16px;
|
| 98 |
+
border: 3px solid #e3e3f5;
|
| 99 |
+
box-shadow: 0 5px 16px rgba(0,0,0,0.08);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/* SUPER BOLD BUTTONS */
|
| 103 |
+
button {
|
| 104 |
+
background: linear-gradient(135deg, #4f46e5, #6b21a8) !important;
|
| 105 |
+
color: #ffffff !important;
|
| 106 |
+
font-size: 18px !important;
|
| 107 |
+
font-weight: 900 !important;
|
| 108 |
+
border-radius: 16px !important;
|
| 109 |
+
padding: 14px 20px !important;
|
| 110 |
+
border: 4px solid #3a2bbf !important;
|
| 111 |
+
transition: 0.25s !important;
|
| 112 |
+
box-shadow: 0 6px 15px rgba(50, 0, 180, 0.35) !important;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
button:hover {
|
| 116 |
+
transform: translateY(-4px) scale(1.04);
|
| 117 |
+
box-shadow: 0 12px 28px rgba(90, 0, 200, 0.55) !important;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
/* π₯ SPECIAL OUTPUT BOX WITH TEXTURE + COLOR */
|
| 121 |
+
.gr-html {
|
| 122 |
+
border-radius: 20px !important;
|
| 123 |
+
padding: 22px !important;
|
| 124 |
+
|
| 125 |
+
background: linear-gradient(135deg, #ffffff 0%, #f8f9ff 50%, #eef6ff 100%) !important;
|
| 126 |
+
|
| 127 |
+
border: 4px solid #d1ddff !important;
|
| 128 |
+
box-shadow:
|
| 129 |
+
inset 0 0 15px rgba(150,150,255,0.25),
|
| 130 |
+
0 8px 18px rgba(0,0,0,0.08) !important;
|
| 131 |
+
|
| 132 |
+
font-weight: 700 !important;
|
| 133 |
+
letter-spacing: 0.4px !important;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/* DEFAULT BUTTON STYLE (for clear/reset etc) */
|
| 137 |
+
button {
|
| 138 |
+
background: linear-gradient(135deg, #4f46e5, #6b21a8) !important;
|
| 139 |
+
color: #ffffff !important;
|
| 140 |
+
font-size: 18px !important;
|
| 141 |
+
font-weight: 900 !important;
|
| 142 |
+
border-radius: 16px !important;
|
| 143 |
+
padding: 14px 20px !important;
|
| 144 |
+
border: 4px solid #3a2bbf !important;
|
| 145 |
+
transition: 0.25s !important;
|
| 146 |
+
box-shadow: 0 6px 15px rgba(50, 0, 180, 0.35) !important;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
/* β SPECIAL LIGHT COLORED FLAG (SUBMIT) BUTTON β */
|
| 150 |
+
button.primary {
|
| 151 |
+
background: linear-gradient(135deg, #b3e5ff, #e1f6ff) !important;
|
| 152 |
+
color: #003c5f !important;
|
| 153 |
+
border: 3px solid #7ecaff !important;
|
| 154 |
+
font-weight: 900 !important;
|
| 155 |
+
box-shadow: 0 6px 15px rgba(120, 180, 255, 0.45) !important;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
button.primary:hover {
|
| 159 |
+
transform: translateY(-4px) scale(1.04);
|
| 160 |
+
background: linear-gradient(135deg, #d7f2ff, #f1fbff) !important;
|
| 161 |
+
border-color: #55b8ff !important;
|
| 162 |
+
box-shadow: 0 10px 26px rgba(120, 180, 255, 0.55) !important;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
/* HIDE the HuggingFace 'Shareable link' button */
|
| 166 |
+
button#share-btn,
|
| 167 |
+
#share-btn {
|
| 168 |
+
display: none !important;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
# --- INTERFACE ---
|
| 174 |
+
iface = gr.Interface(
|
| 175 |
+
fn=execute_autonomous_logic,
|
| 176 |
+
inputs=[
|
| 177 |
+
gr.Radio(
|
| 178 |
+
["Grid_ON", "Inverter_ON", "Grid_OFF"],
|
| 179 |
+
label="π Power Source Sensor"
|
| 180 |
+
),
|
| 181 |
+
gr.Slider(
|
| 182 |
+
0, 30, step=0.1,
|
| 183 |
+
label="β± Time Elapsed (minutes)"
|
| 184 |
+
)
|
| 185 |
+
],
|
| 186 |
+
outputs=[
|
| 187 |
+
gr.HTML(label="βοΈ PowerSense Command"),
|
| 188 |
+
gr.HTML(label="π Reasoning Output"),
|
| 189 |
+
gr.Textbox(visible=False)
|
| 190 |
+
],
|
| 191 |
+
title="π PowerSense AI β Autonomous Pump Controller",
|
| 192 |
+
description="Smart logic engine with Load Signature + Battery Protection.",
|
| 193 |
+
css=custom_css
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
iface.launch()
|