Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,32 @@ is_comfit_alive = False
|
|
| 11 |
clean_link = "comfit-copilot.mch250095.projects.jetstream-cloud.org:3000"
|
| 12 |
full_url = f"http://{clean_link}"
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def get_conn():
|
| 15 |
-
"""Authenticates with Jetstream2 (OpenStack) using Application Credentials."""
|
| 16 |
return openstack.connect(
|
| 17 |
auth_url=os.getenv("OS_AUTH_URL"),
|
| 18 |
application_credential_id=os.getenv("OS_APPLICATION_CREDENTIAL_ID"),
|
|
@@ -23,7 +47,6 @@ def get_conn():
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def get_current_status():
|
| 26 |
-
"""Background task to fetch status and sync shared link visibility."""
|
| 27 |
global active_user_count, is_comfit_alive
|
| 28 |
try:
|
| 29 |
conn = get_conn()
|
|
@@ -41,7 +64,6 @@ def get_current_status():
|
|
| 41 |
}
|
| 42 |
status_text = status_map.get(server.status, f"β STATE: {server.status}")
|
| 43 |
|
| 44 |
-
# Shared health check for the URL
|
| 45 |
if "RUNNING" in status_text:
|
| 46 |
try:
|
| 47 |
if requests.get(full_url, timeout=2).status_code < 500:
|
|
@@ -53,7 +75,7 @@ def get_current_status():
|
|
| 53 |
else:
|
| 54 |
is_comfit_alive = False
|
| 55 |
|
| 56 |
-
link_update = gr.update(value=
|
| 57 |
return status_text, f"π₯ Active Users: {active_user_count}", link_update
|
| 58 |
except Exception as e:
|
| 59 |
return f"β Error: {str(e)}", f"π₯ Active Users: {active_user_count}", gr.update()
|
|
@@ -78,7 +100,6 @@ def manage_instance():
|
|
| 78 |
yield ("β Error: Instance Not Found", gr.update(visible=False))
|
| 79 |
return
|
| 80 |
|
| 81 |
-
# Multi-user bypass
|
| 82 |
if server.status != "ACTIVE":
|
| 83 |
yield ("β³ Milestone 1/3: Un-shelving Begin...", gr.update(visible=False))
|
| 84 |
if server.status == "SHELVED_OFFLOADED":
|
|
@@ -91,7 +112,7 @@ def manage_instance():
|
|
| 91 |
try:
|
| 92 |
if requests.get(full_url, timeout=3).status_code < 500:
|
| 93 |
is_comfit_alive = True
|
| 94 |
-
yield ("π Success! ComFit is ready.", gr.update(value=
|
| 95 |
return
|
| 96 |
except: pass
|
| 97 |
yield (f"π Milestone 3/3: Software booting ({i+1}/24)...", gr.update(visible=False))
|
|
@@ -99,15 +120,14 @@ def manage_instance():
|
|
| 99 |
except Exception as e:
|
| 100 |
yield (f"β Error: {str(e)}", gr.update(visible=False))
|
| 101 |
|
| 102 |
-
# JavaScript for sound + Inactivity Timeout
|
| 103 |
combined_js = """
|
| 104 |
<script>
|
| 105 |
let idleTime = 0;
|
| 106 |
-
const MAX_IDLE = 10;
|
| 107 |
function playNotification(status) {
|
| 108 |
if (status.includes("RUNNING")) {
|
| 109 |
var audio = new Audio('https://cdn.freesound.org');
|
| 110 |
-
audio.play().catch(e => console.log("Audio play blocked.
|
| 111 |
}
|
| 112 |
}
|
| 113 |
function resetTimer() { idleTime = 0; }
|
|
@@ -125,11 +145,8 @@ setInterval(function() {
|
|
| 125 |
</script>
|
| 126 |
"""
|
| 127 |
|
| 128 |
-
# Fixed: Theme removed from constructor for Gradio 6 compatibility
|
| 129 |
with gr.Blocks() as demo:
|
| 130 |
gr.HTML(combined_js)
|
| 131 |
-
|
| 132 |
-
# Session state for cleanup
|
| 133 |
session_tracker = gr.State(value="active", delete_callback=user_left)
|
| 134 |
|
| 135 |
gr.Markdown("# ComFit Copilot (Comfort & Fit Copilot) By Innovision")
|
|
@@ -143,9 +160,10 @@ with gr.Blocks() as demo:
|
|
| 143 |
refresh_btn = gr.Button("π Force Refresh Status", variant="secondary")
|
| 144 |
|
| 145 |
status_label = gr.Label(value="Ready")
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
# Instructions section
|
| 149 |
gr.Markdown("---")
|
| 150 |
gr.Markdown("""
|
| 151 |
### π Instructions for Visitors
|
|
@@ -156,7 +174,6 @@ with gr.Blocks() as demo:
|
|
| 156 |
**Step 2: Access or Wake Up**
|
| 157 |
- If a **Visit Link** is already visible (at top of these instructions), simply click it to open ComFit Copilot.
|
| 158 |
- If no link is visible, click **Activate / Access ComFit Copilot**. This will trigger the un-shelving process, which may take up to 2+ mins.
|
| 159 |
-
<br><br>
|
| 160 |
|
| 161 |
**Note A: Track Milestones**
|
| 162 |
The control panel will cycle through three milestones: Un-shelving, VM Activation, and Software Booting. Please be patient while the cloud prepares your environment.
|
|
@@ -165,31 +182,19 @@ with gr.Blocks() as demo:
|
|
| 165 |
If you do not interact with this page for **10 minutes**, your session will expire.
|
| 166 |
|
| 167 |
**Note C: What to do after clicking on Visit Link**
|
| 168 |
-
|
|
|
|
| 169 |
""")
|
| 170 |
|
| 171 |
-
# UI Event Listeners
|
| 172 |
live_status.change(None, inputs=live_status, js="(s) => playNotification(s)")
|
| 173 |
activate_btn.click(fn=manage_instance, outputs=[status_label, link_box])
|
| 174 |
refresh_btn.click(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 175 |
|
| 176 |
-
# Automatic 5-second timer
|
| 177 |
timer = gr.Timer(5)
|
| 178 |
timer.tick(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 179 |
|
| 180 |
-
# Initialization
|
| 181 |
demo.load(fn=user_joined, outputs=user_display)
|
| 182 |
demo.load(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 183 |
|
| 184 |
-
# Logo Footer Sectio
|
| 185 |
-
gr.Markdown("---")
|
| 186 |
-
gr.Markdown("<center><h3>Supported By</h3></center>")
|
| 187 |
-
with gr.Row():
|
| 188 |
-
# Fixed: Removed 'show_download_button' which caused the crash
|
| 189 |
-
gr.Image("logo1.png", show_label=False, interactive=False, container=False, scale=1)
|
| 190 |
-
gr.Image("logo2.png", show_label=False, interactive=False, container=False, scale=1)
|
| 191 |
-
gr.Image("logo3.png", show_label=False, interactive=False, container=False, scale=1)
|
| 192 |
-
|
| 193 |
if __name__ == "__main__":
|
| 194 |
-
|
| 195 |
-
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 11 |
clean_link = "comfit-copilot.mch250095.projects.jetstream-cloud.org:3000"
|
| 12 |
full_url = f"http://{clean_link}"
|
| 13 |
|
| 14 |
+
# Styled HTML for the American Flag themed button
|
| 15 |
+
AMERICAN_FLAG_LINK = f"""
|
| 16 |
+
<div style="text-align: center; margin: 20px 0;">
|
| 17 |
+
<a href="{full_url}" target="_blank" style="
|
| 18 |
+
display: inline-block;
|
| 19 |
+
padding: 15px 30px;
|
| 20 |
+
font-size: 24px;
|
| 21 |
+
font-weight: bold;
|
| 22 |
+
text-decoration: none;
|
| 23 |
+
color: white;
|
| 24 |
+
background: linear-gradient(90deg, #B22234 33%, #FFFFFF 33%, #FFFFFF 66%, #3C3B6E 66%);
|
| 25 |
+
background-size: 300% 100%;
|
| 26 |
+
border: 4px solid #3C3B6E;
|
| 27 |
+
border-radius: 10px;
|
| 28 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
|
| 29 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.3);
|
| 30 |
+
transition: transform 0.2s;
|
| 31 |
+
" onmouseover="this.style.transform='scale(1.05)'" onmouseout="this.style.transform='scale(1)'">
|
| 32 |
+
<span style="color: #FFFFFF; background-color: #B22234; padding: 2px 5px; border-radius: 3px;">πΊπΈ</span>
|
| 33 |
+
<span style="color: #3C3B6E;">Click here for ComFit Copilot</span>
|
| 34 |
+
<span style="color: #FFFFFF; background-color: #3C3B6E; padding: 2px 5px; border-radius: 3px;">πΊπΈ</span>
|
| 35 |
+
</a>
|
| 36 |
+
</div>
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
def get_conn():
|
|
|
|
| 40 |
return openstack.connect(
|
| 41 |
auth_url=os.getenv("OS_AUTH_URL"),
|
| 42 |
application_credential_id=os.getenv("OS_APPLICATION_CREDENTIAL_ID"),
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
def get_current_status():
|
|
|
|
| 50 |
global active_user_count, is_comfit_alive
|
| 51 |
try:
|
| 52 |
conn = get_conn()
|
|
|
|
| 64 |
}
|
| 65 |
status_text = status_map.get(server.status, f"β STATE: {server.status}")
|
| 66 |
|
|
|
|
| 67 |
if "RUNNING" in status_text:
|
| 68 |
try:
|
| 69 |
if requests.get(full_url, timeout=2).status_code < 500:
|
|
|
|
| 75 |
else:
|
| 76 |
is_comfit_alive = False
|
| 77 |
|
| 78 |
+
link_update = gr.update(value=AMERICAN_FLAG_LINK if is_comfit_alive else "", visible=is_comfit_alive)
|
| 79 |
return status_text, f"π₯ Active Users: {active_user_count}", link_update
|
| 80 |
except Exception as e:
|
| 81 |
return f"β Error: {str(e)}", f"π₯ Active Users: {active_user_count}", gr.update()
|
|
|
|
| 100 |
yield ("β Error: Instance Not Found", gr.update(visible=False))
|
| 101 |
return
|
| 102 |
|
|
|
|
| 103 |
if server.status != "ACTIVE":
|
| 104 |
yield ("β³ Milestone 1/3: Un-shelving Begin...", gr.update(visible=False))
|
| 105 |
if server.status == "SHELVED_OFFLOADED":
|
|
|
|
| 112 |
try:
|
| 113 |
if requests.get(full_url, timeout=3).status_code < 500:
|
| 114 |
is_comfit_alive = True
|
| 115 |
+
yield ("π Success! ComFit is ready.", gr.update(value=AMERICAN_FLAG_LINK, visible=True))
|
| 116 |
return
|
| 117 |
except: pass
|
| 118 |
yield (f"π Milestone 3/3: Software booting ({i+1}/24)...", gr.update(visible=False))
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
yield (f"β Error: {str(e)}", gr.update(visible=False))
|
| 122 |
|
|
|
|
| 123 |
combined_js = """
|
| 124 |
<script>
|
| 125 |
let idleTime = 0;
|
| 126 |
+
const MAX_IDLE = 10;
|
| 127 |
function playNotification(status) {
|
| 128 |
if (status.includes("RUNNING")) {
|
| 129 |
var audio = new Audio('https://cdn.freesound.org');
|
| 130 |
+
audio.play().catch(e => console.log("Audio play blocked."));
|
| 131 |
}
|
| 132 |
}
|
| 133 |
function resetTimer() { idleTime = 0; }
|
|
|
|
| 145 |
</script>
|
| 146 |
"""
|
| 147 |
|
|
|
|
| 148 |
with gr.Blocks() as demo:
|
| 149 |
gr.HTML(combined_js)
|
|
|
|
|
|
|
| 150 |
session_tracker = gr.State(value="active", delete_callback=user_left)
|
| 151 |
|
| 152 |
gr.Markdown("# ComFit Copilot (Comfort & Fit Copilot) By Innovision")
|
|
|
|
| 160 |
refresh_btn = gr.Button("π Force Refresh Status", variant="secondary")
|
| 161 |
|
| 162 |
status_label = gr.Label(value="Ready")
|
| 163 |
+
|
| 164 |
+
# Logic Change: Use gr.HTML for the link instead of Markdown to hide the URL and center it
|
| 165 |
+
link_box = gr.HTML(visible=False)
|
| 166 |
|
|
|
|
| 167 |
gr.Markdown("---")
|
| 168 |
gr.Markdown("""
|
| 169 |
### π Instructions for Visitors
|
|
|
|
| 174 |
**Step 2: Access or Wake Up**
|
| 175 |
- If a **Visit Link** is already visible (at top of these instructions), simply click it to open ComFit Copilot.
|
| 176 |
- If no link is visible, click **Activate / Access ComFit Copilot**. This will trigger the un-shelving process, which may take up to 2+ mins.
|
|
|
|
| 177 |
|
| 178 |
**Note A: Track Milestones**
|
| 179 |
The control panel will cycle through three milestones: Un-shelving, VM Activation, and Software Booting. Please be patient while the cloud prepares your environment.
|
|
|
|
| 182 |
If you do not interact with this page for **10 minutes**, your session will expire.
|
| 183 |
|
| 184 |
**Note C: What to do after clicking on Visit Link**
|
| 185 |
+
|
| 186 |
+
Once you sign-up using your email, a button will pop-up showing that you need to confirm email. You can safely ignore this step and continue with login using information you just used for sign-up.
|
| 187 |
""")
|
| 188 |
|
|
|
|
| 189 |
live_status.change(None, inputs=live_status, js="(s) => playNotification(s)")
|
| 190 |
activate_btn.click(fn=manage_instance, outputs=[status_label, link_box])
|
| 191 |
refresh_btn.click(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 192 |
|
|
|
|
| 193 |
timer = gr.Timer(5)
|
| 194 |
timer.tick(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 195 |
|
|
|
|
| 196 |
demo.load(fn=user_joined, outputs=user_display)
|
| 197 |
demo.load(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
if __name__ == "__main__":
|
| 200 |
+
demo.launch(theme=gr.themes.Soft())
|
|
|