Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def manage_instance(action, confirmed=False):
|
| 2 |
clean_link = "chat.mch250095.projects.jetstream-cloud.org"
|
| 3 |
full_url = f"https://{clean_link}/"
|
|
@@ -6,8 +41,8 @@ def manage_instance(action, confirmed=False):
|
|
| 6 |
try:
|
| 7 |
conn = get_conn()
|
| 8 |
server = conn.compute.find_server(instance_id)
|
|
|
|
| 9 |
if not server:
|
| 10 |
-
# Yielding 2 values: [Label, Markdown]
|
| 11 |
yield "β Error: Instance ID not found.", gr.update(visible=False)
|
| 12 |
return
|
| 13 |
|
|
@@ -21,19 +56,22 @@ def manage_instance(action, confirmed=False):
|
|
| 21 |
yield "β
Milestone 2/3: Unshelved! VM is now ACTIVE.", gr.update(visible=False)
|
| 22 |
|
| 23 |
yield "π Milestone 3/3: Waiting for software...", gr.update(visible=False)
|
|
|
|
| 24 |
# Poll for the web service to boot (~30s)
|
| 25 |
-
for
|
| 26 |
try:
|
| 27 |
-
|
|
|
|
| 28 |
yield "π Success! Ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True)
|
| 29 |
return
|
| 30 |
except:
|
| 31 |
pass
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
time.sleep(5)
|
| 35 |
|
| 36 |
-
yield "β οΈ Service slow.", gr.update(value=f"### [
|
| 37 |
|
| 38 |
elif action == "shelve":
|
| 39 |
yield "π€ Shelving Begin... (Saving and Stopping)", gr.update(visible=False)
|
|
@@ -42,5 +80,36 @@ def manage_instance(action, confirmed=False):
|
|
| 42 |
yield "π Instance is now SHELVED.", gr.update(visible=False)
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import openstack
|
| 6 |
+
from openstack import exceptions
|
| 7 |
+
|
| 8 |
+
def get_conn():
|
| 9 |
+
return openstack.connect(
|
| 10 |
+
auth_url=os.getenv("OS_AUTH_URL"),
|
| 11 |
+
application_credential_id=os.getenv("OS_APPLICATION_CREDENTIAL_ID"),
|
| 12 |
+
application_credential_secret=os.getenv("OS_APPLICATION_CREDENTIAL_SECRET"),
|
| 13 |
+
auth_type="v3applicationcredential",
|
| 14 |
+
region_name="IU",
|
| 15 |
+
interface="public"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def get_current_status():
|
| 19 |
+
try:
|
| 20 |
+
conn = get_conn()
|
| 21 |
+
instance_id = os.getenv("OS_INSTANCE_ID")
|
| 22 |
+
server = conn.compute.find_server(instance_id)
|
| 23 |
+
if server:
|
| 24 |
+
status_map = {
|
| 25 |
+
"ACTIVE": "π’ RUNNING (ACTIVE)",
|
| 26 |
+
"SHELVED_OFFLOADED": "π€ SLEEPING (SHELVED)",
|
| 27 |
+
"SHUTOFF": "π΄ POWERED OFF",
|
| 28 |
+
"BUILD": "π οΈ STARTING...",
|
| 29 |
+
"SHELVING": "β³ SAVING STATE..."
|
| 30 |
+
}
|
| 31 |
+
return status_map.get(server.status, f"β STATE: {server.status}")
|
| 32 |
+
return "β Error: Instance Not Found"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"β Connection Error: {str(e)}"
|
| 35 |
+
|
| 36 |
def manage_instance(action, confirmed=False):
|
| 37 |
clean_link = "chat.mch250095.projects.jetstream-cloud.org"
|
| 38 |
full_url = f"https://{clean_link}/"
|
|
|
|
| 41 |
try:
|
| 42 |
conn = get_conn()
|
| 43 |
server = conn.compute.find_server(instance_id)
|
| 44 |
+
|
| 45 |
if not server:
|
|
|
|
| 46 |
yield "β Error: Instance ID not found.", gr.update(visible=False)
|
| 47 |
return
|
| 48 |
|
|
|
|
| 56 |
yield "β
Milestone 2/3: Unshelved! VM is now ACTIVE.", gr.update(visible=False)
|
| 57 |
|
| 58 |
yield "π Milestone 3/3: Waiting for software...", gr.update(visible=False)
|
| 59 |
+
|
| 60 |
# Poll for the web service to boot (~30s)
|
| 61 |
+
for i in range(24):
|
| 62 |
try:
|
| 63 |
+
response = requests.get(full_url, timeout=3)
|
| 64 |
+
if response.status_code < 400:
|
| 65 |
yield "π Success! Ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True)
|
| 66 |
return
|
| 67 |
except:
|
| 68 |
pass
|
| 69 |
+
|
| 70 |
+
# IMPORTANT: Yield 2 values every loop to keep Gradio happy
|
| 71 |
+
yield f"π Milestone 3/3: Software booting (Attempt {i+1}/24)...", gr.update(visible=False)
|
| 72 |
time.sleep(5)
|
| 73 |
|
| 74 |
+
yield "β οΈ Service slow.", gr.update(value=f"### [Manual Link: {clean_link}]({full_url})", visible=True)
|
| 75 |
|
| 76 |
elif action == "shelve":
|
| 77 |
yield "π€ Shelving Begin... (Saving and Stopping)", gr.update(visible=False)
|
|
|
|
| 80 |
yield "π Instance is now SHELVED.", gr.update(visible=False)
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
+
yield f"β Error: {str(e)}", gr.update(visible=False)
|
| 84 |
+
|
| 85 |
+
# UI Layout
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("# π ComFit Copilot Control Panel")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
live_status = gr.Textbox(label="Live Jetstream2 Status", value="Checking...", interactive=False)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
activate_btn = gr.Button("π Activate (Unshelve)", variant="primary")
|
| 94 |
+
|
| 95 |
+
with gr.Column():
|
| 96 |
+
confirm_check = gr.Checkbox(label="Confirm Shelve", value=False)
|
| 97 |
+
shelve_btn = gr.Button("π€ Shelve (Stop)", variant="stop", interactive=False)
|
| 98 |
+
|
| 99 |
+
status_label = gr.Label(value="Ready")
|
| 100 |
+
link_box = gr.Markdown(visible=False)
|
| 101 |
+
|
| 102 |
+
# UI Logic
|
| 103 |
+
confirm_check.change(fn=lambda x: gr.update(interactive=x), inputs=confirm_check, outputs=shelve_btn)
|
| 104 |
+
|
| 105 |
+
activate_btn.click(fn=lambda: manage_instance("activate"), outputs=[status_label, link_box])
|
| 106 |
+
shelve_btn.click(fn=lambda: manage_instance("shelve", True), outputs=[status_label, link_box])
|
| 107 |
+
|
| 108 |
+
# Timer for status refresh (Gradio 5/6 style)
|
| 109 |
+
timer = gr.Timer(30)
|
| 110 |
+
timer.tick(fn=get_current_status, outputs=live_status)
|
| 111 |
+
demo.load(fn=get_current_status, outputs=live_status)
|
| 112 |
+
|
| 113 |
+
# Start the app and apply the theme
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
demo.launch(theme=gr.themes.Soft())
|