JatinkInnovision commited on
Commit
bfa4d7e
Β·
verified Β·
1 Parent(s): 3aed4a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -8
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 _ in range(24):
26
  try:
27
- if requests.get(full_url, timeout=3).status_code < 400:
 
28
  yield "πŸŽ‰ Success! Ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True)
29
  return
30
  except:
31
  pass
32
- # Must yield 2 values even inside a loop to keep UI responsive
33
- yield "πŸ”„ Milestone 3/3: Software still booting...", gr.update(visible=False)
 
34
  time.sleep(5)
35
 
36
- yield "⚠️ Service slow.", gr.update(value=f"### [Try Manual Link: {clean_link}]({full_url})", visible=True)
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
- # Error handling also requires 2 return values
46
- yield f"❌ Error: {str(e)}", gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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())