linxinhua commited on
Commit
6c18d96
Β·
verified Β·
1 Parent(s): cafb818

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -47
app.py CHANGED
@@ -154,13 +154,13 @@ def analyze_space_activity(available_spaces, repo_dir):
154
  return space_activity
155
 
156
  def create_status_display(space_activity):
157
- """Create formatted status display for all spaces"""
158
- status_display = "πŸ“Š **Current Space Status (sorted by availability):**\n\n"
159
 
160
  for i, space in enumerate(space_activity, 1):
161
- status_display += f"{i}. **{space['space_name']}**\n"
162
- status_display += f" β€’ Status: {space['status']}\n"
163
- status_display += f" β€’ Last activity: {space['last_activity_str']}\n\n"
164
 
165
  return status_display
166
 
@@ -208,7 +208,7 @@ def select_space_with_boundary_check(space_activity, student_id):
208
  return redirect_to_space(redirect_url, selected_space, status_display)
209
 
210
  def redirect_to_space(redirect_url, selected_space, status_display):
211
- """Execute redirect with detailed information display"""
212
 
213
  if selected_space['idle_minutes'] == float('inf'):
214
  idle_info = "Never used (completely fresh)"
@@ -217,17 +217,17 @@ def redirect_to_space(redirect_url, selected_space, status_display):
217
 
218
  redirect_html = f"""
219
  <div style="max-width: 900px; margin: 0 auto; padding: 20px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">
220
- <div style="text-align: center; margin-bottom: 30px; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 12px;">
221
- <h1 style="margin: 0 0 10px 0;">🎯 Learning Assistant Assigned</h1>
222
- <h2 style="margin: 0; font-weight: normal; opacity: 0.9;">{selected_space['space_name']}</h2>
223
- <p style="margin: 10px 0 0 0; font-size: 16px;">
224
  ✨ This space was idle for: <strong>{idle_info}</strong>
225
  </p>
226
  </div>
227
 
228
- <div style="background: #f8f9fa; padding: 25px; border-radius: 12px; margin-bottom: 25px; border-left: 4px solid #007bff;">
229
  <h3 style="margin-top: 0; color: #333;">πŸ“Š Space Selection Analysis</h3>
230
- <div style="background: white; padding: 20px; border-radius: 8px; font-family: 'Courier New', monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; border: 1px solid #e9ecef;">
231
  {status_display}
232
  </div>
233
  <p style="margin-bottom: 0; color: #666; font-size: 14px; margin-top: 15px;">
@@ -235,46 +235,67 @@ def redirect_to_space(redirect_url, selected_space, status_display):
235
  </p>
236
  </div>
237
 
238
- <div style="text-align: center; background: linear-gradient(135deg, #28a745, #20c997); color: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
239
- <h3 style="margin-top: 0;">πŸ”„ Redirecting to Your Learning Assistant</h3>
240
- <p style="margin: 15px 0;">You will be automatically redirected in <span id="countdown">5</span> seconds.</p>
 
 
241
 
242
- <div style="margin: 25px 0;">
243
- <a href="{redirect_url}"
244
- style="display: inline-block; background: rgba(255,255,255,0.2); color: white; padding: 12px 24px;
245
- text-decoration: none; border-radius: 25px; font-weight: bold; border: 2px solid rgba(255,255,255,0.3);
246
- transition: all 0.3s ease;"
247
- onmouseover="this.style.background='rgba(255,255,255,0.3)'"
248
- onmouseout="this.style.background='rgba(255,255,255,0.2)'">
249
- Click Here if Not Redirected Automatically
250
- </a>
251
- </div>
252
 
253
- <details style="margin-top: 20px; text-align: left;">
254
- <summary style="cursor: pointer; color: rgba(255,255,255,0.9);">πŸ”— View Target URL</summary>
255
- <p style="margin: 10px 0 0 0; padding: 10px; background: rgba(0,0,0,0.1); border-radius: 5px; word-break: break-all; font-family: monospace; font-size: 12px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  {redirect_url}
 
 
 
257
  </p>
258
- </details>
259
  </div>
260
- </div>
261
-
262
- <script>
263
- let countdown = 5;
264
- const countdownElement = document.getElementById('countdown');
265
 
266
- const timer = setInterval(function() {{
267
- countdown--;
268
- if (countdownElement) {{
269
- countdownElement.textContent = countdown;
270
- }}
271
-
272
- if (countdown <= 0) {{
273
- clearInterval(timer);
274
- window.location.href = "{redirect_url}";
275
- }}
276
- }}, 1000);
277
- </script>
 
 
 
 
 
 
278
  """
