Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,11 @@ import gradio as gr
|
|
| 5 |
import openstack
|
| 6 |
from openstack import exceptions
|
| 7 |
|
| 8 |
-
#
|
| 9 |
active_user_count = 0
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def get_conn():
|
| 12 |
return openstack.connect(
|
|
@@ -19,7 +22,7 @@ def get_conn():
|
|
| 19 |
)
|
| 20 |
|
| 21 |
def get_current_status():
|
| 22 |
-
global active_user_count
|
| 23 |
try:
|
| 24 |
conn = get_conn()
|
| 25 |
instance_id = os.getenv("OS_INSTANCE_ID")
|
|
@@ -36,43 +39,60 @@ def get_current_status():
|
|
| 36 |
}
|
| 37 |
status_text = status_map.get(server.status, f"β STATE: {server.status}")
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
return f"β
|
| 42 |
|
| 43 |
def user_joined():
|
| 44 |
-
global active_user_count
|
| 45 |
active_user_count += 1
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def user_left():
|
| 49 |
global active_user_count
|
| 50 |
active_user_count = max(0, active_user_count - 1)
|
| 51 |
|
| 52 |
def manage_instance():
|
| 53 |
-
|
| 54 |
-
full_url = f"http://{clean_link}"
|
| 55 |
-
instance_id = os.getenv("OS_INSTANCE_ID")
|
| 56 |
try:
|
| 57 |
conn = get_conn()
|
|
|
|
| 58 |
server = conn.compute.find_server(instance_id)
|
|
|
|
| 59 |
if not server:
|
| 60 |
yield ("β Error: Instance Not Found", gr.update(visible=False))
|
| 61 |
return
|
| 62 |
|
| 63 |
-
if server.status =
|
| 64 |
-
yield ("βΉοΈ Already ACTIVE. Checking link...", gr.update(visible=False))
|
| 65 |
-
else:
|
| 66 |
yield ("β³ Milestone 1/3: Un-shelving...", gr.update(visible=False))
|
| 67 |
if server.status == "SHELVED_OFFLOADED":
|
| 68 |
conn.compute.unshelve_server(server)
|
| 69 |
conn.compute.wait_for_server(server, status='ACTIVE', wait=600)
|
| 70 |
yield ("β
Milestone 2/3: VM is ACTIVE.", gr.update(visible=False))
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
for i in range(24):
|
| 74 |
try:
|
| 75 |
if requests.get(full_url, timeout=3).status_code < 500:
|
|
|
|
| 76 |
yield ("π Success! ComFit is ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True))
|
| 77 |
return
|
| 78 |
except: pass
|
|
@@ -81,13 +101,12 @@ def manage_instance():
|
|
| 81 |
except Exception as e:
|
| 82 |
yield (f"β Error: {str(e)}", gr.update(visible=False))
|
| 83 |
|
| 84 |
-
#
|
| 85 |
combined_js = """
|
| 86 |
<script>
|
| 87 |
let idleTime = 0;
|
| 88 |
-
const MAX_IDLE = 10; //
|
| 89 |
|
| 90 |
-
// Sound function
|
| 91 |
function playNotification(status) {
|
| 92 |
if (status.includes("RUNNING")) {
|
| 93 |
var audio = new Audio('https://cdn.freesound.org');
|
|
@@ -95,29 +114,24 @@ function playNotification(status) {
|
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
-
// Inactivity Monitor
|
| 99 |
function resetTimer() { idleTime = 0; }
|
| 100 |
-
window.onload = resetTimer;
|
| 101 |
window.onmousemove = resetTimer;
|
| 102 |
window.onmousedown = resetTimer;
|
| 103 |
-
window.ontouchstart = resetTimer;
|
| 104 |
window.onclick = resetTimer;
|
| 105 |
window.onkeypress = resetTimer;
|
| 106 |
|
| 107 |
setInterval(function() {
|
| 108 |
idleTime++;
|
| 109 |
if (idleTime >= MAX_IDLE) {
|
| 110 |
-
alert("Session Expired due to
|
| 111 |
-
window.location.reload();
|
| 112 |
}
|
| 113 |
-
}, 60000);
|
| 114 |
</script>
|
| 115 |
"""
|
| 116 |
|
| 117 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 118 |
gr.HTML(combined_js)
|
| 119 |
-
|
| 120 |
-
# Session state for cleanup
|
| 121 |
session_tracker = gr.State(value="active", delete_callback=user_left)
|
| 122 |
|
| 123 |
gr.Markdown("# π ComFit Copilot Shared Control Panel")
|
|
@@ -133,11 +147,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 133 |
live_status.change(None, inputs=live_status, js="(s) => playNotification(s)")
|
| 134 |
activate_btn.click(fn=manage_instance, outputs=[status_label, link_box])
|
| 135 |
|
|
|
|
| 136 |
timer = gr.Timer(5)
|
| 137 |
-
timer.tick(fn=get_current_status, outputs=[live_status, user_display])
|
| 138 |
|
| 139 |
-
|
| 140 |
-
demo.load(fn=
|
|
|
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
demo.launch()
|
|
|
|
| 5 |
import openstack
|
| 6 |
from openstack import exceptions
|
| 7 |
|
| 8 |
+
# GLOBAL SHARED STATE
|
| 9 |
active_user_count = 0
|
| 10 |
+
is_comfit_alive = False # Track if software is reachable across all sessions
|
| 11 |
+
clean_link = "comfit-copilot.mch250095.projects.jetstream-cloud.org:3000"
|
| 12 |
+
full_url = f"http://{clean_link}"
|
| 13 |
|
| 14 |
def get_conn():
|
| 15 |
return openstack.connect(
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
def get_current_status():
|
| 25 |
+
global active_user_count, is_comfit_alive
|
| 26 |
try:
|
| 27 |
conn = get_conn()
|
| 28 |
instance_id = os.getenv("OS_INSTANCE_ID")
|
|
|
|
| 39 |
}
|
| 40 |
status_text = status_map.get(server.status, f"β STATE: {server.status}")
|
| 41 |
|
| 42 |
+
# Background health check to update the global 'is_comfit_alive' flag
|
| 43 |
+
if "RUNNING" in status_text:
|
| 44 |
+
try:
|
| 45 |
+
if requests.get(full_url, timeout=2).status_code < 500:
|
| 46 |
+
is_comfit_alive = True
|
| 47 |
+
else:
|
| 48 |
+
is_comfit_alive = False
|
| 49 |
+
except:
|
| 50 |
+
is_comfit_alive = False
|
| 51 |
+
else:
|
| 52 |
+
is_comfit_alive = False
|
| 53 |
+
|
| 54 |
+
# Determine link visibility for the live timer update
|
| 55 |
+
link_update = gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=is_comfit_alive)
|
| 56 |
+
|
| 57 |
+
return status_text, f"π₯ Active Users: {active_user_count}", link_update
|
| 58 |
except Exception as e:
|
| 59 |
+
return f"β Error: {str(e)}", f"π₯ Active Users: {active_user_count}", gr.update()
|
| 60 |
|
| 61 |
def user_joined():
|
| 62 |
+
global active_user_count, is_comfit_alive
|
| 63 |
active_user_count += 1
|
| 64 |
+
# If already alive, show the link immediately on page load
|
| 65 |
+
link_visibility = gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=is_comfit_alive)
|
| 66 |
+
return f"π₯ Active Users: {active_user_count}", link_visibility
|
| 67 |
|
| 68 |
def user_left():
|
| 69 |
global active_user_count
|
| 70 |
active_user_count = max(0, active_user_count - 1)
|
| 71 |
|
| 72 |
def manage_instance():
|
| 73 |
+
global is_comfit_alive
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
conn = get_conn()
|
| 76 |
+
instance_id = os.getenv("OS_INSTANCE_ID")
|
| 77 |
server = conn.compute.find_server(instance_id)
|
| 78 |
+
|
| 79 |
if not server:
|
| 80 |
yield ("β Error: Instance Not Found", gr.update(visible=False))
|
| 81 |
return
|
| 82 |
|
| 83 |
+
if server.status != "ACTIVE":
|
|
|
|
|
|
|
| 84 |
yield ("β³ Milestone 1/3: Un-shelving...", gr.update(visible=False))
|
| 85 |
if server.status == "SHELVED_OFFLOADED":
|
| 86 |
conn.compute.unshelve_server(server)
|
| 87 |
conn.compute.wait_for_server(server, status='ACTIVE', wait=600)
|
| 88 |
yield ("β
Milestone 2/3: VM is ACTIVE.", gr.update(visible=False))
|
| 89 |
|
| 90 |
+
# Check software
|
| 91 |
+
yield ("π Milestone 3/3: Connecting to ComFit...", gr.update(visible=False))
|
| 92 |
for i in range(24):
|
| 93 |
try:
|
| 94 |
if requests.get(full_url, timeout=3).status_code < 500:
|
| 95 |
+
is_comfit_alive = True
|
| 96 |
yield ("π Success! ComFit is ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True))
|
| 97 |
return
|
| 98 |
except: pass
|
|
|
|
| 101 |
except Exception as e:
|
| 102 |
yield (f"β Error: {str(e)}", gr.update(visible=False))
|
| 103 |
|
| 104 |
+
# Combined JS for Sound + Inactivity Timeout (Change MAX_IDLE here)
|
| 105 |
combined_js = """
|
| 106 |
<script>
|
| 107 |
let idleTime = 0;
|
| 108 |
+
const MAX_IDLE = 10; // <--- CHANGE TIMEOUT HERE (in minutes)
|
| 109 |
|
|
|
|
| 110 |
function playNotification(status) {
|
| 111 |
if (status.includes("RUNNING")) {
|
| 112 |
var audio = new Audio('https://cdn.freesound.org');
|
|
|
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
|
|
|
| 117 |
function resetTimer() { idleTime = 0; }
|
|
|
|
| 118 |
window.onmousemove = resetTimer;
|
| 119 |
window.onmousedown = resetTimer;
|
|
|
|
| 120 |
window.onclick = resetTimer;
|
| 121 |
window.onkeypress = resetTimer;
|
| 122 |
|
| 123 |
setInterval(function() {
|
| 124 |
idleTime++;
|
| 125 |
if (idleTime >= MAX_IDLE) {
|
| 126 |
+
alert("Session Expired due to inactivity. Please refresh.");
|
| 127 |
+
window.location.reload();
|
| 128 |
}
|
| 129 |
+
}, 60000);
|
| 130 |
</script>
|
| 131 |
"""
|
| 132 |
|
| 133 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 134 |
gr.HTML(combined_js)
|
|
|
|
|
|
|
| 135 |
session_tracker = gr.State(value="active", delete_callback=user_left)
|
| 136 |
|
| 137 |
gr.Markdown("# π ComFit Copilot Shared Control Panel")
|
|
|
|
| 147 |
live_status.change(None, inputs=live_status, js="(s) => playNotification(s)")
|
| 148 |
activate_btn.click(fn=manage_instance, outputs=[status_label, link_box])
|
| 149 |
|
| 150 |
+
# Timer refreshes status, user count, and global link visibility every 5s
|
| 151 |
timer = gr.Timer(5)
|
| 152 |
+
timer.tick(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 153 |
|
| 154 |
+
# Load initial shared state for new users
|
| 155 |
+
demo.load(fn=user_joined, outputs=[user_display, link_box])
|
| 156 |
+
demo.load(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
demo.launch()
|