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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -43,46 +43,40 @@ def manage_instance(action, confirmed=False):
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
 
49
  if action == "activate":
50
- yield "⏳ Milestone 1/3: Un-shelving Begin...", gr.update(visible=False)
51
  if server.status == "SHELVED_OFFLOADED":
52
  conn.compute.unshelve_server(server)
53
 
54
- # Wait for VM to reach ACTIVE state
55
  conn.compute.wait_for_server(server, status='ACTIVE', wait=180)
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)
78
  conn.compute.shelve_server(server)
79
  conn.compute.wait_for_server(server, status='SHELVED_OFFLOADED', wait=180)
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
 
@@ -91,7 +85,6 @@ with gr.Blocks() as demo:
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)
@@ -99,17 +92,24 @@ with gr.Blocks() as demo:
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())
 
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
 
49
  if action == "activate":
50
+ yield ("⏳ Milestone 1/3: Un-shelving Begin...", gr.update(visible=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 i in range(24):
59
  try:
60
  response = requests.get(full_url, timeout=3)
61
  if response.status_code < 400:
62
+ yield ("πŸŽ‰ Success! Ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True))
63
  return
64
  except:
65
  pass
66
+ yield (f"πŸ”„ Milestone 3/3: Software booting (Attempt {i+1}/24)...", gr.update(visible=False))
 
 
67
  time.sleep(5)
68
 
69
+ yield ("⚠️ Service slow.", gr.update(value=f"### [Manual Link: {clean_link}]({full_url})", visible=True))
70
 
71
  elif action == "shelve":
72
+ yield ("πŸ’€ Shelving Begin... (Saving and Stopping)", gr.update(visible=False))
73
  conn.compute.shelve_server(server)
74
  conn.compute.wait_for_server(server, status='SHELVED_OFFLOADED', wait=180)
75
+ yield ("πŸ›Œ Instance is now SHELVED.", gr.update(visible=False))
76
 
77
  except Exception as e:
78
+ yield (f"❌ Error: {str(e)}", gr.update(visible=False))
79
 
 
80
  with gr.Blocks() as demo:
81
  gr.Markdown("# πŸš€ ComFit Copilot Control Panel")
82
 
 
85
 
86
  with gr.Row():
87
  activate_btn = gr.Button("πŸš€ Activate (Unshelve)", variant="primary")
 
88
  with gr.Column():
89
  confirm_check = gr.Checkbox(label="Confirm Shelve", value=False)
90
  shelve_btn = gr.Button("πŸ’€ Shelve (Stop)", variant="stop", interactive=False)
 
92
  status_label = gr.Label(value="Ready")
93
  link_box = gr.Markdown(visible=False)
94
 
 
95
  confirm_check.change(fn=lambda x: gr.update(interactive=x), inputs=confirm_check, outputs=shelve_btn)
96
 
97
+ # Added concurrency_limit to support the generator (yield) logic
98
+ activate_btn.click(
99
+ fn=lambda: manage_instance("activate"),
100
+ outputs=[status_label, link_box],
101
+ concurrency_limit=5
102
+ )
103
+
104
+ shelve_btn.click(
105
+ fn=lambda: manage_instance("shelve", True),
106
+ outputs=[status_label, link_box],
107
+ concurrency_limit=5
108
+ )
109
 
 
110
  timer = gr.Timer(30)
111
  timer.tick(fn=get_current_status, outputs=live_status)
112
  demo.load(fn=get_current_status, outputs=live_status)
113
 
 
114
  if __name__ == "__main__":
115
  demo.launch(theme=gr.themes.Soft())