279
 
280
  return gr.HTML(redirect_html)
@@ -306,7 +327,7 @@ def get_url_params(request: gr.Request):
306
  query_params = dict(request.query_params)
307
  check_id = query_params.get('check', None)
308
  if check_id:
309
- return f"Load Distributor - Student {check_id}", check_id
310
  else:
311
  return "Load Distributor", None
312
  return "Load Distributor", None
 
154
  return space_activity
155
 
156
  def create_status_display(space_activity):
157
+ """Create formatted status display for all spaces with proper line breaks"""
158
+ status_display = "πŸ“Š **Current Space Status (sorted by availability):**<br><br>"
159
 
160
  for i, space in enumerate(space_activity, 1):
161
+ status_display += f"{i}. **{space['space_name']}**<br>"
162
+ status_display += f"&nbsp;&nbsp;&nbsp;β€’ Status: {space['status']}<br>"
163
+ status_display += f"&nbsp;&nbsp;&nbsp;β€’ Last activity: {space['last_activity_str']}<br><br>"
164
 
165
  return status_display
166
 
 
208
  return redirect_to_space(redirect_url, selected_space, status_display)
209
 
210
  def redirect_to_space(redirect_url, selected_space, status_display):
211
+ """Display redirect information with manual click option (HF Spaces can't auto-redirect)"""
212
 
213
  if selected_space['idle_minutes'] == float('inf'):
214
  idle_info = "Never used (completely fresh)"
 
217
 
