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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -79
app.py CHANGED
@@ -1,38 +1,3 @@
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}/"
@@ -42,7 +7,7 @@ def manage_instance(action, confirmed=False):
42
  conn = get_conn()
43
  server = conn.compute.find_server(instance_id)
44
  if not server:
45
- # Yielding 2 values to match [status_label, link_box]
46
  yield "❌ Error: Instance ID not found.", gr.update(visible=False)
47
  return
48
 
@@ -51,63 +16,31 @@ def manage_instance(action, confirmed=False):
51
  if server.status == "SHELVED_OFFLOADED":
52
  conn.compute.unshelve_server(server)
53
 
 
54
  conn.compute.wait_for_server(server, status='ACTIVE', wait=180)
55
  yield "βœ… Milestone 2/3: Unshelved! VM is now ACTIVE.", gr.update(visible=False)
56
 
57
  yield "πŸ”„ Milestone 3/3: Waiting for software...", gr.update(visible=False)
 
58
  for _ in range(24):
59
  try:
60
  if requests.get(full_url, timeout=3).status_code < 400:
61
  yield "πŸŽ‰ Success! Ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True)
62
  return
63
- except: pass
 
 
 
64
  time.sleep(5)
 
65
  yield "⚠️ Service slow.", gr.update(value=f"### [Try Manual Link: {clean_link}]({full_url})", visible=True)
66
 
67
  elif action == "shelve":
68
- # FIX: Always yield TWO values to satisfy Gradio's output requirement
69
- yield "⏳ Shelving Begin... (Saving and Stopping)", gr.update(visible=False)
70
-
71
  conn.compute.shelve_server(server)
72
-
73
- # Use SDK to wait for state change
74
  conn.compute.wait_for_server(server, status='SHELVED_OFFLOADED', wait=180)
75
-
76
- yield "πŸ’€ Instance is now SHELVED.", gr.update(visible=False)
77
 
78
  except Exception as e:
79
- # FIX: Always yield TWO values on error
80
- yield f"❌ Error: {str(e)}", gr.update(visible=False)
81
-
82
- # UI Layout - Theme moved to launch() later to avoid UserWarning
83
- with gr.Blocks() as demo:
84
- gr.Markdown("# πŸš€ ComFit Copilot Control Panel")
85
-
86
- with gr.Row():
87
- # Textbox for live status
88
- live_status = gr.Textbox(label="Live Jetstream2 Status", value="Checking...", interactive=False)
89
-
90
- with gr.Row():
91
- activate_btn = gr.Button("πŸš€ Activate (Unshelve)", variant="primary")
92
-
93
- with gr.Column():
94
- confirm_check = gr.Checkbox(label="I am done working and want to Shelve", value=False)
95
- shelve_btn = gr.Button("πŸ’€ Shelve (Save & Stop)", variant="stop", interactive=False)
96
-
97
- status_label = gr.Label(value="Ready")
98
- link_box = gr.Markdown(visible=False)
99
-
100
- # Enable Shelve button only if checkbox is ticked
101
- confirm_check.change(fn=lambda x: gr.update(interactive=x), inputs=confirm_check, outputs=shelve_btn)
102
-
103
- activate_btn.click(fn=lambda: manage_instance("activate"), outputs=[status_label, link_box])
104
- shelve_btn.click(fn=lambda: manage_instance("shelve", True), outputs=[status_label, link_box])
105
-
106
- # NEW GRADIO 5/6 WAY: Use a Timer component for periodic updates
107
- timer = gr.Timer(30)
108
- timer.tick(fn=get_current_status, outputs=live_status)
109
- # Also run once on load
110
- demo.load(fn=get_current_status, outputs=live_status)
111
-
112
- # Pass theme to launch() as requested by the warning
113
- demo.launch(theme=gr.themes.Soft())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def manage_instance(action, confirmed=False):
2
  clean_link = "chat.mch250095.projects.jetstream-cloud.org"
3
  full_url = f"https://{clean_link}/"
 
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
 
 
16
  if server.status == "SHELVED_OFFLOADED":
17
  conn.compute.unshelve_server(server)
18
 
19
+ # Wait for VM to reach ACTIVE state
20
  conn.compute.wait_for_server(server, status='ACTIVE', wait=180)
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)
 
 
40
  conn.compute.shelve_server(server)
 
 
41
  conn.compute.wait_for_server(server, status='SHELVED_OFFLOADED', wait=180)
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)