Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,78 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import openstack
|
| 4 |
|
| 5 |
-
# 1. Connect to OpenStack using HF Secrets
|
| 6 |
def get_conn():
|
| 7 |
return openstack.connect(
|
| 8 |
auth_url=os.getenv("OS_AUTH_URL"),
|
| 9 |
application_credential_id=os.getenv("OS_APPLICATION_CREDENTIAL_ID"),
|
| 10 |
application_credential_secret=os.getenv("OS_APPLICATION_CREDENTIAL_SECRET"),
|
| 11 |
-
|
| 12 |
-
auth_type="v3applicationcredential",
|
| 13 |
region_name="IU",
|
| 14 |
interface="public"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
conn = get_conn()
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
if
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
except Exception as e:
|
| 42 |
-
|
| 43 |
|
| 44 |
-
#
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
gr.Markdown("# ComFit Copilot Controller")
|
| 47 |
-
gr.Markdown("Click the button below to unshelve the instance on Jetstream2.")
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
import gradio as gr
|
| 5 |
import openstack
|
| 6 |
|
|
|
|
| 7 |
def get_conn():
|
| 8 |
return openstack.connect(
|
| 9 |
auth_url=os.getenv("OS_AUTH_URL"),
|
| 10 |
application_credential_id=os.getenv("OS_APPLICATION_CREDENTIAL_ID"),
|
| 11 |
application_credential_secret=os.getenv("OS_APPLICATION_CREDENTIAL_SECRET"),
|
| 12 |
+
auth_type="v3applicationcredential",
|
|
|
|
| 13 |
region_name="IU",
|
| 14 |
interface="public"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
def activate_and_monitor():
|
| 18 |
+
target_url = "https://chat.mch250095.projects.jetstream-cloud.org/"
|
| 19 |
+
instance_id = os.getenv("OS_INSTANCE_ID")
|
| 20 |
+
|
| 21 |
try:
|
| 22 |
conn = get_conn()
|
| 23 |
+
server = conn.compute.find_server(instance_id)
|
| 24 |
|
| 25 |
+
if not server:
|
| 26 |
+
yield "❌ Error: Instance not found.", gr.update(visible=False)
|
| 27 |
+
return
|
| 28 |
+
|
| 29 |
+
# Milestone 1: Un-shelving Begin
|
| 30 |
+
yield "⏳ Milestone 1/3: Un-shelving Begin...", gr.update(visible=False)
|
| 31 |
|
| 32 |
+
if server.status == "SHELVED_OFFLOADED":
|
| 33 |
+
conn.compute.unshelve_server(server)
|
| 34 |
+
elif server.status == "ACTIVE":
|
| 35 |
+
pass # Skip to next milestone
|
| 36 |
+
else:
|
| 37 |
+
yield f"⚠️ Instance is in {server.status} state. Attempting to proceed...", gr.update(visible=False)
|
| 38 |
|
| 39 |
+
# Milestone 2: Unshelved (Wait for ACTIVE status)
|
| 40 |
+
# wait_for_server waits until the status is 'ACTIVE'
|
| 41 |
+
conn.compute.wait_for_server(server, status='ACTIVE', wait=120)
|
| 42 |
+
yield "✅ Milestone 2/3: Unshelved (Instance is ACTIVE).", gr.update(visible=False)
|
| 43 |
+
|
| 44 |
+
# Milestone 3: URL Check (Wait for web service to respond)
|
| 45 |
+
yield "🔄 Milestone 3/3: Waiting for software to start at URL...", gr.update(visible=False)
|
| 46 |
|
| 47 |
+
# Poll the URL for up to 2 minutes
|
| 48 |
+
for _ in range(24):
|
| 49 |
+
try:
|
| 50 |
+
response = requests.get(target_url, timeout=5)
|
| 51 |
+
if response.status_code < 400:
|
| 52 |
+
yield "🎉 Success! ComFit Copilot is alive.", gr.update(value=f"Visit: {target_url}", visible=True)
|
| 53 |
+
return
|
| 54 |
+
except:
|
| 55 |
+
pass
|
| 56 |
+
time.sleep(5)
|
| 57 |
|
| 58 |
+
yield "⚠️ Instance is up, but the web link is not responding yet. Please try the link manually.", gr.update(value=f"Visit: {target_url}", visible=True)
|
| 59 |
+
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
+
yield f"❌ Error: {str(e)}", gr.update(visible=False)
|
| 62 |
|
| 63 |
+
# UI Layout
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown("# ComFit Copilot Controller")
|
|
|
|
| 66 |
|
| 67 |
+
with gr.Column():
|
| 68 |
+
activate_btn = gr.Button("🚀 Activate ComFit Copilot", variant="primary")
|
| 69 |
+
status_text = gr.Label(value="Ready")
|
| 70 |
+
# Hidden link component that appears when ready
|
| 71 |
+
link_output = gr.Markdown(visible=False)
|
| 72 |
+
|
| 73 |
+
activate_btn.click(
|
| 74 |
+
fn=activate_and_monitor,
|
| 75 |
+
outputs=[status_text, link_output]
|
| 76 |
+
)
|
| 77 |
|
| 78 |
+
demo.launch()
|