218
  redirect_html = f"""
219
  <div style="max-width: 900px; margin: 0 auto; padding: 20px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">
220
+ <div style="text-align: center; margin-bottom: 30px; padding: 25px; background: linear-gradient(135deg, #28a745, #20c997); color: white; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
221
+ <h1 style="margin: 0 0 15px 0; font-size: 28px;">🎯 Learning Assistant Assigned</h1>
222
+ <h2 style="margin: 0 0 10px 0; font-weight: normal; font-size: 24px;">{selected_space['space_name']}</h2>
223
+ <p style="margin: 0; font-size: 18px; opacity: 0.9;">
224
  ✨ This space was idle for: <strong>{idle_info}</strong>
225
  </p>
226
  </div>
227
 
228
+ <div style="background: #f8f9fa; padding: 25px; border-radius: 12px; margin-bottom: 25px; border-left: 4px solid #28a745;">
229
  <h3 style="margin-top: 0; color: #333;">πŸ“Š Space Selection Analysis</h3>
230
+ <div style="background: white; padding: 20px; border-radius: 8px; font-size: 14px; line-height: 1.8; border: 1px solid #e9ecef;">
231
  {status_display}
232
  </div>
233
  <p style="margin-bottom: 0; color: #666; font-size: 14px; margin-top: 15px;">
 
235
  </p>
236
  </div>
237
 
238
+ <div style="text-align: center; margin-bottom: 30px; padding: 25px; background: linear-gradient(135deg, #2196f3, #1976d2); color: white; border-radius: 12px; box-shadow: 0 4px 6px rgba(33,150,243,0.3);">
239
+ <h2 style="margin-top: 0; color: white;">πŸš€ Access Your Learning Assistant</h2>
240
+ <p style="color: rgba(255,255,255,0.9); font-size: 16px; margin-bottom: 25px;">
241
+ Click the button below or copy the URL to access your assigned learning assistant.
242
+ </p>
243
 
244
+ <a href="{redirect_url}"
245
+ target="_blank"
246
+ style="display: inline-block; background: rgba(255,255,255,0.15); color: white;
247
+ padding: 15px 30px; font-size: 18px; font-weight: bold; text-decoration: none;
248
+ border-radius: 25px; border: 2px solid rgba(255,255,255,0.3);
249
+ transition: all 0.3s ease; margin-bottom: 20px;"
250
+ onmouseover="this.style.background='rgba(255,255,255,0.25)'; this.style.transform='translateY(-2px)'"
251
+ onmouseout="this.style.background='rgba(255,255,255,0.15)'; this.style.transform='translateY(0px)'">
252
+ Right-click β†’ "Open in new tab"
253
+ </a>
254
 
255
+ <div style="background: rgba(0,0,0,0.1); padding: 15px; border-radius: 8px; margin-top: 15px;">
256
+ <p style="margin: 5px 0; color: rgba(255,255,255,0.9); font-size: 14px; font-weight: bold;">
257
+ πŸ“‹ Copy this URL:
258
+ </p>
259
+ <div style="background: rgba(255,255,255,0.9); color: #333; padding: 12px; border-radius: 5px;
260
+ font-family: 'Courier New', monospace; font-size: 12px; word-break: break-all;
261
+ border: 1px solid rgba(255,255,255,0.5); margin-top: 8px;"
262
+ onclick="navigator.clipboard.writeText('{redirect_url}').then(() => {{
263
+ this.style.background='#d4edda';
264
+ this.innerHTML = 'βœ… URL Copied! Paste in new browser tab';
265
+ setTimeout(() => {{
266
+ this.style.background='rgba(255,255,255,0.9)';
267
+ this.innerHTML = '{redirect_url}';
268
+ }}, 2000);
269
+ }}).catch(() => {{
270
+ this.innerHTML = 'Click failed - please manually select and copy';
271
+ }})"
272
+ style="cursor: pointer; user-select: all;">
273
  {redirect_url}
274
+ </div>
275
+ <p style="margin: 8px 0 0 0; color: rgba(255,255,255,0.8); font-size: 12px;">
276
+ πŸ’‘ Click the URL above to copy it automatically
277
  </p>
278
+ </div>
279
  </div>
 
 
 
 
 
280
 
281
+ <div style="background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 20px; margin-bottom: 20px;">
282
+ <h4 style="margin-top: 0; color: #856404;">πŸ“‹ Alternative Access Methods</h4>
283
+ <div style="color: #856404; line-height: 1.6;">
284
+ <p><strong>Method 1:</strong> Click the blue button above (recommended)</p>
285
+ <p><strong>Method 2:</strong> Copy and paste this URL into a new browser tab:</p>
286
+ <div style="background: white; padding: 10px; border-radius: 5px; margin: 10px 0; font-family: monospace; font-size: 12px; word-break: break-all; border: 1px solid #ddd;">
287
+ {redirect_url}
288
+ </div>
289
+ <p><strong>Method 3:</strong> Search for "{selected_space['space_name']}" on Hugging Face Spaces</p>
290
+ </div>
291
+ </div>
292
+
293
+ <div style="text-align: center; padding: 20px; background: #f1f3f4; border-radius: 8px; margin-top: 20px;">
294
+ <p style="margin: 0; color: #5f6368; font-size: 14px;">
295
+ πŸ”„ Need a different assistant? <a href="javascript:location.reload()" style="color: #1976d2; text-decoration: none;">Refresh this page</a> to get reassigned.
296
+ </p>
297
+ </div>
298
+ </div>
299
  """
300
 
301
  return gr.HTML(redirect_html)
 
327
  query_params = dict(request.query_params)
328
  check_id = query_params.get('check', None)
329
  if check_id:
330
+ return f"Load Distributor", check_id
331
  else:
332
  return "Load Distributor", None
333
  return "Load Distributor", None