aimodelshare 0.3.7__py3-none-any.whl → 0.3.94__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. aimodelshare/moral_compass/__init__.py +51 -2
  2. aimodelshare/moral_compass/api_client.py +92 -4
  3. aimodelshare/moral_compass/apps/__init__.py +36 -16
  4. aimodelshare/moral_compass/apps/ai_consequences.py +98 -88
  5. aimodelshare/moral_compass/apps/bias_detective_ca.py +2722 -0
  6. aimodelshare/moral_compass/apps/bias_detective_en.py +2722 -0
  7. aimodelshare/moral_compass/apps/bias_detective_part1.py +2722 -0
  8. aimodelshare/moral_compass/apps/bias_detective_part2.py +2465 -0
  9. aimodelshare/moral_compass/apps/bias_detective_part_es.py +2722 -0
  10. aimodelshare/moral_compass/apps/ethical_revelation.py +237 -147
  11. aimodelshare/moral_compass/apps/fairness_fixer.py +1839 -859
  12. aimodelshare/moral_compass/apps/fairness_fixer_ca.py +1869 -0
  13. aimodelshare/moral_compass/apps/fairness_fixer_en.py +1869 -0
  14. aimodelshare/moral_compass/apps/fairness_fixer_es.py +1869 -0
  15. aimodelshare/moral_compass/apps/judge.py +130 -143
  16. aimodelshare/moral_compass/apps/justice_equity_upgrade.py +793 -831
  17. aimodelshare/moral_compass/apps/justice_equity_upgrade_ca.py +815 -0
  18. aimodelshare/moral_compass/apps/justice_equity_upgrade_en.py +815 -0
  19. aimodelshare/moral_compass/apps/justice_equity_upgrade_es.py +815 -0
  20. aimodelshare/moral_compass/apps/mc_integration_helpers.py +227 -745
  21. aimodelshare/moral_compass/apps/model_building_app_ca.py +4399 -0
  22. aimodelshare/moral_compass/apps/model_building_app_ca_final.py +3899 -0
  23. aimodelshare/moral_compass/apps/model_building_app_en.py +4167 -0
  24. aimodelshare/moral_compass/apps/model_building_app_en_final.py +3869 -0
  25. aimodelshare/moral_compass/apps/model_building_app_es.py +4351 -0
  26. aimodelshare/moral_compass/apps/model_building_app_es_final.py +3899 -0
  27. aimodelshare/moral_compass/apps/model_building_game.py +4211 -935
  28. aimodelshare/moral_compass/apps/moral_compass_challenge.py +195 -95
  29. aimodelshare/moral_compass/apps/what_is_ai.py +126 -117
  30. aimodelshare/moral_compass/challenge.py +98 -17
  31. {aimodelshare-0.3.7.dist-info → aimodelshare-0.3.94.dist-info}/METADATA +1 -1
  32. {aimodelshare-0.3.7.dist-info → aimodelshare-0.3.94.dist-info}/RECORD +35 -19
  33. aimodelshare/moral_compass/apps/bias_detective.py +0 -714
  34. {aimodelshare-0.3.7.dist-info → aimodelshare-0.3.94.dist-info}/WHEEL +0 -0
  35. {aimodelshare-0.3.7.dist-info → aimodelshare-0.3.94.dist-info}/licenses/LICENSE +0 -0
  36. {aimodelshare-0.3.7.dist-info → aimodelshare-0.3.94.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,3869 @@
1
+ """
2
+ Model Building Game - Gradio application for the Justice & Equity Challenge.
3
+
4
+ Session-based authentication with leaderboard caching and progressive rank unlocking.
5
+
6
+ Concurrency Notes:
7
+ - This app is designed to run in a multi-threaded environment (Cloud Run).
8
+ - Per-user state is stored in gr.State objects, NOT in os.environ.
9
+ - Caches are protected by locks to ensure thread safety.
10
+ - Linear algebra libraries are constrained to single-threaded mode to prevent
11
+ CPU oversubscription in containerized deployments.
12
+ """
13
+
14
+ import os
15
+
16
+ # -------------------------------------------------------------------------
17
+ # Thread Limit Configuration (MUST be set before importing numpy/sklearn)
18
+ # Prevents CPU oversubscription in containerized environments like Cloud Run.
19
+ # -------------------------------------------------------------------------
20
+ os.environ.setdefault("OMP_NUM_THREADS", "1")
21
+ os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
22
+ os.environ.setdefault("MKL_NUM_THREADS", "1")
23
+ os.environ.setdefault("NUMEXPR_NUM_THREADS", "1")
24
+
25
+ import time
26
+ import random
27
+ import requests
28
+ import contextlib
29
+ from io import StringIO
30
+ import threading
31
+ import functools
32
+ from pathlib import Path
33
+ from datetime import datetime, timedelta
34
+ from typing import Optional, Dict, Any, Tuple, Callable, TypeVar
35
+
36
+ import numpy as np
37
+ import pandas as pd
38
+ import gradio as gr
39
+
40
+ # --- Scikit-learn Imports ---
41
+ from sklearn.model_selection import train_test_split
42
+ from sklearn.preprocessing import StandardScaler
43
+ from sklearn.impute import SimpleImputer
44
+ from sklearn.compose import ColumnTransformer
45
+ from sklearn.pipeline import Pipeline
46
+ from sklearn.preprocessing import OneHotEncoder
47
+ from sklearn.linear_model import LogisticRegression
48
+ from sklearn.tree import DecisionTreeClassifier
49
+ from sklearn.ensemble import RandomForestClassifier
50
+ from sklearn.neighbors import KNeighborsClassifier
51
+
52
+ # --- AI Model Share Imports ---
53
+ try:
54
+ from aimodelshare.playground import Competition
55
+ except ImportError:
56
+ raise ImportError(
57
+ "The 'aimodelshare' library is required. Install with: pip install aimodelshare"
58
+ )
59
+
60
+ # -------------------------------------------------------------------------
61
+ # Configuration & Caching Infrastructure
62
+ # -------------------------------------------------------------------------
63
+
64
+
65
+ # -------------------------------------------------------------------------
66
+ # CACHE CONFIGURATION (Optimized: Thread-Safe SQLite)
67
+ # -------------------------------------------------------------------------
68
+ import sqlite3
69
+
70
+ CACHE_DB_FILE = "prediction_cache.sqlite"
71
+
72
+ def get_cached_prediction(key):
73
+ """
74
+ Lightning-fast lookup from SQLite database.
75
+ THREAD-SAFE FIX: Opens a new connection for every lookup.
76
+ """
77
+ # 1. Check if DB exists
78
+ if not os.path.exists(CACHE_DB_FILE):
79
+ return None
80
+
81
+ try:
82
+ # Use a context manager ('with') to ensure the connection
83
+ # is ALWAYS closed, releasing file locks immediately.
84
+ # timeout=10 ensures we don't wait forever if the file is busy.
85
+ with sqlite3.connect(CACHE_DB_FILE, timeout=10.0) as conn:
86
+ cursor = conn.cursor()
87
+ cursor.execute("SELECT value FROM cache WHERE key=?", (key,))
88
+ result = cursor.fetchone()
89
+
90
+ if result:
91
+ return result[0]
92
+ else:
93
+ return None
94
+
95
+ except sqlite3.OperationalError as e:
96
+ # Handle locking errors gracefully
97
+ print(f"⚠️ CACHE LOCK ERROR: {e}. Falling back to training.", flush=True)
98
+ return None
99
+
100
+ except Exception as e:
101
+ print(f"⚠️ DB READ ERROR: {e}", flush=True)
102
+ return None
103
+
104
+ print("✅ App configured for Thread-Safe SQLite Cache.")
105
+
106
+
107
+ LEADERBOARD_CACHE_SECONDS = int(os.environ.get("LEADERBOARD_CACHE_SECONDS", "45"))
108
+ MAX_LEADERBOARD_ENTRIES = os.environ.get("MAX_LEADERBOARD_ENTRIES")
109
+ MAX_LEADERBOARD_ENTRIES = int(MAX_LEADERBOARD_ENTRIES) if MAX_LEADERBOARD_ENTRIES else None
110
+ DEBUG_LOG = os.environ.get("DEBUG_LOG", "false").lower() == "true"
111
+
112
+ # In-memory caches (per container instance)
113
+ # Each cache has its own lock for thread safety under concurrent requests
114
+ _cache_lock = threading.Lock() # Protects _leaderboard_cache
115
+ _user_stats_lock = threading.Lock() # Protects _user_stats_cache
116
+ _auth_lock = threading.Lock() # Protects get_aws_token() credential injection
117
+
118
+ # Auth-aware leaderboard cache: separate entries for authenticated vs anonymous
119
+ # Structure: {"anon": {"data": df, "timestamp": float}, "auth": {"data": df, "timestamp": float}}
120
+ _leaderboard_cache: Dict[str, Dict[str, Any]] = {
121
+ "anon": {"data": None, "timestamp": 0.0},
122
+ "auth": {"data": None, "timestamp": 0.0},
123
+ }
124
+ _user_stats_cache: Dict[str, Dict[str, Any]] = {}
125
+ USER_STATS_TTL = LEADERBOARD_CACHE_SECONDS
126
+
127
+ # -------------------------------------------------------------------------
128
+ # Retry Helper for External API Calls
129
+ # -------------------------------------------------------------------------
130
+
131
+ T = TypeVar("T")
132
+
133
+ def _retry_with_backoff(
134
+ func: Callable[[], T],
135
+ max_attempts: int = 3,
136
+ base_delay: float = 0.5,
137
+ description: str = "operation"
138
+ ) -> T:
139
+ """
140
+ Execute a function with exponential backoff retry on failure.
141
+
142
+ Concurrency Note: This helper provides resilience against transient
143
+ network failures when calling external APIs (Competition.get_leaderboard,
144
+ playground.submit_model). Essential for Cloud Run deployments where
145
+ network calls may occasionally fail under load.
146
+
147
+ Args:
148
+ func: Callable to execute (should take no arguments)
149
+ max_attempts: Maximum number of attempts (default: 3)
150
+ base_delay: Initial delay in seconds, doubled each retry (default: 0.5)
151
+ description: Human-readable description for logging
152
+
153
+ Returns:
154
+ Result from successful function call
155
+
156
+ Raises:
157
+ Last exception if all attempts fail
158
+ """
159
+ last_exception: Optional[Exception] = None
160
+ delay = base_delay
161
+
162
+ for attempt in range(1, max_attempts + 1):
163
+ try:
164
+ return func()
165
+ except Exception as e:
166
+ last_exception = e
167
+ if attempt < max_attempts:
168
+ _log(f"{description} attempt {attempt} failed: {e}. Retrying in {delay}s...")
169
+ time.sleep(delay)
170
+ delay *= 2 # Exponential backoff
171
+ else:
172
+ _log(f"{description} failed after {max_attempts} attempts: {e}")
173
+
174
+ # Loop always runs at least once (max_attempts >= 1), so last_exception is set
175
+ raise last_exception # type: ignore[misc]
176
+
177
+ def _log(msg: str):
178
+ """Log message if DEBUG_LOG is enabled."""
179
+ if DEBUG_LOG:
180
+ print(f"[ModelBuildingGame] {msg}")
181
+
182
+ def _normalize_team_name(name: str) -> str:
183
+ """Normalize team name for consistent comparison and storage."""
184
+ if not name:
185
+ return ""
186
+ return " ".join(str(name).strip().split())
187
+
188
+ def _get_leaderboard_with_optional_token(playground_instance: Optional["Competition"], token: Optional[str] = None) -> Optional[pd.DataFrame]:
189
+ """
190
+ Fetch fresh leaderboard with optional token authentication and retry logic.
191
+
192
+ This is a helper function that centralizes the pattern of fetching
193
+ a fresh (non-cached) leaderboard with optional token authentication.
194
+ Use this for user-facing flows that require fresh, full data.
195
+
196
+ Concurrency Note: Uses _retry_with_backoff for resilience against
197
+ transient network failures.
198
+
199
+ Args:
200
+ playground_instance: The Competition playground instance (or None)
201
+ token: Optional authentication token for the fetch
202
+
203
+ Returns:
204
+ DataFrame with leaderboard data, or None if fetch fails or playground is None
205
+ """
206
+ if playground_instance is None:
207
+ return None
208
+
209
+ def _fetch():
210
+ if token:
211
+ return playground_instance.get_leaderboard(token=token)
212
+ return playground_instance.get_leaderboard()
213
+
214
+ try:
215
+ return _retry_with_backoff(_fetch, description="leaderboard fetch")
216
+ except Exception as e:
217
+ _log(f"Leaderboard fetch failed after retries: {e}")
218
+ return None
219
+
220
+ def _fetch_leaderboard(token: Optional[str]) -> Optional[pd.DataFrame]:
221
+ """
222
+ Fetch leaderboard with auth-aware caching (TTL: LEADERBOARD_CACHE_SECONDS).
223
+
224
+ Concurrency Note: Cache is keyed by auth scope ("anon" vs "auth") to prevent
225
+ cross-user data leakage. Authenticated users share a single "auth" cache entry
226
+ to avoid unbounded cache growth. Protected by _cache_lock.
227
+ """
228
+ # Determine cache key based on authentication status
229
+ cache_key = "auth" if token else "anon"
230
+ now = time.time()
231
+
232
+ with _cache_lock:
233
+ cache_entry = _leaderboard_cache[cache_key]
234
+ if (
235
+ cache_entry["data"] is not None
236
+ and now - cache_entry["timestamp"] < LEADERBOARD_CACHE_SECONDS
237
+ ):
238
+ _log(f"Leaderboard cache hit ({cache_key})")
239
+ return cache_entry["data"]
240
+
241
+ _log(f"Fetching fresh leaderboard ({cache_key})...")
242
+ df = None
243
+ try:
244
+ playground_id = "https://cf3wdpkg0d.execute-api.us-east-1.amazonaws.com/prod/m"
245
+ playground_instance = Competition(playground_id)
246
+
247
+ def _fetch():
248
+ return playground_instance.get_leaderboard(token=token) if token else playground_instance.get_leaderboard()
249
+
250
+ df = _retry_with_backoff(_fetch, description="leaderboard fetch")
251
+ if df is not None and not df.empty and MAX_LEADERBOARD_ENTRIES:
252
+ df = df.head(MAX_LEADERBOARD_ENTRIES)
253
+ _log(f"Leaderboard fetched ({cache_key}): {len(df) if df is not None else 0} entries")
254
+ except Exception as e:
255
+ _log(f"Leaderboard fetch failed ({cache_key}): {e}")
256
+ df = None
257
+
258
+ with _cache_lock:
259
+ _leaderboard_cache[cache_key]["data"] = df
260
+ _leaderboard_cache[cache_key]["timestamp"] = time.time()
261
+ return df
262
+
263
+ def _get_or_assign_team(username: str, leaderboard_df: Optional[pd.DataFrame]) -> Tuple[str, bool]:
264
+ """Get existing team from leaderboard or assign random team."""
265
+ # TEAM_NAMES is defined in configuration section below
266
+ try:
267
+ if leaderboard_df is not None and not leaderboard_df.empty and "Team" in leaderboard_df.columns:
268
+ user_submissions = leaderboard_df[leaderboard_df["username"] == username]
269
+ if not user_submissions.empty:
270
+ if "timestamp" in user_submissions.columns:
271
+ try:
272
+ user_submissions = user_submissions.copy()
273
+ user_submissions["timestamp"] = pd.to_datetime(
274
+ user_submissions["timestamp"], errors="coerce"
275
+ )
276
+ user_submissions = user_submissions.sort_values("timestamp", ascending=False)
277
+ _log(f"Sorted {len(user_submissions)} submissions by timestamp for {username}")
278
+ except Exception as ts_err:
279
+ _log(f"Timestamp sort error: {ts_err}")
280
+ existing_team = user_submissions.iloc[0]["Team"]
281
+ if pd.notna(existing_team) and str(existing_team).strip():
282
+ normalized = _normalize_team_name(existing_team)
283
+ _log(f"Found existing team for {username}: {normalized}")
284
+ return normalized, False
285
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
286
+ _log(f"Assigning new team to {username}: {new_team}")
287
+ return new_team, True
288
+ except Exception as e:
289
+ _log(f"Team assignment error: {e}")
290
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
291
+ return new_team, True
292
+
293
+ def _try_session_based_auth(request: "gr.Request") -> Tuple[bool, Optional[str], Optional[str]]:
294
+ """Attempt to authenticate via session token. Returns (success, username, token)."""
295
+ try:
296
+ session_id = request.query_params.get("sessionid") if request else None
297
+ if not session_id:
298
+ _log("No sessionid in request")
299
+ return False, None, None
300
+
301
+ from aimodelshare.aws import get_token_from_session, _get_username_from_token
302
+
303
+ token = get_token_from_session(session_id)
304
+ if not token:
305
+ _log("Failed to get token from session")
306
+ return False, None, None
307
+
308
+ username = _get_username_from_token(token)
309
+ if not username:
310
+ _log("Failed to extract username from token")
311
+ return False, None, None
312
+
313
+ _log(f"Session auth successful for {username}")
314
+ return True, username, token
315
+
316
+ except Exception as e:
317
+ _log(f"Session auth failed: {e}")
318
+ return False, None, None
319
+
320
+ def _compute_user_stats(username: str, token: str) -> Dict[str, Any]:
321
+ """
322
+ Compute user statistics with caching.
323
+
324
+ Concurrency Note: Protected by _user_stats_lock for thread-safe
325
+ cache reads and writes.
326
+ """
327
+ now = time.time()
328
+
329
+ # Thread-safe cache check
330
+ with _user_stats_lock:
331
+ cached = _user_stats_cache.get(username)
332
+ if cached and (now - cached.get("_ts", 0) < USER_STATS_TTL):
333
+ _log(f"User stats cache hit for {username}")
334
+ # Return shallow copy to prevent caller mutations from affecting cache.
335
+ # Stats dict contains only primitives (float, int, str), so shallow copy is sufficient.
336
+ return cached.copy()
337
+
338
+ _log(f"Computing fresh stats for {username}")
339
+ leaderboard_df = _fetch_leaderboard(token)
340
+ team_name, _ = _get_or_assign_team(username, leaderboard_df)
341
+
342
+ stats = {
343
+ "best_score": 0.0,
344
+ "rank": 0,
345
+ "team_name": team_name,
346
+ "submission_count": 0,
347
+ "last_score": 0.0,
348
+ "_ts": time.time()
349
+ }
350
+
351
+ try:
352
+ if leaderboard_df is not None and not leaderboard_df.empty:
353
+ user_submissions = leaderboard_df[leaderboard_df["username"] == username]
354
+ if not user_submissions.empty:
355
+ stats["submission_count"] = len(user_submissions)
356
+ if "accuracy" in user_submissions.columns:
357
+ stats["best_score"] = float(user_submissions["accuracy"].max())
358
+ if "timestamp" in user_submissions.columns:
359
+ try:
360
+ user_submissions = user_submissions.copy()
361
+ user_submissions["timestamp"] = pd.to_datetime(
362
+ user_submissions["timestamp"], errors="coerce"
363
+ )
364
+ recent = user_submissions.sort_values("timestamp", ascending=False).iloc[0]
365
+ stats["last_score"] = float(recent["accuracy"])
366
+ except:
367
+ stats["last_score"] = stats["best_score"]
368
+ else:
369
+ stats["last_score"] = stats["best_score"]
370
+
371
+ if "accuracy" in leaderboard_df.columns:
372
+ user_bests = leaderboard_df.groupby("username")["accuracy"].max()
373
+ ranked = user_bests.sort_values(ascending=False)
374
+ try:
375
+ stats["rank"] = int(ranked.index.get_loc(username) + 1)
376
+ except KeyError:
377
+ stats["rank"] = 0
378
+ except Exception as e:
379
+ _log(f"Error computing stats for {username}: {e}")
380
+
381
+ # Thread-safe cache update
382
+ with _user_stats_lock:
383
+ _user_stats_cache[username] = stats
384
+ _log(f"Stats for {username}: {stats}")
385
+ return stats
386
+ def _build_attempts_tracker_html(current_count, limit=10):
387
+ """
388
+ Generate HTML for the attempts tracker display.
389
+ Shows current attempt count vs limit with color coding.
390
+
391
+ Args:
392
+ current_count: Number of attempts used so far
393
+ limit: Maximum allowed attempts (default: ATTEMPT_LIMIT)
394
+
395
+ Returns:
396
+ str: HTML string for the tracker display
397
+ """
398
+ if current_count >= limit:
399
+ # Limit reached - red styling
400
+ bg_color = "#f0f9ff"
401
+ border_color = "#bae6fd"
402
+ text_color = "#0369a1"
403
+ icon = "🛑"
404
+ label = f"Last chance (for now) to boost your score!: {current_count}/{limit}"
405
+ else:
406
+ # Normal - blue styling
407
+ bg_color = "#f0f9ff"
408
+ border_color = "#bae6fd"
409
+ text_color = "#0369a1"
410
+ icon = "📊"
411
+ label = f"Attempts used: {current_count}/{limit}"
412
+
413
+ return f"""<div style='text-align:center; padding:8px; margin:8px 0; background:{bg_color}; border-radius:8px; border:1px solid {border_color};'>
414
+ <p style='margin:0; color:{text_color}; font-weight:600; font-size:1rem;'>{icon} {label}</p>
415
+ </div>"""
416
+
417
+ def check_attempt_limit(submission_count: int, limit: int = None) -> Tuple[bool, str]:
418
+ """Check if submission count exceeds limit."""
419
+ # ATTEMPT_LIMIT is defined in configuration section below
420
+ if limit is None:
421
+ limit = ATTEMPT_LIMIT
422
+
423
+ if submission_count >= limit:
424
+ msg = f"⚠️ Attempt limit reached ({submission_count}/{limit})"
425
+ return False, msg
426
+ return True, f"Attempts: {submission_count}/{limit}"
427
+
428
+ # -------------------------------------------------------------------------
429
+ # Future: Fairness Metrics
430
+ # -------------------------------------------------------------------------
431
+
432
+ # def compute_fairness_metrics(y_true, y_pred, sensitive_attrs):
433
+ # """
434
+ # Compute fairness metrics for model predictions.
435
+ #
436
+ # Args:
437
+ # y_true: Ground truth labels
438
+ # y_pred: Model predictions
439
+ # sensitive_attrs: DataFrame with sensitive attributes (race, sex, age)
440
+ #
441
+ # Returns:
442
+ # dict: Fairness metrics including demographic parity, equalized odds
443
+ #
444
+ # TODO: Implement using fairlearn or aif360
445
+ # """
446
+ # pass
447
+
448
+
449
+
450
+ # -------------------------------------------------------------------------
451
+ # 1. Configuration
452
+ # -------------------------------------------------------------------------
453
+
454
+ MY_PLAYGROUND_ID = "https://cf3wdpkg0d.execute-api.us-east-1.amazonaws.com/prod/m"
455
+
456
+ # --- Submission Limit Configuration ---
457
+ # Maximum number of successful leaderboard submissions per user per session.
458
+ # Preview runs (pre-login) and failed/invalid attempts do NOT count toward this limit.
459
+ # Only actual successful playground.submit_model() calls increment the count.
460
+ #
461
+ # TODO: Server-side persistent enforcement recommended
462
+ # The current attempt limit is stored in gr.State (per-session) and can be bypassed
463
+ # by refreshing the browser. For production use with 100+ concurrent users,
464
+ # consider implementing server-side persistence via Redis or Firestore to track
465
+ # attempt counts per user across sessions.
466
+ ATTEMPT_LIMIT = 1000000000
467
+
468
+ # --- Leaderboard Polling Configuration ---
469
+ # After a real authenticated submission, we poll the leaderboard to detect eventual consistency.
470
+ # This prevents the "stuck on first preview KPI" issue where the leaderboard hasn't updated yet.
471
+ # Increased from 12 to 60 to better tolerate backend latency and cold starts.
472
+ # If polling times out, optimistic fallback logic will provide provisional UI updates.
473
+ LEADERBOARD_POLL_TRIES = 60 # Number of polling attempts (increased to handle backend latency/cold starts)
474
+ LEADERBOARD_POLL_SLEEP = 1.0 # Sleep duration between polls (seconds)
475
+ ENABLE_AUTO_RESUBMIT_AFTER_READY = False # Future feature flag for auto-resubmit
476
+
477
+ MODEL_TYPES = {
478
+ "The Balanced Generalist": {
479
+ "model_builder": lambda: LogisticRegression(
480
+ max_iter=500, random_state=42, class_weight="balanced"
481
+ ),
482
+ "card": "A fast, reliable, well-rounded model. Good starting point; less prone to overfitting."
483
+ },
484
+ "The Rule-Maker": {
485
+ "model_builder": lambda: DecisionTreeClassifier(
486
+ random_state=42, class_weight="balanced"
487
+ ),
488
+ "card": "Learns simple 'if/then' rules. Easy to interpret, but can miss subtle patterns."
489
+ },
490
+ "The 'Nearest Neighbor'": {
491
+ "model_builder": lambda: KNeighborsClassifier(),
492
+ "card": "Looks at the closest past examples. 'You look like these others; I'll predict like they behave.'"
493
+ },
494
+ "The Deep Pattern-Finder": {
495
+ "model_builder": lambda: RandomForestClassifier(
496
+ random_state=42, class_weight="balanced"
497
+ ),
498
+ "card": "An ensemble of many decision trees. Powerful, can capture deep patterns; watch complexity."
499
+ }
500
+ }
501
+
502
+ DEFAULT_MODEL = "The Balanced Generalist"
503
+
504
+ TEAM_NAMES = [
505
+ "The Moral Champions", "The Justice League", "The Data Detectives",
506
+ "The Ethical Explorers", "The Fairness Finders", "The Accuracy Avengers"
507
+ ]
508
+ CURRENT_TEAM_NAME = random.choice(TEAM_NAMES)
509
+
510
+
511
+ # --- Feature groups for scaffolding (Weak -> Medium -> Strong) ---
512
+ FEATURE_SET_ALL_OPTIONS = [
513
+ ("Juvenile Felony Count", "juv_fel_count"),
514
+ ("Juvenile Misdemeanor Count", "juv_misd_count"),
515
+ ("Other Juvenile Count", "juv_other_count"),
516
+ ("Race", "race"),
517
+ ("Sex", "sex"),
518
+ ("Charge Severity (M/F)", "c_charge_degree"),
519
+ ("Days Before Arrest", "days_b_screening_arrest"),
520
+ ("Age", "age"),
521
+ ("Length of Stay", "length_of_stay"),
522
+ ("Prior Crimes Count", "priors_count"),
523
+ ]
524
+ FEATURE_SET_GROUP_1_VALS = [
525
+ "juv_fel_count", "juv_misd_count", "juv_other_count", "race", "sex",
526
+ "c_charge_degree", "days_b_screening_arrest"
527
+ ]
528
+ FEATURE_SET_GROUP_2_VALS = ["c_charge_desc", "age"]
529
+ FEATURE_SET_GROUP_3_VALS = ["length_of_stay", "priors_count"]
530
+ ALL_NUMERIC_COLS = [
531
+ "juv_fel_count", "juv_misd_count", "juv_other_count",
532
+ "days_b_screening_arrest", "age", "length_of_stay", "priors_count"
533
+ ]
534
+ ALL_CATEGORICAL_COLS = [
535
+ "race", "sex", "c_charge_degree"
536
+ ]
537
+ DEFAULT_FEATURE_SET = FEATURE_SET_GROUP_1_VALS
538
+
539
+
540
+ # --- Data Size config ---
541
+ DATA_SIZE_MAP = {
542
+ "Small (20%)": 0.2,
543
+ "Medium (60%)": 0.6,
544
+ "Large (80%)": 0.8,
545
+ "Full (100%)": 1.0
546
+ }
547
+ DEFAULT_DATA_SIZE = "Small (20%)"
548
+
549
+
550
+ MAX_ROWS = 4000
551
+ TOP_N_CHARGE_CATEGORICAL = 50
552
+ WARM_MINI_ROWS = 300 # Small warm dataset for instant preview
553
+ CACHE_MAX_AGE_HOURS = 24 # Cache validity duration
554
+ np.random.seed(42)
555
+
556
+ # Global state containers (populated during initialization)
557
+ playground = None
558
+ X_TRAIN_RAW = None # Keep this for 100%
559
+ X_TEST_RAW = None
560
+ Y_TRAIN = None
561
+ Y_TEST = None
562
+ # Add a container for our pre-sampled data
563
+ X_TRAIN_SAMPLES_MAP = {}
564
+ Y_TRAIN_SAMPLES_MAP = {}
565
+
566
+ # Warm mini dataset for instant preview
567
+ X_TRAIN_WARM = None
568
+ Y_TRAIN_WARM = None
569
+
570
+ # Cache for transformed test sets (for future performance improvements)
571
+ TEST_CACHE = {}
572
+
573
+ # Initialization flags to track readiness state
574
+ INIT_FLAGS = {
575
+ "competition": False,
576
+ "dataset_core": False,
577
+ "pre_samples_small": False,
578
+ "pre_samples_medium": False,
579
+ "pre_samples_large": False,
580
+ "pre_samples_full": False,
581
+ "leaderboard": False,
582
+ "default_preprocessor": False,
583
+ "warm_mini": False,
584
+ "errors": []
585
+ }
586
+
587
+ # Lock for thread-safe flag updates
588
+ INIT_LOCK = threading.Lock()
589
+
590
+ # -------------------------------------------------------------------------
591
+ # 2. Data & Backend Utilities
592
+ # -------------------------------------------------------------------------
593
+
594
+ def _get_cache_dir():
595
+ """Get or create the cache directory for datasets."""
596
+ cache_dir = Path.home() / ".aimodelshare_cache"
597
+ cache_dir.mkdir(exist_ok=True)
598
+ return cache_dir
599
+
600
+ def _safe_request_csv(url, cache_filename="compas.csv"):
601
+ """
602
+ Request CSV from URL with local caching.
603
+ Reuses cached file if it exists and is less than CACHE_MAX_AGE_HOURS old.
604
+ """
605
+ cache_dir = _get_cache_dir()
606
+ cache_path = cache_dir / cache_filename
607
+
608
+ # Check if cache exists and is fresh
609
+ if cache_path.exists():
610
+ file_time = datetime.fromtimestamp(cache_path.stat().st_mtime)
611
+ if datetime.now() - file_time < timedelta(hours=CACHE_MAX_AGE_HOURS):
612
+ return pd.read_csv(cache_path)
613
+
614
+ # Download fresh data
615
+ response = requests.get(url, timeout=30)
616
+ response.raise_for_status()
617
+ df = pd.read_csv(StringIO(response.text))
618
+
619
+ # Save to cache
620
+ df.to_csv(cache_path, index=False)
621
+
622
+ return df
623
+
624
+ def safe_int(value, default=1):
625
+ """
626
+ Safely coerce a value to int, returning default if value is None or invalid.
627
+ Protects against TypeError when Gradio sliders receive None.
628
+ """
629
+ if value is None:
630
+ return default
631
+ try:
632
+ return int(value)
633
+ except (ValueError, TypeError):
634
+ return default
635
+
636
+ def load_and_prep_data(use_cache=True):
637
+ """
638
+ Load, sample, and prepare raw COMPAS dataset.
639
+ NOW PRE-SAMPLES ALL DATA SIZES and creates warm mini dataset.
640
+ """
641
+ url = "https://raw.githubusercontent.com/propublica/compas-analysis/master/compas-scores-two-years.csv"
642
+
643
+ # Use cached version if available
644
+ if use_cache:
645
+ try:
646
+ df = _safe_request_csv(url)
647
+ except Exception as e:
648
+ print(f"Cache failed, fetching directly: {e}")
649
+ response = requests.get(url)
650
+ df = pd.read_csv(StringIO(response.text))
651
+ else:
652
+ response = requests.get(url)
653
+ df = pd.read_csv(StringIO(response.text))
654
+
655
+ # Calculate length_of_stay
656
+ try:
657
+ df['c_jail_in'] = pd.to_datetime(df['c_jail_in'])
658
+ df['c_jail_out'] = pd.to_datetime(df['c_jail_out'])
659
+ df['length_of_stay'] = (df['c_jail_out'] - df['c_jail_in']).dt.total_seconds() / (24 * 60 * 60) # in days
660
+ except Exception:
661
+ df['length_of_stay'] = np.nan
662
+
663
+ if df.shape[0] > MAX_ROWS:
664
+ df = df.sample(n=MAX_ROWS, random_state=42)
665
+
666
+ feature_columns = ALL_NUMERIC_COLS + ALL_CATEGORICAL_COLS
667
+ feature_columns = sorted(list(set(feature_columns)))
668
+
669
+ target_column = "two_year_recid"
670
+
671
+ if "c_charge_desc" in df.columns:
672
+ top_charges = df["c_charge_desc"].value_counts().head(TOP_N_CHARGE_CATEGORICAL).index
673
+ df["c_charge_desc"] = df["c_charge_desc"].apply(
674
+ lambda x: x if pd.notna(x) and x in top_charges else "OTHER"
675
+ )
676
+
677
+ for col in feature_columns:
678
+ if col not in df.columns:
679
+ if col == 'length_of_stay' and 'length_of_stay' in df.columns:
680
+ continue
681
+ df[col] = np.nan
682
+
683
+ X = df[feature_columns].copy()
684
+ y = df[target_column].copy()
685
+
686
+ X_train_raw, X_test_raw, y_train, y_test = train_test_split(
687
+ X, y, test_size=0.25, random_state=42, stratify=y
688
+ )
689
+
690
+ # Pre-sample all data sizes
691
+ global X_TRAIN_SAMPLES_MAP, Y_TRAIN_SAMPLES_MAP, X_TRAIN_WARM, Y_TRAIN_WARM
692
+
693
+ X_TRAIN_SAMPLES_MAP["Full (100%)"] = X_train_raw
694
+ Y_TRAIN_SAMPLES_MAP["Full (100%)"] = y_train
695
+
696
+ for label, frac in DATA_SIZE_MAP.items():
697
+ if frac < 1.0:
698
+ X_train_sampled = X_train_raw.sample(frac=frac, random_state=42)
699
+ y_train_sampled = y_train.loc[X_train_sampled.index]
700
+ X_TRAIN_SAMPLES_MAP[label] = X_train_sampled
701
+ Y_TRAIN_SAMPLES_MAP[label] = y_train_sampled
702
+
703
+ # Create warm mini dataset for instant preview
704
+ warm_size = min(WARM_MINI_ROWS, len(X_train_raw))
705
+ X_TRAIN_WARM = X_train_raw.sample(n=warm_size, random_state=42)
706
+ Y_TRAIN_WARM = y_train.loc[X_TRAIN_WARM.index]
707
+
708
+
709
+
710
+ return X_train_raw, X_test_raw, y_train, y_test
711
+
712
+ def _background_initializer():
713
+ """
714
+ Background thread that performs sequential initialization tasks.
715
+ Updates INIT_FLAGS dict with readiness booleans and captures errors.
716
+
717
+ Initialization sequence:
718
+ 1. Competition object connection
719
+ 2. Dataset cached download and core split
720
+ 3. Warm mini dataset creation
721
+ 4. Progressive sampling: small -> medium -> large -> full
722
+ 5. Leaderboard prefetch
723
+ 6. Default preprocessor fit on small sample
724
+ """
725
+ global playground, X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST
726
+
727
+ try:
728
+ # Step 1: Connect to competition
729
+ with INIT_LOCK:
730
+ if playground is None:
731
+ playground = Competition(MY_PLAYGROUND_ID)
732
+ INIT_FLAGS["competition"] = True
733
+ except Exception as e:
734
+ with INIT_LOCK:
735
+ INIT_FLAGS["errors"].append(f"Competition connection failed: {str(e)}")
736
+
737
+ try:
738
+ # Step 2: Load dataset core (train/test split)
739
+ X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST = load_and_prep_data(use_cache=True)
740
+ with INIT_LOCK:
741
+ INIT_FLAGS["dataset_core"] = True
742
+ except Exception as e:
743
+ with INIT_LOCK:
744
+ INIT_FLAGS["errors"].append(f"Dataset loading failed: {str(e)}")
745
+ return # Cannot proceed without data
746
+
747
+ try:
748
+ # Step 3: Warm mini dataset (already created in load_and_prep_data)
749
+ if X_TRAIN_WARM is not None and len(X_TRAIN_WARM) > 0:
750
+ with INIT_LOCK:
751
+ INIT_FLAGS["warm_mini"] = True
752
+ except Exception as e:
753
+ with INIT_LOCK:
754
+ INIT_FLAGS["errors"].append(f"Warm mini dataset failed: {str(e)}")
755
+
756
+ # Progressive sampling - samples are already created in load_and_prep_data
757
+ # Just mark them as ready sequentially with delays to simulate progressive loading
758
+
759
+ try:
760
+ # Step 4a: Small sample (20%)
761
+ time.sleep(0.5) # Simulate processing
762
+ with INIT_LOCK:
763
+ INIT_FLAGS["pre_samples_small"] = True
764
+ except Exception as e:
765
+ with INIT_LOCK:
766
+ INIT_FLAGS["errors"].append(f"Small sample failed: {str(e)}")
767
+
768
+ try:
769
+ # Step 4b: Medium sample (60%)
770
+ time.sleep(0.5)
771
+ with INIT_LOCK:
772
+ INIT_FLAGS["pre_samples_medium"] = True
773
+ except Exception as e:
774
+ with INIT_LOCK:
775
+ INIT_FLAGS["errors"].append(f"Medium sample failed: {str(e)}")
776
+
777
+ try:
778
+ # Step 4c: Large sample (80%)
779
+ time.sleep(0.5)
780
+ with INIT_LOCK:
781
+ INIT_FLAGS["pre_samples_large"] = True
782
+ except Exception as e:
783
+ with INIT_LOCK:
784
+ INIT_FLAGS["errors"].append(f"Large sample failed: {str(e)}")
785
+ print(f"✗ Large sample failed: {e}")
786
+
787
+ try:
788
+ # Step 4d: Full sample (100%)
789
+ print("Background init: Full sample (100%)...")
790
+ time.sleep(0.5)
791
+ with INIT_LOCK:
792
+ INIT_FLAGS["pre_samples_full"] = True
793
+ except Exception as e:
794
+ with INIT_LOCK:
795
+ INIT_FLAGS["errors"].append(f"Full sample failed: {str(e)}")
796
+
797
+ try:
798
+ # Step 5: Leaderboard prefetch (best-effort, unauthenticated)
799
+ # Concurrency Note: Do NOT use os.environ for ambient token - prefetch
800
+ # anonymously to warm the cache for initial page loads.
801
+ if playground is not None:
802
+ _ = _get_leaderboard_with_optional_token(playground, None)
803
+ with INIT_LOCK:
804
+ INIT_FLAGS["leaderboard"] = True
805
+ except Exception as e:
806
+ with INIT_LOCK:
807
+ INIT_FLAGS["errors"].append(f"Leaderboard prefetch failed: {str(e)}")
808
+
809
+ try:
810
+ # Step 6: Default preprocessor on small sample
811
+ _fit_default_preprocessor()
812
+ with INIT_LOCK:
813
+ INIT_FLAGS["default_preprocessor"] = True
814
+ except Exception as e:
815
+ with INIT_LOCK:
816
+ INIT_FLAGS["errors"].append(f"Default preprocessor failed: {str(e)}")
817
+ print(f"✗ Default preprocessor failed: {e}")
818
+
819
+
820
+ def _fit_default_preprocessor():
821
+ """
822
+ Pre-fit a default preprocessor on the small sample with default features.
823
+ Uses memoized preprocessor builder for efficiency.
824
+ """
825
+ if "Small (20%)" not in X_TRAIN_SAMPLES_MAP:
826
+ return
827
+
828
+ X_sample = X_TRAIN_SAMPLES_MAP["Small (20%)"]
829
+
830
+ # Use default feature set
831
+ numeric_cols = [f for f in DEFAULT_FEATURE_SET if f in ALL_NUMERIC_COLS]
832
+ categorical_cols = [f for f in DEFAULT_FEATURE_SET if f in ALL_CATEGORICAL_COLS]
833
+
834
+ if not numeric_cols and not categorical_cols:
835
+ return
836
+
837
+ # Use memoized builder
838
+ preprocessor, selected_cols = build_preprocessor(numeric_cols, categorical_cols)
839
+ preprocessor.fit(X_sample[selected_cols])
840
+
841
+ def start_background_init():
842
+ """
843
+ Start the background initialization thread.
844
+ Should be called once at app creation.
845
+ """
846
+ thread = threading.Thread(target=_background_initializer, daemon=True)
847
+ thread.start()
848
+
849
+ def poll_init_status():
850
+ """
851
+ Poll the initialization status and return readiness bool.
852
+ Returns empty string for HTML so users don't see the checklist.
853
+
854
+ Returns:
855
+ tuple: (status_html, ready_bool)
856
+ """
857
+ with INIT_LOCK:
858
+ flags = INIT_FLAGS.copy()
859
+
860
+ # Determine if minimum requirements met
861
+ ready = flags["competition"] and flags["dataset_core"] and flags["pre_samples_small"]
862
+
863
+ return "", ready
864
+
865
+ def get_available_data_sizes():
866
+ """
867
+ Return list of data sizes that are currently available based on init flags.
868
+ """
869
+ with INIT_LOCK:
870
+ flags = INIT_FLAGS.copy()
871
+
872
+ available = []
873
+ if flags["pre_samples_small"]:
874
+ available.append("Small (20%)")
875
+ if flags["pre_samples_medium"]:
876
+ available.append("Medium (60%)")
877
+ if flags["pre_samples_large"]:
878
+ available.append("Large (80%)")
879
+ if flags["pre_samples_full"]:
880
+ available.append("Full (100%)")
881
+
882
+ return available if available else ["Small (20%)"] # Fallback
883
+
884
+ def _is_ready() -> bool:
885
+ """
886
+ Check if initialization is complete and system is ready for real submissions.
887
+
888
+ Returns:
889
+ bool: True if competition, dataset, and small sample are initialized
890
+ """
891
+ with INIT_LOCK:
892
+ flags = INIT_FLAGS.copy()
893
+ return flags["competition"] and flags["dataset_core"] and flags["pre_samples_small"]
894
+
895
+ def _get_user_latest_accuracy(df: Optional[pd.DataFrame], username: str) -> Optional[float]:
896
+ """
897
+ Extract the user's latest submission accuracy from the leaderboard.
898
+
899
+ Uses timestamp sorting when available; otherwise assumes last row is latest.
900
+
901
+ Args:
902
+ df: Leaderboard DataFrame
903
+ username: Username to extract accuracy for
904
+
905
+ Returns:
906
+ float: Latest submission accuracy, or None if not found/invalid
907
+ """
908
+ if df is None or df.empty:
909
+ return None
910
+
911
+ try:
912
+ user_rows = df[df["username"] == username]
913
+ if user_rows.empty or "accuracy" not in user_rows.columns:
914
+ return None
915
+
916
+ # Try timestamp-based sorting if available
917
+ if "timestamp" in user_rows.columns:
918
+ user_rows = user_rows.copy()
919
+ user_rows["__parsed_ts"] = pd.to_datetime(user_rows["timestamp"], errors="coerce")
920
+ valid_ts = user_rows[user_rows["__parsed_ts"].notna()]
921
+
922
+ if not valid_ts.empty:
923
+ # Sort by timestamp and get latest
924
+ latest_row = valid_ts.sort_values("__parsed_ts", ascending=False).iloc[0]
925
+ return float(latest_row["accuracy"])
926
+
927
+ # Fallback: assume last row is latest (append order)
928
+ return float(user_rows.iloc[-1]["accuracy"])
929
+
930
+ except Exception as e:
931
+ _log(f"Error extracting latest accuracy for {username}: {e}")
932
+ return None
933
+
934
+ def _get_user_latest_ts(df: Optional[pd.DataFrame], username: str) -> Optional[float]:
935
+ """
936
+ Extract the user's latest valid timestamp from the leaderboard.
937
+
938
+ Args:
939
+ df: Leaderboard DataFrame
940
+ username: Username to extract timestamp for
941
+
942
+ Returns:
943
+ float: Latest timestamp as unix epoch, or None if not found/invalid
944
+ """
945
+ if df is None or df.empty:
946
+ return None
947
+
948
+ try:
949
+ user_rows = df[df["username"] == username]
950
+ if user_rows.empty or "timestamp" not in user_rows.columns:
951
+ return None
952
+
953
+ # Parse timestamps and get the latest
954
+ user_rows = user_rows.copy()
955
+ user_rows["__parsed_ts"] = pd.to_datetime(user_rows["timestamp"], errors="coerce")
956
+ valid_ts = user_rows[user_rows["__parsed_ts"].notna()]
957
+
958
+ if valid_ts.empty:
959
+ return None
960
+
961
+ latest_ts = valid_ts["__parsed_ts"].max()
962
+ return latest_ts.timestamp() if pd.notna(latest_ts) else None
963
+ except Exception as e:
964
+ _log(f"Error extracting latest timestamp for {username}: {e}")
965
+ return None
966
+
967
+ def _user_rows_changed(
968
+ refreshed_leaderboard: Optional[pd.DataFrame],
969
+ username: str,
970
+ old_row_count: int,
971
+ old_best_score: float,
972
+ old_latest_ts: Optional[float] = None,
973
+ old_latest_score: Optional[float] = None
974
+ ) -> bool:
975
+ """
976
+ Check if user's leaderboard entries have changed after submission.
977
+
978
+ Used after polling to detect if the leaderboard has updated with the new submission.
979
+ Checks row count (new submission added), best score (score improved), latest timestamp,
980
+ and latest accuracy (handles backend overwrite without append).
981
+
982
+ Args:
983
+ refreshed_leaderboard: Fresh leaderboard data
984
+ username: Username to check for
985
+ old_row_count: Previous number of submissions for this user
986
+ old_best_score: Previous best accuracy score
987
+ old_latest_ts: Previous latest timestamp (unix epoch), optional
988
+ old_latest_score: Previous latest submission accuracy, optional
989
+
990
+ Returns:
991
+ bool: True if user has more rows, better score, newer timestamp, or changed latest accuracy
992
+ """
993
+ if refreshed_leaderboard is None or refreshed_leaderboard.empty:
994
+ return False
995
+
996
+ try:
997
+ user_rows = refreshed_leaderboard[refreshed_leaderboard["username"] == username]
998
+ if user_rows.empty:
999
+ return False
1000
+
1001
+ new_row_count = len(user_rows)
1002
+ new_best_score = float(user_rows["accuracy"].max()) if "accuracy" in user_rows.columns else 0.0
1003
+ new_latest_ts = _get_user_latest_ts(refreshed_leaderboard, username)
1004
+ new_latest_score = _get_user_latest_accuracy(refreshed_leaderboard, username)
1005
+
1006
+ # Changed if we have more submissions, better score, newer timestamp, or changed latest accuracy
1007
+ changed = (new_row_count > old_row_count) or (new_best_score > old_best_score + 0.0001)
1008
+
1009
+ # Check timestamp if available
1010
+ if old_latest_ts is not None and new_latest_ts is not None:
1011
+ changed = changed or (new_latest_ts > old_latest_ts)
1012
+
1013
+ # Check latest accuracy change (handles overwrite-without-append case)
1014
+ if old_latest_score is not None and new_latest_score is not None:
1015
+ accuracy_changed = abs(new_latest_score - old_latest_score) >= 0.00001
1016
+ if accuracy_changed:
1017
+ _log(f"Latest accuracy changed: {old_latest_score:.4f} -> {new_latest_score:.4f}")
1018
+ changed = changed or accuracy_changed
1019
+
1020
+ if changed:
1021
+ _log(f"User rows changed for {username}:")
1022
+ _log(f" Row count: {old_row_count} -> {new_row_count}")
1023
+ _log(f" Best score: {old_best_score:.4f} -> {new_best_score:.4f}")
1024
+ _log(f" Latest score: {old_latest_score if old_latest_score else 'N/A'} -> {new_latest_score if new_latest_score else 'N/A'}")
1025
+ _log(f" Timestamp: {old_latest_ts} -> {new_latest_ts}")
1026
+
1027
+ return changed
1028
+ except Exception as e:
1029
+ _log(f"Error checking user rows: {e}")
1030
+ return False
1031
+
1032
+ @functools.lru_cache(maxsize=32)
1033
+ def _get_cached_preprocessor_config(numeric_cols_tuple, categorical_cols_tuple):
1034
+ """
1035
+ Create and return preprocessor configuration (memoized).
1036
+ Uses tuples for hashability in lru_cache.
1037
+
1038
+ Concurrency Note: Uses sparse_output=True for OneHotEncoder to reduce memory
1039
+ footprint under concurrent requests. Downstream models that require dense
1040
+ arrays (DecisionTree, RandomForest) will convert via .toarray() as needed.
1041
+ LogisticRegression and KNeighborsClassifier handle sparse matrices natively.
1042
+
1043
+ Returns tuple of (transformers_list, selected_columns) ready for ColumnTransformer.
1044
+ """
1045
+ numeric_cols = list(numeric_cols_tuple)
1046
+ categorical_cols = list(categorical_cols_tuple)
1047
+
1048
+ transformers = []
1049
+ selected_cols = []
1050
+
1051
+ if numeric_cols:
1052
+ num_tf = Pipeline(steps=[
1053
+ ("imputer", SimpleImputer(strategy="median")),
1054
+ ("scaler", StandardScaler())
1055
+ ])
1056
+ transformers.append(("num", num_tf, numeric_cols))
1057
+ selected_cols.extend(numeric_cols)
1058
+
1059
+ if categorical_cols:
1060
+ # Use sparse_output=True to reduce memory footprint
1061
+ cat_tf = Pipeline(steps=[
1062
+ ("imputer", SimpleImputer(strategy="constant", fill_value="missing")),
1063
+ ("onehot", OneHotEncoder(handle_unknown="ignore", sparse_output=True))
1064
+ ])
1065
+ transformers.append(("cat", cat_tf, categorical_cols))
1066
+ selected_cols.extend(categorical_cols)
1067
+
1068
+ return transformers, selected_cols
1069
+
1070
+ def build_preprocessor(numeric_cols, categorical_cols):
1071
+ """
1072
+ Build a preprocessor using cached configuration.
1073
+ The configuration (pipeline structure) is memoized; the actual fit is not.
1074
+
1075
+ Note: Returns sparse matrices when categorical columns are present.
1076
+ Use _ensure_dense() helper if model requires dense input.
1077
+ """
1078
+ # Convert to tuples for caching
1079
+ numeric_tuple = tuple(sorted(numeric_cols))
1080
+ categorical_tuple = tuple(sorted(categorical_cols))
1081
+
1082
+ transformers, selected_cols = _get_cached_preprocessor_config(numeric_tuple, categorical_tuple)
1083
+
1084
+ # Create new ColumnTransformer with cached config
1085
+ preprocessor = ColumnTransformer(transformers=transformers, remainder="drop")
1086
+
1087
+ return preprocessor, selected_cols
1088
+
1089
+ def _ensure_dense(X):
1090
+ """
1091
+ Convert sparse matrix to dense if necessary.
1092
+
1093
+ Helper function for models that don't support sparse input
1094
+ (DecisionTree, RandomForest). LogisticRegression and KNN
1095
+ handle sparse matrices natively.
1096
+ """
1097
+ from scipy import sparse
1098
+ if sparse.issparse(X):
1099
+ return X.toarray()
1100
+ return X
1101
+
1102
+ def tune_model_complexity(model, level):
1103
+ """
1104
+ Map a 1–10 slider value to model hyperparameters.
1105
+ Levels 1–3: Conservative / simple
1106
+ Levels 4–7: Balanced
1107
+ Levels 8–10: Aggressive / risk of overfitting
1108
+ """
1109
+ level = int(level)
1110
+ if isinstance(model, LogisticRegression):
1111
+ c_map = {1: 0.01, 2: 0.025, 3: 0.05, 4: 0.1, 5: 0.25, 6: 0.5, 7: 1.0, 8: 2.0, 9: 5.0, 10: 10.0}
1112
+ model.C = c_map.get(level, 1.0)
1113
+ model.max_iter = max(getattr(model, "max_iter", 0), 500)
1114
+ elif isinstance(model, RandomForestClassifier):
1115
+ depth_map = {1: 3, 2: 5, 3: 7, 4: 9, 5: 11, 6: 15, 7: 20, 8: 25, 9: None, 10: None}
1116
+ est_map = {1: 20, 2: 30, 3: 40, 4: 60, 5: 80, 6: 100, 7: 120, 8: 150, 9: 180, 10: 220}
1117
+ model.max_depth = depth_map.get(level, 10)
1118
+ model.n_estimators = est_map.get(level, 100)
1119
+ elif isinstance(model, DecisionTreeClassifier):
1120
+ depth_map = {1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 8, 7: 10, 8: 12, 9: 15, 10: None}
1121
+ model.max_depth = depth_map.get(level, 6)
1122
+ elif isinstance(model, KNeighborsClassifier):
1123
+ k_map = {1: 100, 2: 75, 3: 60, 4: 50, 5: 40, 6: 30, 7: 25, 8: 15, 9: 7, 10: 3}
1124
+ model.n_neighbors = k_map.get(level, 25)
1125
+ return model
1126
+
1127
+ # --- New Helper Functions for HTML Generation ---
1128
+
1129
+ def _normalize_team_name(name: str) -> str:
1130
+ """
1131
+ Normalize team name for consistent comparison and storage.
1132
+
1133
+ Strips leading/trailing whitespace and collapses multiple spaces into single spaces.
1134
+ This ensures consistent formatting across environment variables, state, and leaderboard rendering.
1135
+
1136
+ Args:
1137
+ name: Team name to normalize (can be None or empty)
1138
+
1139
+ Returns:
1140
+ str: Normalized team name, or empty string if input is None/empty
1141
+
1142
+ Examples:
1143
+ >>> _normalize_team_name(" The Ethical Explorers ")
1144
+ 'The Ethical Explorers'
1145
+ >>> _normalize_team_name("The Moral Champions")
1146
+ 'The Moral Champions'
1147
+ >>> _normalize_team_name(None)
1148
+ ''
1149
+ """
1150
+ if not name:
1151
+ return ""
1152
+ return " ".join(str(name).strip().split())
1153
+
1154
+
1155
+
1156
+ def _build_skeleton_leaderboard(rows=6, is_team=True, submit_button_label="5. 🔬 Build & Submit Model"):
1157
+ context_label = "Team" if is_team else "Individual"
1158
+ return f"""
1159
+ <div class='lb-placeholder' aria-live='polite'>
1160
+ <div class='lb-placeholder-title'>{context_label} Standings Pending</div>
1161
+ <div class='lb-placeholder-sub'>
1162
+ <p style='margin:0 0 6px 0;'>Submit your first model to populate this table.</p>
1163
+ <p style='margin:0;'><strong>Click “{submit_button_label}” (bottom-left)</strong> to begin!</p>
1164
+ </div>
1165
+ </div>
1166
+ """
1167
+ # --- FIX APPLIED HERE ---
1168
+ def build_login_prompt_html():
1169
+ """
1170
+ Generate HTML for the login prompt text *only*.
1171
+ The styled preview card will be prepended to this.
1172
+ """
1173
+ return f"""
1174
+ <h2 style='color: #111827; margin-top:20px; border-top: 2px solid #e5e7eb; padding-top: 20px;'>🔐 Sign in to submit & rank</h2>
1175
+ <div style='margin-top:16px; text-align:left; font-size:1rem; line-height:1.6; color:#374151;'>
1176
+ <p style='margin:12px 0;'>
1177
+ This is a preview run only. Sign in to publish your score to the live leaderboard,
1178
+ earn promotions, and contribute team points.
1179
+ </p>
1180
+ <p style='margin:12px 0;'>
1181
+ <strong>New user?</strong> Create a free account at
1182
+ <a href='https://www.modelshare.ai/login' target='_blank'
1183
+ style='color:#4f46e5; text-decoration:underline;'>modelshare.ai/login</a>
1184
+ </p>
1185
+ </div>
1186
+ """
1187
+ # --- END OF FIX ---
1188
+
1189
+ def _build_kpi_card_html(new_score, last_score, new_rank, last_rank, submission_count, is_preview=False, is_pending=False, local_test_accuracy=None):
1190
+ """Generates the HTML for the KPI feedback card. Supports preview mode label and pending state."""
1191
+
1192
+ # Handle pending state - show processing message with provisional diff
1193
+ if is_pending:
1194
+ title = "⏳ Submission Processing"
1195
+ acc_color = "#3b82f6" # Blue
1196
+ acc_text = f"{(local_test_accuracy * 100):.2f}%" if local_test_accuracy is not None else "N/A"
1197
+
1198
+ # Compute provisional diff between local (new) and last score
1199
+ if local_test_accuracy is not None and last_score is not None and last_score > 0:
1200
+ score_diff = local_test_accuracy - last_score
1201
+ if abs(score_diff) < 0.0001:
1202
+ acc_diff_html = "<p style='font-size: 1.5rem; font-weight: 600; color: #6b7280; margin:0;'>No Change (↔) <span style='font-size: 0.9rem; color: #9ca3af;'>(Provisional)</span></p><p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>Pending leaderboard update...</p>"
1203
+ elif score_diff > 0:
1204
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: #16a34a; margin:0;'>+{(score_diff * 100):.2f} (⬆️) <span style='font-size: 0.9rem; color: #9ca3af;'>(Provisional)</span></p><p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>Pending leaderboard update...</p>"
1205
+ else:
1206
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: #ef4444; margin:0;'>{(score_diff * 100):.2f} (⬇️) <span style='font-size: 0.9rem; color: #9ca3af;'>(Provisional)</span></p><p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>Pending leaderboard update...</p>"
1207
+ else:
1208
+ # No last score available - just show pending message
1209
+ acc_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>Pending leaderboard update...</p>"
1210
+
1211
+ border_color = acc_color
1212
+ rank_color = "#6b7280" # Gray
1213
+ rank_text = "Pending"
1214
+ rank_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0;'>Calculating rank...</p>"
1215
+
1216
+ # Handle preview mode - Styled to match "success" card
1217
+ elif is_preview:
1218
+ title = "🔬 Successful Preview Run!"
1219
+ acc_color = "#16a34a" # Green (like success)
1220
+ acc_text = f"{(new_score * 100):.2f}%" if new_score > 0 else "N/A"
1221
+ acc_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>(Preview only - not submitted)</p>" # Neutral color
1222
+ border_color = acc_color # Green border
1223
+ rank_color = "#3b82f6" # Blue (like rank)
1224
+ rank_text = "N/A" # Placeholder
1225
+ rank_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0;'>Not ranked (preview)</p>" # Neutral color
1226
+
1227
+ # 1. Handle First Submission
1228
+ elif submission_count == 0:
1229
+ title = "🎉 First Model Submitted!"
1230
+ acc_color = "#16a34a" # green
1231
+ acc_text = f"{(new_score * 100):.2f}%"
1232
+ acc_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>(Your first score!)</p>"
1233
+
1234
+ rank_color = "#3b82f6" # blue
1235
+ rank_text = f"#{new_rank}"
1236
+ rank_diff_html = "<p style='font-size: 1.5rem; font-weight: 600; color: #3b82f6; margin:0;'>You're on the board!</p>"
1237
+ border_color = acc_color
1238
+
1239
+ else:
1240
+ # 2. Handle Score Changes
1241
+ score_diff = new_score - last_score
1242
+ if abs(score_diff) < 0.0001:
1243
+ title = "✅ Submission Successful"
1244
+ acc_color = "#6b7280" # gray
1245
+ acc_text = f"{(new_score * 100):.2f}%"
1246
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {acc_color}; margin:0;'>No Change (↔)</p>"
1247
+ border_color = acc_color
1248
+ elif score_diff > 0:
1249
+ title = "✅ Submission Successful!"
1250
+ acc_color = "#16a34a" # green
1251
+ acc_text = f"{(new_score * 100):.2f}%"
1252
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {acc_color}; margin:0;'>+{(score_diff * 100):.2f} (⬆️)</p>"
1253
+ border_color = acc_color
1254
+ else:
1255
+ title = "📉 Score Dropped"
1256
+ acc_color = "#ef4444" # red
1257
+ acc_text = f"{(new_score * 100):.2f}%"
1258
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {acc_color}; margin:0;'>{(score_diff * 100):.2f} (⬇️)</p>"
1259
+ border_color = acc_color
1260
+
1261
+ # 3. Handle Rank Changes
1262
+ rank_diff = last_rank - new_rank
1263
+ rank_color = "#3b82f6" # blue
1264
+ rank_text = f"#{new_rank}"
1265
+ if last_rank == 0: # Handle first rank
1266
+ rank_diff_html = "<p style='font-size: 1.5rem; font-weight: 600; color: #3b82f6; margin:0;'>You're on the board!</p>"
1267
+ elif rank_diff > 0:
1268
+ rank_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: #16a34a; margin:0;'>🚀 Moved up {rank_diff} spot{'s' if rank_diff > 1 else ''}!</p>"
1269
+ elif rank_diff < 0:
1270
+ rank_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: #ef4444; margin:0;'>🔻 Dropped {abs(rank_diff)} spot{'s' if abs(rank_diff) > 1 else ''}</p>"
1271
+ else:
1272
+ rank_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {rank_color}; margin:0;'>No Change (↔)</p>"
1273
+
1274
+ return f"""
1275
+ <div class='kpi-card' style='border-color: {border_color};'>
1276
+ <h2 style='color: #111827; margin-top:0;'>{title}</h2>
1277
+ <div class='kpi-card-body'>
1278
+ <div class='kpi-metric-box'>
1279
+ <p class='kpi-label'>New Accuracy</p>
1280
+ <p class='kpi-score' style='color: {acc_color};'>{acc_text}</p>
1281
+ {acc_diff_html}
1282
+ </div>
1283
+ <div class='kpi-metric-box'>
1284
+ <p class='kpi-label'>Your Rank</p>
1285
+ <p class='kpi-score' style='color: {rank_color};'>{rank_text}</p>
1286
+ {rank_diff_html}
1287
+ </div>
1288
+ </div>
1289
+ </div>
1290
+ """
1291
+
1292
+ def _build_team_html(team_summary_df, team_name):
1293
+ """
1294
+ Generates the HTML for the team leaderboard.
1295
+
1296
+ Uses normalized, case-insensitive comparison to highlight the user's team row,
1297
+ ensuring reliable highlighting even with whitespace or casing variations.
1298
+ """
1299
+ if team_summary_df is None or team_summary_df.empty:
1300
+ return "<p style='text-align:center; color:#6b7280; padding-top:20px;'>No team submissions yet.</p>"
1301
+
1302
+ # Normalize the current user's team name for comparison
1303
+ normalized_user_team = _normalize_team_name(team_name).lower()
1304
+
1305
+ header = """
1306
+ <table class='leaderboard-html-table'>
1307
+ <thead>
1308
+ <tr>
1309
+ <th>Rank</th>
1310
+ <th>Team</th>
1311
+ <th>Best_Score</th>
1312
+ <th>Avg_Score</th>
1313
+ <th>Submissions</th>
1314
+ </tr>
1315
+ </thead>
1316
+ <tbody>
1317
+ """
1318
+
1319
+ body = ""
1320
+ for index, row in team_summary_df.iterrows():
1321
+ # Normalize the row's team name and compare case-insensitively
1322
+ normalized_row_team = _normalize_team_name(row["Team"]).lower()
1323
+ is_user_team = normalized_row_team == normalized_user_team
1324
+ row_class = "class='user-row-highlight'" if is_user_team else ""
1325
+ body += f"""
1326
+ <tr {row_class}>
1327
+ <td>{index}</td>
1328
+ <td>{row['Team']}</td>
1329
+ <td>{(row['Best_Score'] * 100):.2f}%</td>
1330
+ <td>{(row['Avg_Score'] * 100):.2f}%</td>
1331
+ <td>{row['Submissions']}</td>
1332
+ </tr>
1333
+ """
1334
+
1335
+ footer = "</tbody></table>"
1336
+ return header + body + footer
1337
+
1338
+ def _build_individual_html(individual_summary_df, username):
1339
+ """Generates the HTML for the individual leaderboard."""
1340
+ if individual_summary_df is None or individual_summary_df.empty:
1341
+ return "<p style='text-align:center; color:#6b7280; padding-top:20px;'>No individual submissions yet.</p>"
1342
+
1343
+ header = """
1344
+ <table class='leaderboard-html-table'>
1345
+ <thead>
1346
+ <tr>
1347
+ <th>Rank</th>
1348
+ <th>Engineer</th>
1349
+ <th>Best_Score</th>
1350
+ <th>Submissions</th>
1351
+ </tr>
1352
+ </thead>
1353
+ <tbody>
1354
+ """
1355
+
1356
+ body = ""
1357
+ for index, row in individual_summary_df.iterrows():
1358
+ is_user = row["Engineer"] == username
1359
+ row_class = "class='user-row-highlight'" if is_user else ""
1360
+ body += f"""
1361
+ <tr {row_class}>
1362
+ <td>{index}</td>
1363
+ <td>{row['Engineer']}</td>
1364
+ <td>{(row['Best_Score'] * 100):.2f}%</td>
1365
+ <td>{row['Submissions']}</td>
1366
+ </tr>
1367
+ """
1368
+
1369
+ footer = "</tbody></table>"
1370
+ return header + body + footer
1371
+
1372
+
1373
+
1374
+
1375
+ # --- End Helper Functions ---
1376
+
1377
+
1378
+ def generate_competitive_summary(leaderboard_df, team_name, username, last_submission_score, last_rank, submission_count):
1379
+ """
1380
+ Build summaries, HTML, and KPI card.
1381
+
1382
+ Concurrency Note: Uses the team_name parameter directly for team highlighting,
1383
+ NOT os.environ, to prevent cross-user data leakage under concurrent requests.
1384
+
1385
+ Returns (team_html, individual_html, kpi_card_html, new_best_accuracy, new_rank, this_submission_score).
1386
+ """
1387
+ team_summary_df = pd.DataFrame(columns=["Team", "Best_Score", "Avg_Score", "Submissions"])
1388
+ individual_summary_df = pd.DataFrame(columns=["Engineer", "Best_Score", "Submissions"])
1389
+
1390
+ if leaderboard_df is None or leaderboard_df.empty or "accuracy" not in leaderboard_df.columns:
1391
+ return (
1392
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Leaderboard empty.</p>",
1393
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Leaderboard empty.</p>",
1394
+ _build_kpi_card_html(0, 0, 0, 0, 0, is_preview=False, is_pending=False, local_test_accuracy=None),
1395
+ 0.0, 0, 0.0
1396
+ )
1397
+
1398
+ # Team summary
1399
+ if "Team" in leaderboard_df.columns:
1400
+ team_summary_df = (
1401
+ leaderboard_df.groupby("Team")["accuracy"]
1402
+ .agg(Best_Score="max", Avg_Score="mean", Submissions="count")
1403
+ .reset_index()
1404
+ .sort_values("Best_Score", ascending=False)
1405
+ .reset_index(drop=True)
1406
+ )
1407
+ team_summary_df.index = team_summary_df.index + 1
1408
+
1409
+ # Individual summary
1410
+ user_bests = leaderboard_df.groupby("username")["accuracy"].max()
1411
+ user_counts = leaderboard_df.groupby("username")["accuracy"].count()
1412
+ individual_summary_df = pd.DataFrame(
1413
+ {"Engineer": user_bests.index, "Best_Score": user_bests.values, "Submissions": user_counts.values}
1414
+ ).sort_values("Best_Score", ascending=False).reset_index(drop=True)
1415
+ individual_summary_df.index = individual_summary_df.index + 1
1416
+
1417
+ # Get stats for KPI card
1418
+ new_rank = 0
1419
+ new_best_accuracy = 0.0
1420
+ this_submission_score = 0.0
1421
+
1422
+ try:
1423
+ # All submissions for this user
1424
+ user_rows = leaderboard_df[leaderboard_df["username"] == username].copy()
1425
+
1426
+ if not user_rows.empty:
1427
+ # Attempt robust timestamp parsing
1428
+ if "timestamp" in user_rows.columns:
1429
+ parsed_ts = pd.to_datetime(user_rows["timestamp"], errors="coerce")
1430
+
1431
+ if parsed_ts.notna().any():
1432
+ # At least one valid timestamp → use parsed ordering
1433
+ user_rows["__parsed_ts"] = parsed_ts
1434
+ user_rows = user_rows.sort_values("__parsed_ts", ascending=False)
1435
+ this_submission_score = float(user_rows.iloc[0]["accuracy"])
1436
+ else:
1437
+ # All timestamps invalid → assume append order, take last as "latest"
1438
+ this_submission_score = float(user_rows.iloc[-1]["accuracy"])
1439
+ else:
1440
+ # No timestamp column → fallback to last row
1441
+ this_submission_score = float(user_rows.iloc[-1]["accuracy"])
1442
+
1443
+ # Rank & best accuracy (unchanged logic, but make sure we use the same best row)
1444
+ my_rank_row = None
1445
+ # Build individual summary before this block (already done above)
1446
+ my_rank_row = individual_summary_df[individual_summary_df["Engineer"] == username]
1447
+ if not my_rank_row.empty:
1448
+ new_rank = my_rank_row.index[0]
1449
+ new_best_accuracy = float(my_rank_row["Best_Score"].iloc[0])
1450
+
1451
+ except Exception as e:
1452
+ _log(f"Latest submission score extraction failed: {e}")
1453
+
1454
+ # Generate HTML outputs
1455
+ # Concurrency Note: Use team_name parameter directly, not os.environ
1456
+ team_html = _build_team_html(team_summary_df, team_name)
1457
+ individual_html = _build_individual_html(individual_summary_df, username)
1458
+ kpi_card_html = _build_kpi_card_html(
1459
+ this_submission_score, last_submission_score, new_rank, last_rank, submission_count,
1460
+ is_preview=False, is_pending=False, local_test_accuracy=None
1461
+ )
1462
+
1463
+ return team_html, individual_html, kpi_card_html, new_best_accuracy, new_rank, this_submission_score
1464
+
1465
+
1466
+ def get_model_card(model_name):
1467
+ return MODEL_TYPES.get(model_name, {}).get("card", "No description available.")
1468
+
1469
+ def compute_rank_settings(
1470
+ submission_count,
1471
+ current_model,
1472
+ current_complexity,
1473
+ current_feature_set,
1474
+ current_data_size
1475
+ ):
1476
+ """Returns rank gating settings (updated for 1–10 complexity scale)."""
1477
+
1478
+
1479
+ # Always allow all options
1480
+ return {
1481
+ "rank_message": "# 👑 Rank: Lead Engineer\n<p style='font-size:24px; line-height:1.4;'>All tools unlocked — optimize freely!</p>",
1482
+ "model_choices": list(MODEL_TYPES.keys()),
1483
+ "model_value": current_model if current_model in MODEL_TYPES else "The Balanced Generalist",
1484
+ "model_interactive": True,
1485
+ "complexity_max": 10,
1486
+ "complexity_value": current_complexity,
1487
+ "feature_set_choices": FEATURE_SET_ALL_OPTIONS,
1488
+ "feature_set_value": current_feature_set,
1489
+ "feature_set_interactive": True,
1490
+ "data_size_choices": ["Small (20%)", "Medium (60%)", "Large (80%)", "Full (100%)"],
1491
+ "data_size_value": current_data_size if current_data_size in DATA_SIZE_MAP else "Small (20%)",
1492
+ "data_size_interactive": True,
1493
+ }
1494
+
1495
+
1496
+ # Find components by name to yield updates
1497
+ # --- Existing global component placeholders ---
1498
+ submit_button = None
1499
+ submission_feedback_display = None
1500
+ team_leaderboard_display = None
1501
+ individual_leaderboard_display = None
1502
+ last_submission_score_state = None
1503
+ last_rank_state = None
1504
+ best_score_state = None
1505
+ submission_count_state = None
1506
+ rank_message_display = None
1507
+ model_type_radio = None
1508
+ complexity_slider = None
1509
+ feature_set_checkbox = None
1510
+ data_size_radio = None
1511
+ attempts_tracker_display = None
1512
+ team_name_state = None
1513
+ # Login components
1514
+ login_username = None
1515
+ login_password = None
1516
+ login_submit = None
1517
+ login_error = None
1518
+ # Add missing placeholders for auth states (FIX)
1519
+ username_state = None
1520
+ token_state = None
1521
+ first_submission_score_state = None # (already commented as "will be assigned globally")
1522
+ # Add state placeholders for readiness gating and preview tracking
1523
+ readiness_state = None
1524
+ was_preview_state = None
1525
+ kpi_meta_state = None
1526
+ last_seen_ts_state = None # Track last seen user timestamp from leaderboard
1527
+
1528
+
1529
+ def get_or_assign_team(username, token=None):
1530
+ """
1531
+ Get the existing team for a user from the leaderboard, or assign a new random team.
1532
+
1533
+ Queries the playground leaderboard to check if the user has prior submissions with
1534
+ a team assignment. If found, returns that team (most recent if multiple submissions).
1535
+ Otherwise assigns a random team. All team names are normalized for consistency.
1536
+
1537
+ Args:
1538
+ username: str, the username to check for existing team
1539
+ token: str, optional authentication token for leaderboard fetch
1540
+
1541
+ Returns:
1542
+ tuple: (team_name: str, is_new: bool)
1543
+ - team_name: The normalized team name (existing or newly assigned)
1544
+ - is_new: True if newly assigned, False if existing team recovered
1545
+ """
1546
+ try:
1547
+ # Query the leaderboard
1548
+ if playground is None:
1549
+ # Fallback to random assignment if playground not available
1550
+ print("Playground not available, assigning random team")
1551
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
1552
+ return new_team, True
1553
+
1554
+ # Use centralized helper for authenticated leaderboard fetch
1555
+ leaderboard_df = _get_leaderboard_with_optional_token(playground, token)
1556
+
1557
+ # Check if leaderboard has data and Team column
1558
+ if leaderboard_df is not None and not leaderboard_df.empty and "Team" in leaderboard_df.columns:
1559
+ # Filter for this user's submissions
1560
+ user_submissions = leaderboard_df[leaderboard_df["username"] == username]
1561
+
1562
+ if not user_submissions.empty:
1563
+ # Sort by timestamp (most recent first) if timestamp column exists
1564
+ # Use contextlib.suppress for resilient timestamp parsing
1565
+ if "timestamp" in user_submissions.columns:
1566
+ try:
1567
+ # Attempt to coerce timestamp column to datetime and sort descending
1568
+ user_submissions = user_submissions.copy()
1569
+ user_submissions["timestamp"] = pd.to_datetime(user_submissions["timestamp"], errors='coerce')
1570
+ user_submissions = user_submissions.sort_values("timestamp", ascending=False)
1571
+ print(f"Sorted {len(user_submissions)} submissions by timestamp for {username}")
1572
+ except Exception as ts_error:
1573
+ # If timestamp parsing fails, continue with unsorted DataFrame
1574
+ print(f"Warning: Could not sort by timestamp for {username}: {ts_error}")
1575
+
1576
+ # Get the most recent team assignment (first row after sorting)
1577
+ existing_team = user_submissions.iloc[0]["Team"]
1578
+
1579
+ # Check if team value is valid (not null/empty)
1580
+ if pd.notna(existing_team) and existing_team and str(existing_team).strip():
1581
+ normalized_team = _normalize_team_name(existing_team)
1582
+ print(f"Found existing team for {username}: {normalized_team}")
1583
+ return normalized_team, False
1584
+
1585
+ # No existing team found - assign random
1586
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
1587
+ print(f"Assigning new team to {username}: {new_team}")
1588
+ return new_team, True
1589
+
1590
+ except Exception as e:
1591
+ # On any error, fall back to random assignment
1592
+ print(f"Error checking leaderboard for team: {e}")
1593
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
1594
+ print(f"Fallback: assigning random team to {username}: {new_team}")
1595
+ return new_team, True
1596
+
1597
+ def perform_inline_login(username_input, password_input):
1598
+ """
1599
+ Perform inline authentication and return credentials via gr.State updates.
1600
+
1601
+ Concurrency Note: This function NO LONGER stores per-user credentials in
1602
+ os.environ to prevent cross-user data leakage. Authentication state is
1603
+ returned exclusively via gr.State updates (username_state, token_state,
1604
+ team_name_state). Password is never stored server-side.
1605
+
1606
+ Args:
1607
+ username_input: str, the username entered by user
1608
+ password_input: str, the password entered by user
1609
+
1610
+ Returns:
1611
+ dict: Gradio component updates for login UI elements and submit button
1612
+ - On success: hides login form, shows success message, enables submit
1613
+ - On failure: keeps login form visible, shows error with signup link
1614
+ """
1615
+ from aimodelshare.aws import get_aws_token
1616
+
1617
+ # Validate inputs
1618
+ if not username_input or not username_input.strip():
1619
+ error_html = """
1620
+ <div style='background:#fef2f2; padding:12px; border-radius:8px; border-left:4px solid #ef4444; margin-top:12px;'>
1621
+ <p style='margin:0; color:#991b1b; font-weight:500;'>⚠️ Username is required</p>
1622
+ </div>
1623
+ """
1624
+ return {
1625
+ login_username: gr.update(),
1626
+ login_password: gr.update(),
1627
+ login_submit: gr.update(),
1628
+ login_error: gr.update(value=error_html, visible=True),
1629
+ submit_button: gr.update(),
1630
+ submission_feedback_display: gr.update(),
1631
+ team_name_state: gr.update(),
1632
+ username_state: gr.update(),
1633
+ token_state: gr.update()
1634
+ }
1635
+
1636
+ if not password_input or not password_input.strip():
1637
+ error_html = """
1638
+ <div style='background:#fef2f2; padding:12px; border-radius:8px; border-left:4px solid #ef4444; margin-top:12px;'>
1639
+ <p style='margin:0; color:#991b1b; font-weight:500;'>⚠️ Password is required</p>
1640
+ </div>
1641
+ """
1642
+ return {
1643
+ login_username: gr.update(),
1644
+ login_password: gr.update(),
1645
+ login_submit: gr.update(),
1646
+ login_error: gr.update(value=error_html, visible=True),
1647
+ submit_button: gr.update(),
1648
+ submission_feedback_display: gr.update(),
1649
+ team_name_state: gr.update(),
1650
+ username_state: gr.update(),
1651
+ token_state: gr.update()
1652
+ }
1653
+
1654
+ # Concurrency Note: get_aws_token() reads credentials from os.environ, which creates
1655
+ # a race condition in multi-threaded environments. We use _auth_lock to serialize
1656
+ # credential injection, preventing concurrent requests from seeing each other's
1657
+ # credentials. The password is immediately cleared after the auth attempt.
1658
+ #
1659
+ # FUTURE: Ideally get_aws_token() would be refactored to accept credentials as
1660
+ # parameters instead of reading from os.environ. This lock is a workaround.
1661
+ username_clean = username_input.strip()
1662
+
1663
+ # Attempt to get AWS token with serialized credential injection
1664
+ try:
1665
+ with _auth_lock:
1666
+ os.environ["username"] = username_clean
1667
+ os.environ["password"] = password_input.strip() # Only for get_aws_token() call
1668
+ try:
1669
+ token = get_aws_token()
1670
+ finally:
1671
+ # SECURITY: Always clear credentials from environment, even on exception
1672
+ # Also clear stale env vars from previous implementations within the lock
1673
+ # to prevent any race conditions during cleanup
1674
+ os.environ.pop("password", None)
1675
+ os.environ.pop("username", None)
1676
+ os.environ.pop("AWS_TOKEN", None)
1677
+ os.environ.pop("TEAM_NAME", None)
1678
+
1679
+ # Get or assign team for this user with explicit token (already normalized by get_or_assign_team)
1680
+ team_name, is_new_team = get_or_assign_team(username_clean, token=token)
1681
+ # Normalize team name before storing (defensive - already normalized by get_or_assign_team)
1682
+ team_name = _normalize_team_name(team_name)
1683
+
1684
+ # Build success message based on whether team is new or existing
1685
+ if is_new_team:
1686
+ team_message = f"You have been assigned to a new team: <b>{team_name}</b> 🎉"
1687
+ else:
1688
+ team_message = f"Welcome back! You remain on team: <b>{team_name}</b> ✅"
1689
+
1690
+ # Success: hide login form, show success message with team info, enable submit button
1691
+ success_html = f"""
1692
+ <div style='background:#f0fdf4; padding:16px; border-radius:8px; border-left:4px solid #16a34a; margin-top:12px;'>
1693
+ <p style='margin:0; color:#15803d; font-weight:600; font-size:1.1rem;'>✓ Signed in successfully!</p>
1694
+ <p style='margin:8px 0 0 0; color:#166534; font-size:0.95rem;'>
1695
+ {team_message}
1696
+ </p>
1697
+ <p style='margin:8px 0 0 0; color:#166534; font-size:0.95rem;'>
1698
+ Click "Build & Submit Model" again to publish your score.
1699
+ </p>
1700
+ </div>
1701
+ """
1702
+ return {
1703
+ login_username: gr.update(visible=False),
1704
+ login_password: gr.update(visible=False),
1705
+ login_submit: gr.update(visible=False),
1706
+ login_error: gr.update(value=success_html, visible=True),
1707
+ submit_button: gr.update(value="🔬 Build & Submit Model", interactive=True),
1708
+ submission_feedback_display: gr.update(visible=False),
1709
+ team_name_state: gr.update(value=team_name),
1710
+ username_state: gr.update(value=username_clean),
1711
+ token_state: gr.update(value=token)
1712
+ }
1713
+
1714
+ except Exception as e:
1715
+ # Note: Credentials are already cleaned up by the finally block in the try above.
1716
+ # The lock ensures no race condition during cleanup.
1717
+
1718
+ # Authentication failed: show error with signup link
1719
+ error_html = f"""
1720
+ <div style='background:#fef2f2; padding:16px; border-radius:8px; border-left:4px solid #ef4444; margin-top:12px;'>
1721
+ <p style='margin:0; color:#991b1b; font-weight:600; font-size:1.1rem;'>⚠️ Authentication failed</p>
1722
+ <p style='margin:8px 0; color:#7f1d1d; font-size:0.95rem;'>
1723
+ Could not verify your credentials. Please check your username and password.
1724
+ </p>
1725
+ <p style='margin:8px 0 0 0; color:#7f1d1d; font-size:0.95rem;'>
1726
+ <strong>New user?</strong> Create a free account at
1727
+ <a href='https://www.modelshare.ai/login' target='_blank'
1728
+ style='color:#dc2626; text-decoration:underline;'>modelshare.ai/login</a>
1729
+ </p>
1730
+ <details style='margin-top:12px; font-size:0.85rem; color:#7f1d1d;'>
1731
+ <summary style='cursor:pointer;'>Technical details</summary>
1732
+ <pre style='margin-top:8px; padding:8px; background:#fee; border-radius:4px; overflow-x:auto;'>{str(e)}</pre>
1733
+ </details>
1734
+ </div>
1735
+ """
1736
+ return {
1737
+ login_username: gr.update(visible=True),
1738
+ login_password: gr.update(visible=True),
1739
+ login_submit: gr.update(visible=True),
1740
+ login_error: gr.update(value=error_html, visible=True),
1741
+ submit_button: gr.update(),
1742
+ submission_feedback_display: gr.update(),
1743
+ team_name_state: gr.update(),
1744
+ username_state: gr.update(),
1745
+ token_state: gr.update()
1746
+ }
1747
+
1748
+ def run_experiment(
1749
+ model_name_key,
1750
+ complexity_level,
1751
+ feature_set,
1752
+ data_size_str,
1753
+ team_name,
1754
+ last_submission_score,
1755
+ last_rank,
1756
+ submission_count,
1757
+ first_submission_score,
1758
+ best_score,
1759
+ username=None,
1760
+ token=None,
1761
+ readiness_flag=None,
1762
+ was_preview_prev=None,
1763
+ progress=gr.Progress()
1764
+ ):
1765
+ """
1766
+ Core experiment: Uses 'yield' for visual updates and progress bar.
1767
+ Updated with "Look-Before-You-Leap" caching strategy.
1768
+ """
1769
+ # --- COLLISION GUARDS ---
1770
+ # Log types of potentially shadowed names to ensure they refer to component objects, not dicts
1771
+ _log(f"DEBUG guard: types — submit_button={type(submit_button)} submission_feedback_display={type(submission_feedback_display)} kpi_meta_state={type(kpi_meta_state)} was_preview_state={type(was_preview_state)} readiness_flag_param={type(readiness_flag)}")
1772
+
1773
+ # If any of the component names are found as dicts (indicating parameter shadowing), short-circuit
1774
+ if isinstance(submit_button, dict) or isinstance(submission_feedback_display, dict) or isinstance(kpi_meta_state, dict) or isinstance(was_preview_state, dict):
1775
+ error_html = """
1776
+ <div class='kpi-card' style='border-color: #ef4444;'>
1777
+ <h2 style='color: #111827; margin-top:0;'>⚠️ Configuration Error</h2>
1778
+ <div class='kpi-card-body'>
1779
+ <p style='color: #991b1b;'>Parameter shadowing detected. Global component variables were shadowed by local parameters.</p>
1780
+ <p style='color: #7f1d1d; margin-top: 8px;'>Please refresh the page and try again. If the issue persists, contact support.</p>
1781
+ </div>
1782
+ </div>
1783
+ """
1784
+ yield {
1785
+ submission_feedback_display: gr.update(value=error_html, visible=True),
1786
+ submit_button: gr.update(value="🔬 Build & Submit Model", interactive=True)
1787
+ }
1788
+ return
1789
+
1790
+ # Sanitize feature_set: convert dicts/tuples to their string values
1791
+ sanitized_feature_set = []
1792
+ for feat in (feature_set or []):
1793
+ if isinstance(feat, dict):
1794
+ # Extract 'value' key if present, otherwise use string representation
1795
+ sanitized_feature_set.append(feat.get("value", str(feat)))
1796
+ elif isinstance(feat, tuple):
1797
+ # For tuples like ("Label", "value"), take the second element
1798
+ sanitized_feature_set.append(feat[1] if len(feat) > 1 else str(feat))
1799
+ else:
1800
+ # Already a string
1801
+ sanitized_feature_set.append(str(feat))
1802
+ feature_set = sanitized_feature_set
1803
+
1804
+ # Use readiness_flag parameter if provided, otherwise check readiness
1805
+ if readiness_flag is not None:
1806
+ ready = readiness_flag
1807
+ else:
1808
+ ready = _is_ready()
1809
+ _log(f"run_experiment: ready={ready}, username={username}, token_present={token is not None}")
1810
+
1811
+ # Add debug log (optional)
1812
+ _log(f"run_experiment received username={username} token_present={token is not None}")
1813
+ # Concurrency Note: Use provided parameters exclusively, not os.environ.
1814
+ # Default to "Unknown_User" only if no username provided via state.
1815
+ if not username:
1816
+ username = "Unknown_User"
1817
+
1818
+ # Helper to generate the animated HTML
1819
+ def get_status_html(step_num, title, subtitle):
1820
+ return f"""
1821
+ <div class='processing-status'>
1822
+ <span class='processing-icon'>⚙️</span>
1823
+ <div class='processing-text'>Step {step_num}/5: {title}</div>
1824
+ <div class='processing-subtext'>{subtitle}</div>
1825
+ </div>
1826
+ """
1827
+
1828
+ # --- Stage 1: Lock UI and give initial feedback ---
1829
+ progress(0.1, desc="Starting Experiment...")
1830
+ initial_updates = {
1831
+ submit_button: gr.update(value="⏳ Experiment Running...", interactive=False),
1832
+ submission_feedback_display: gr.update(value=get_status_html(1, "Initializing", "Preparing your data ingredients..."), visible=True), # Make sure it's visible
1833
+ login_error: gr.update(visible=False), # Hide login success/error message
1834
+ attempts_tracker_display: gr.update(value=_build_attempts_tracker_html(submission_count))
1835
+ }
1836
+ yield initial_updates
1837
+
1838
+ if not model_name_key or model_name_key not in MODEL_TYPES:
1839
+ model_name_key = DEFAULT_MODEL
1840
+ complexity_level = safe_int(complexity_level, 2)
1841
+
1842
+ log_output = f"▶ New Experiment\nModel: {model_name_key}\n..."
1843
+
1844
+ # Check readiness
1845
+ # If playground is None or not ready, fallback error
1846
+ if playground is None or not ready:
1847
+ settings = compute_rank_settings(
1848
+ submission_count, model_name_key, complexity_level, feature_set, data_size_str
1849
+ )
1850
+
1851
+ error_msg = "<p style='text-align:center; color:red; padding:20px 0;'>"
1852
+ if playground is None:
1853
+ error_msg += "Playground not connected. Please try again later."
1854
+ else:
1855
+ error_msg += "Data still initializing. Please wait a moment and try again."
1856
+ error_msg += "</p>"
1857
+
1858
+ error_kpi_meta = {
1859
+ "was_preview": False, "preview_score": None, "ready_at_run_start": False,
1860
+ "poll_iterations": 0, "local_test_accuracy": None, "this_submission_score": None,
1861
+ "new_best_accuracy": None, "rank": None
1862
+ }
1863
+
1864
+ error_updates = {
1865
+ submission_feedback_display: gr.update(value=error_msg, visible=True),
1866
+ submit_button: gr.update(value="🔬 Build & Submit Model", interactive=True),
1867
+ team_leaderboard_display: _build_skeleton_leaderboard(rows=6, is_team=True),
1868
+ individual_leaderboard_display: _build_skeleton_leaderboard(rows=6, is_team=False),
1869
+ last_submission_score_state: last_submission_score,
1870
+ last_rank_state: last_rank,
1871
+ best_score_state: best_score,
1872
+ submission_count_state: submission_count,
1873
+ first_submission_score_state: first_submission_score,
1874
+ rank_message_display: settings["rank_message"],
1875
+ model_type_radio: gr.update(choices=settings["model_choices"], value=settings["model_value"], interactive=settings["model_interactive"]),
1876
+ complexity_slider: gr.update(minimum=1, maximum=settings["complexity_max"], value=settings["complexity_value"]),
1877
+ feature_set_checkbox: gr.update(choices=settings["feature_set_choices"], value=settings["feature_set_value"], interactive=settings["feature_set_interactive"]),
1878
+ data_size_radio: gr.update(choices=settings["data_size_choices"], value=settings["data_size_value"], interactive=settings["data_size_interactive"]),
1879
+ login_username: gr.update(visible=False),
1880
+ login_password: gr.update(visible=False),
1881
+ login_submit: gr.update(visible=False),
1882
+ login_error: gr.update(visible=False),
1883
+ attempts_tracker_display: gr.update(value=_build_attempts_tracker_html(submission_count)),
1884
+ was_preview_state: False,
1885
+ kpi_meta_state: error_kpi_meta,
1886
+ last_seen_ts_state: None
1887
+ }
1888
+ yield error_updates
1889
+ return
1890
+
1891
+ try:
1892
+ # --- Stage 2: Smart Build (Cache vs Train) ---
1893
+ progress(0.3, desc="Building Model...")
1894
+
1895
+ # 1. Generate Cache Key (Matches format in precompute_cache.py)
1896
+ # Key: "ModelName|Complexity|DataSize|SortedFeatures"
1897
+ sanitized_features = sorted([str(f) for f in feature_set])
1898
+ feature_key = ",".join(sanitized_features)
1899
+ cache_key = f"{model_name_key}|{complexity_level}|{data_size_str}|{feature_key}"
1900
+
1901
+ # 2. Check Cache
1902
+ cached_predictions = get_cached_prediction(cache_key)
1903
+
1904
+ # Initialize submission variables
1905
+ predictions = None
1906
+ tuned_model = None
1907
+ preprocessor = None
1908
+
1909
+ if cached_predictions:
1910
+ # === FAST PATH (Zero CPU) ===
1911
+ _log(f"⚡ CACHE HIT: {cache_key}")
1912
+ yield {
1913
+ submission_feedback_display: gr.update(value=get_status_html(2, "Training Model", "⚡ The machine is learning from history..."), visible=True),
1914
+ login_error: gr.update(visible=False)
1915
+ }
1916
+
1917
+ # --- DECOMPRESSION STEP (Vital) ---
1918
+ # If string "01010...", convert to [0, 1, 0, 1...]
1919
+ if isinstance(cached_predictions, str):
1920
+ predictions = [int(c) for c in cached_predictions]
1921
+ else:
1922
+ predictions = cached_predictions
1923
+
1924
+ # Pass None to submit_model to skip training overhead validation
1925
+ tuned_model = None
1926
+ preprocessor = None
1927
+
1928
+
1929
+ else:
1930
+ # === CACHE MISS (Training Disabled) ===
1931
+ # This ensures we NEVER run heavy training code in production.
1932
+ msg = f"❌ CACHE MISS: {cache_key}"
1933
+ _log(msg)
1934
+
1935
+ # User-friendly error message
1936
+ error_html = f"""
1937
+ <div style='background:#fee2e2; padding:16px; border-radius:8px; border:2px solid #ef4444; color:#991b1b; text-align:center;'>
1938
+ <h3 style='margin:0;'>⚠️ Configuration Not Found</h3>
1939
+ <p style='margin:8px 0;'>This specific combination of settings was not found in our pre-computed database.</p>
1940
+ <p style='font-size:0.9em;'>To ensure system stability, real-time training is disabled. Please adjust your settings (e.g., change the Data Size or Model Strategy) and try again.</p>
1941
+ </div>
1942
+ """
1943
+
1944
+ yield {
1945
+ submission_feedback_display: gr.update(value=error_html, visible=True),
1946
+ submit_button: gr.update(value="🔬 Build & Submit Model", interactive=True),
1947
+ login_error: gr.update(visible=False)
1948
+ }
1949
+ return # <--- CRITICAL: Stop execution here.
1950
+
1951
+
1952
+ # --- Stage 3: Submit (API Call 1) ---
1953
+ # AUTHENTICATION GATE: Check for token before submission
1954
+ if token is None:
1955
+ # User not authenticated - compute preview score and show login prompt
1956
+ progress(0.6, desc="Computing Preview Score...")
1957
+
1958
+ # We need to calculate accuracy for the preview card
1959
+ from sklearn.metrics import accuracy_score
1960
+ # Ensure predictions are in correct format (list or array)
1961
+ if isinstance(predictions, list):
1962
+ # Cached predictions are lists
1963
+ preds_array = np.array(predictions)
1964
+ else:
1965
+ preds_array = predictions
1966
+
1967
+ preview_score = accuracy_score(Y_TEST, preds_array)
1968
+
1969
+ preview_kpi_meta = {
1970
+ "was_preview": True, "preview_score": preview_score, "ready_at_run_start": ready,
1971
+ "poll_iterations": 0, "local_test_accuracy": preview_score,
1972
+ "this_submission_score": None, "new_best_accuracy": None, "rank": None
1973
+ }
1974
+
1975
+ # 1. Generate the styled preview card
1976
+ preview_card_html = _build_kpi_card_html(
1977
+ new_score=preview_score, last_score=0, new_rank=0, last_rank=0,
1978
+ submission_count=-1, is_preview=True, is_pending=False, local_test_accuracy=None
1979
+ )
1980
+
1981
+ # 2. Inject login text
1982
+ login_prompt_text_html = build_login_prompt_html()
1983
+ closing_div_index = preview_card_html.rfind("</div>")
1984
+ if closing_div_index != -1:
1985
+ combined_html = preview_card_html[:closing_div_index] + login_prompt_text_html + "</div>"
1986
+ else:
1987
+ combined_html = preview_card_html + login_prompt_text_html
1988
+
1989
+ settings = compute_rank_settings(submission_count, model_name_key, complexity_level, feature_set, data_size_str)
1990
+
1991
+ gate_updates = {
1992
+ submission_feedback_display: gr.update(value=combined_html, visible=True),
1993
+ submit_button: gr.update(value="Sign In Required", interactive=False),
1994
+ login_username: gr.update(visible=True), login_password: gr.update(visible=True),
1995
+ login_submit: gr.update(visible=True), login_error: gr.update(value="", visible=False),
1996
+ team_leaderboard_display: _build_skeleton_leaderboard(rows=6, is_team=True),
1997
+ individual_leaderboard_display: _build_skeleton_leaderboard(rows=6, is_team=False),
1998
+ last_submission_score_state: last_submission_score, last_rank_state: last_rank,
1999
+ best_score_state: best_score, submission_count_state: submission_count,
2000
+ first_submission_score_state: first_submission_score,
2001
+ rank_message_display: settings["rank_message"],
2002
+ model_type_radio: gr.update(choices=settings["model_choices"], value=settings["model_value"], interactive=settings["model_interactive"]),
2003
+ complexity_slider: gr.update(minimum=1, maximum=settings["complexity_max"], value=settings["complexity_value"]),
2004
+ feature_set_checkbox: gr.update(choices=settings["feature_set_choices"], value=settings["feature_set_value"], interactive=settings["feature_set_interactive"]),
2005
+ data_size_radio: gr.update(choices=settings["data_size_choices"], value=settings["data_size_value"], interactive=settings["data_size_interactive"]),
2006
+ attempts_tracker_display: gr.update(value=_build_attempts_tracker_html(submission_count)),
2007
+ was_preview_state: True, kpi_meta_state: preview_kpi_meta, last_seen_ts_state: None
2008
+ }
2009
+ yield gate_updates
2010
+ return # Stop here
2011
+
2012
+ # --- ATTEMPT LIMIT CHECK ---
2013
+ if submission_count >= ATTEMPT_LIMIT:
2014
+ limit_warning_html = f"""
2015
+ <div class='kpi-card' style='border-color: #ef4444;'>
2016
+ <h2 style='color: #111827; margin-top:0;'>🛑 Submission Limit Reached</h2>
2017
+ <div class='kpi-card-body'>
2018
+ <div class='kpi-metric-box'>
2019
+ <p class='kpi-label'>Attempts Used</p>
2020
+ <p class='kpi-score' style='color: #ef4444;'>{ATTEMPT_LIMIT} / {ATTEMPT_LIMIT}</p>
2021
+ </div>
2022
+ </div>
2023
+ <div style='margin-top: 16px; background:#fef2f2; padding:16px; border-radius:12px; text-align:left; font-size:0.98rem; line-height:1.4;'>
2024
+ <p style='margin:0; color:#991b1b;'><b>Nice Work!</b> Scroll down to "Finish and Reflect".</p>
2025
+ </div>
2026
+ </div>"""
2027
+ settings = compute_rank_settings(submission_count, model_name_key, complexity_level, feature_set, data_size_str)
2028
+ limit_reached_updates = {
2029
+ submission_feedback_display: gr.update(value=limit_warning_html, visible=True),
2030
+ submit_button: gr.update(value="🛑 Submission Limit Reached", interactive=False),
2031
+ model_type_radio: gr.update(interactive=False), complexity_slider: gr.update(interactive=False),
2032
+ feature_set_checkbox: gr.update(interactive=False), data_size_radio: gr.update(interactive=False),
2033
+ attempts_tracker_display: gr.update(value=f"<div style='text-align:center; padding:8px; margin:8px 0; background:#fef2f2; border-radius:8px; border:1px solid #ef4444;'><p style='margin:0; color:#991b1b; font-weight:600;'>🛑 Attempts used: {ATTEMPT_LIMIT}/{ATTEMPT_LIMIT}</p></div>"),
2034
+ team_leaderboard_display: team_leaderboard_display, individual_leaderboard_display: individual_leaderboard_display,
2035
+ last_submission_score_state: last_submission_score, last_rank_state: last_rank,
2036
+ best_score_state: best_score, submission_count_state: submission_count,
2037
+ first_submission_score_state: first_submission_score, rank_message_display: settings["rank_message"],
2038
+ login_username: gr.update(visible=False), login_password: gr.update(visible=False),
2039
+ login_submit: gr.update(visible=False), login_error: gr.update(visible=False),
2040
+ was_preview_state: False, kpi_meta_state: {}, last_seen_ts_state: None
2041
+ }
2042
+ yield limit_reached_updates
2043
+ return
2044
+
2045
+ progress(0.5, desc="Submitting to Cloud...")
2046
+ yield {
2047
+ submission_feedback_display: gr.update(value=get_status_html(3, "Submitting", "Sending model to the competition server..."), visible=True),
2048
+ login_error: gr.update(visible=False)
2049
+ }
2050
+
2051
+ description = f"{model_name_key} (Cplx:{complexity_level} Size:{data_size_str})"
2052
+ tags = f"team:{team_name},model:{model_name_key}"
2053
+
2054
+ # 1. FETCH BASELINE
2055
+ baseline_leaderboard_df = _get_leaderboard_with_optional_token(playground, token)
2056
+
2057
+ from sklearn.metrics import accuracy_score
2058
+ # Ensure correct type for local accuracy calc
2059
+ if isinstance(predictions, list):
2060
+ local_accuracy_preds = np.array(predictions)
2061
+ else:
2062
+ local_accuracy_preds = predictions
2063
+ local_test_accuracy = accuracy_score(Y_TEST, local_accuracy_preds)
2064
+
2065
+ # 2. SUBMIT & CAPTURE ACCURACY
2066
+ def _submit():
2067
+ # If using cache (tuned_model is None), we pass None for model/preprocessor
2068
+ # and explicitly pass predictions.
2069
+ return playground.submit_model(
2070
+ model=tuned_model,
2071
+ preprocessor=preprocessor,
2072
+ prediction_submission=predictions,
2073
+ input_dict={'description': description, 'tags': tags},
2074
+ custom_metadata={'Team': team_name, 'Moral_Compass': 0},
2075
+ token=token,
2076
+ return_metrics=["accuracy"]
2077
+ )
2078
+
2079
+ try:
2080
+ submit_result = _retry_with_backoff(_submit, description="model submission")
2081
+ if isinstance(submit_result, tuple) and len(submit_result) == 3:
2082
+ _, _, metrics = submit_result
2083
+ if metrics and "accuracy" in metrics and metrics["accuracy"] is not None:
2084
+ this_submission_score = float(metrics["accuracy"])
2085
+ else:
2086
+ this_submission_score = local_test_accuracy
2087
+ else:
2088
+ this_submission_score = local_test_accuracy
2089
+ except Exception as e:
2090
+ _log(f"Submission return parsing failed: {e}. Using local accuracy.")
2091
+ this_submission_score = local_test_accuracy
2092
+
2093
+ _log(f"Submission successful. Server Score: {this_submission_score}")
2094
+
2095
+ try:
2096
+ # Short timeout to trigger the lambda without hanging the UI
2097
+ _log("Triggering backend merge...")
2098
+ playground.get_leaderboard(token=token)
2099
+ except Exception:
2100
+ # We ignore errors here because the 'submit_model' post
2101
+ # already succeeded. This is just a cleanup task.
2102
+ pass
2103
+ # -------------------------------------------------------------------------
2104
+
2105
+ # Immediately increment submission count...
2106
+ new_submission_count = submission_count + 1
2107
+ new_first_submission_score = first_submission_score
2108
+ if submission_count == 0 and first_submission_score is None:
2109
+ new_first_submission_score = this_submission_score
2110
+
2111
+ # --- Stage 4: Local Rank Calculation (Optimistic) ---
2112
+ progress(0.9, desc="Calculating Rank...")
2113
+
2114
+ # 3. SIMULATE UPDATED LEADERBOARD
2115
+ simulated_df = baseline_leaderboard_df.copy() if baseline_leaderboard_df is not None else pd.DataFrame()
2116
+
2117
+ # We use pd.Timestamp.now() to ensure pandas sorting logic sees this as the absolute latest
2118
+ new_row = pd.DataFrame([{
2119
+ "username": username,
2120
+ "accuracy": this_submission_score,
2121
+ "Team": team_name,
2122
+ "timestamp": pd.Timestamp.now(),
2123
+ "version": "latest"
2124
+ }])
2125
+
2126
+ if not simulated_df.empty:
2127
+ simulated_df = pd.concat([simulated_df, new_row], ignore_index=True)
2128
+ else:
2129
+ simulated_df = new_row
2130
+
2131
+ # 4. GENERATE TABLES (Use helper for tables only)
2132
+ # We ignore the kpi_card return from this function because it might use internal sorting
2133
+ # that doesn't respect our new row perfectly.
2134
+ team_html, individual_html, _, new_best_accuracy, new_rank, _ = generate_competitive_summary(
2135
+ simulated_df, team_name, username, last_submission_score, last_rank, submission_count
2136
+ )
2137
+
2138
+ # 5. GENERATE KPI CARD EXPLICITLY (The Authority Fix)
2139
+ # We manually build the card using the score we KNOW we just got.
2140
+ kpi_card_html = _build_kpi_card_html(
2141
+ new_score=this_submission_score,
2142
+ last_score=last_submission_score,
2143
+ new_rank=new_rank,
2144
+ last_rank=last_rank,
2145
+ submission_count=submission_count,
2146
+ is_preview=False,
2147
+ is_pending=False
2148
+ )
2149
+
2150
+ # --- Stage 5: Final UI Update ---
2151
+ progress(1.0, desc="Complete!")
2152
+
2153
+ success_kpi_meta = {
2154
+ "was_preview": False, "preview_score": None, "ready_at_run_start": ready,
2155
+ "poll_iterations": 0, "local_test_accuracy": local_test_accuracy,
2156
+ "this_submission_score": this_submission_score, "new_best_accuracy": new_best_accuracy,
2157
+ "rank": new_rank, "pending": False, "optimistic_fallback": True
2158
+ }
2159
+
2160
+ settings = compute_rank_settings(new_submission_count, model_name_key, complexity_level, feature_set, data_size_str)
2161
+
2162
+ # -------------------------------------------------------------------------
2163
+ # NEW LOGIC: Check for Limit Reached immediately AFTER this submission
2164
+ # -------------------------------------------------------------------------
2165
+ limit_reached = new_submission_count >= ATTEMPT_LIMIT
2166
+
2167
+ # Prepare the UI state based on whether limit is reached
2168
+ if limit_reached:
2169
+ # 1. Append the Limit Warning HTML *below* the Result Card
2170
+ limit_html = f"""
2171
+ <div style='margin-top: 16px; border: 2px solid #ef4444; background:#fef2f2; padding:16px; border-radius:12px; text-align:left;'>
2172
+ <h3 style='margin:0 0 8px 0; color:#991b1b;'>🛑 Submission Limit Reached ({ATTEMPT_LIMIT}/{ATTEMPT_LIMIT})</h3>
2173
+ <p style='margin:0; color:#7f1d1d; line-height:1.4;'>
2174
+ <b>You have used all your attempts for this session.</b><br>
2175
+ Review your final results above, then scroll down to "Finish and Reflect" to continue.
2176
+ </p>
2177
+ </div>
2178
+ """
2179
+ final_html_display = kpi_card_html + limit_html
2180
+
2181
+ # 2. Disable all controls
2182
+ button_update = gr.update(value="🛑 Limit Reached", interactive=False)
2183
+ interactive_state = False
2184
+ tracker_html = f"<div style='text-align:center; padding:8px; margin:8px 0; background:#fef2f2; border-radius:8px; border:1px solid #ef4444;'><p style='margin:0; color:#991b1b; font-weight:600;'>🛑 Attempts used: {ATTEMPT_LIMIT}/{ATTEMPT_LIMIT} (Max)</p></div>"
2185
+
2186
+ else:
2187
+ # Normal State: Show just the result card and keep controls active
2188
+ final_html_display = kpi_card_html
2189
+ button_update = gr.update(value="🔬 Build & Submit Model", interactive=True)
2190
+ interactive_state = True
2191
+ tracker_html = _build_attempts_tracker_html(new_submission_count)
2192
+
2193
+ # -------------------------------------------------------------------------
2194
+
2195
+ final_updates = {
2196
+ submission_feedback_display: gr.update(value=final_html_display, visible=True),
2197
+ team_leaderboard_display: team_html,
2198
+ individual_leaderboard_display: individual_html,
2199
+ last_submission_score_state: this_submission_score,
2200
+ last_rank_state: new_rank,
2201
+ best_score_state: new_best_accuracy,
2202
+ submission_count_state: new_submission_count,
2203
+ first_submission_score_state: new_first_submission_score,
2204
+ rank_message_display: settings["rank_message"],
2205
+
2206
+ # Apply the interactive state calculated above
2207
+ model_type_radio: gr.update(choices=settings["model_choices"], value=settings["model_value"], interactive=(settings["model_interactive"] and interactive_state)),
2208
+ complexity_slider: gr.update(minimum=1, maximum=settings["complexity_max"], value=settings["complexity_value"], interactive=interactive_state),
2209
+ feature_set_checkbox: gr.update(choices=settings["feature_set_choices"], value=settings["feature_set_value"], interactive=(settings["feature_set_interactive"] and interactive_state)),
2210
+ data_size_radio: gr.update(choices=settings["data_size_choices"], value=settings["data_size_value"], interactive=(settings["data_size_interactive"] and interactive_state)),
2211
+
2212
+ submit_button: button_update,
2213
+
2214
+ login_username: gr.update(visible=False), login_password: gr.update(visible=False),
2215
+ login_submit: gr.update(visible=False), login_error: gr.update(visible=False),
2216
+ attempts_tracker_display: gr.update(value=tracker_html),
2217
+ was_preview_state: False,
2218
+ kpi_meta_state: success_kpi_meta,
2219
+ last_seen_ts_state: time.time()
2220
+ }
2221
+ yield final_updates
2222
+
2223
+ except Exception as e:
2224
+ error_msg = f"ERROR: {e}"
2225
+ _log(f"Exception in run_experiment: {error_msg}")
2226
+ settings = compute_rank_settings(
2227
+ submission_count, model_name_key, complexity_level, feature_set, data_size_str
2228
+ )
2229
+
2230
+ exception_kpi_meta = {
2231
+ "was_preview": False, "preview_score": None, "ready_at_run_start": ready if 'ready' in locals() else False,
2232
+ "poll_iterations": 0, "local_test_accuracy": None, "this_submission_score": None,
2233
+ "new_best_accuracy": None, "rank": None, "error": str(e)
2234
+ }
2235
+
2236
+ error_updates = {
2237
+ submission_feedback_display: gr.update(
2238
+ f"<p style='text-align:center; color:red; padding:20px 0;'>An error occurred: {error_msg}</p>", visible=True
2239
+ ),
2240
+ team_leaderboard_display: f"<p style='text-align:center; color:red; padding-top:20px;'>An error occurred: {error_msg}</p>",
2241
+ individual_leaderboard_display: f"<p style='text-align:center; color:red; padding-top:20px;'>An error occurred: {error_msg}</p>",
2242
+ last_submission_score_state: last_submission_score,
2243
+ last_rank_state: last_rank,
2244
+ best_score_state: best_score,
2245
+ submission_count_state: submission_count,
2246
+ first_submission_score_state: first_submission_score,
2247
+ rank_message_display: settings["rank_message"],
2248
+ model_type_radio: gr.update(choices=settings["model_choices"], value=settings["model_value"], interactive=settings["model_interactive"]),
2249
+ complexity_slider: gr.update(minimum=1, maximum=settings["complexity_max"], value=settings["complexity_value"]),
2250
+ feature_set_checkbox: gr.update(choices=settings["feature_set_choices"], value=settings["feature_set_value"], interactive=settings["feature_set_interactive"]),
2251
+ data_size_radio: gr.update(choices=settings["data_size_choices"], value=settings["data_size_value"], interactive=settings["data_size_interactive"]),
2252
+ submit_button: gr.update(value="🔬 Build & Submit Model", interactive=True),
2253
+ login_username: gr.update(visible=False),
2254
+ login_password: gr.update(visible=False),
2255
+ login_submit: gr.update(visible=False),
2256
+ login_error: gr.update(visible=False),
2257
+ attempts_tracker_display: gr.update(value=_build_attempts_tracker_html(submission_count)),
2258
+ was_preview_state: False,
2259
+ kpi_meta_state: exception_kpi_meta,
2260
+ last_seen_ts_state: None
2261
+ }
2262
+ yield error_updates
2263
+
2264
+ def on_initial_load(username, token=None, team_name=""):
2265
+ """
2266
+ Updated to show "Welcome & CTA" if the SPECIFIC USER has 0 submissions,
2267
+ even if the leaderboard/team already has data from others.
2268
+ """
2269
+ initial_ui = compute_rank_settings(
2270
+ 0, DEFAULT_MODEL, 2, DEFAULT_FEATURE_SET, DEFAULT_DATA_SIZE
2271
+ )
2272
+
2273
+ # 1. Prepare the Welcome HTML
2274
+ display_team = team_name if team_name else "Your Team"
2275
+
2276
+ welcome_html = f"""
2277
+ <div style='text-align:center; padding: 30px 20px;'>
2278
+ <div style='font-size: 3rem; margin-bottom: 10px;'>👋</div>
2279
+ <h3 style='margin: 0 0 8px 0; color: #111827; font-size: 1.5rem;'>Welcome to <b>{display_team}</b>!</h3>
2280
+ <p style='font-size: 1.1rem; color: #4b5563; margin: 0 0 20px 0;'>
2281
+ Your team is waiting for your help to improve the AI.
2282
+ </p>
2283
+
2284
+ <div style='background:#eff6ff; padding:16px; border-radius:12px; border:2px solid #bfdbfe; display:inline-block;'>
2285
+ <p style='margin:0; color:#1e40af; font-weight:bold; font-size:1.1rem;'>
2286
+ 👈 Click "Build & Submit Model" to Start Playing!
2287
+ </p>
2288
+ </div>
2289
+ </div>
2290
+ """
2291
+
2292
+ # Check background init
2293
+ with INIT_LOCK:
2294
+ background_ready = INIT_FLAGS["leaderboard"]
2295
+
2296
+ should_attempt_fetch = background_ready or (token is not None)
2297
+ full_leaderboard_df = None
2298
+
2299
+ if should_attempt_fetch:
2300
+ try:
2301
+ if playground:
2302
+ full_leaderboard_df = _get_leaderboard_with_optional_token(playground, token)
2303
+ except Exception as e:
2304
+ print(f"Error on initial load fetch: {e}")
2305
+ full_leaderboard_df = None
2306
+
2307
+ # -------------------------------------------------------------------------
2308
+ # LOGIC UPDATE: Check if THIS user has submitted anything
2309
+ # -------------------------------------------------------------------------
2310
+ user_has_submitted = False
2311
+ if full_leaderboard_df is not None and not full_leaderboard_df.empty:
2312
+ if "username" in full_leaderboard_df.columns and username:
2313
+ # Check if the username exists in the dataframe
2314
+ user_has_submitted = username in full_leaderboard_df["username"].values
2315
+
2316
+ # Decision Logic
2317
+ if not user_has_submitted:
2318
+ # CASE 1: New User (or first time loading session) -> FORCE WELCOME
2319
+ # regardless of whether the leaderboard has other people's data.
2320
+ team_html = welcome_html
2321
+ individual_html = "<p style='text-align:center; color:#6b7280; padding-top:40px;'>Submit your model to see where you rank!</p>"
2322
+
2323
+ elif full_leaderboard_df is None or full_leaderboard_df.empty:
2324
+ # CASE 2: Returning user, but data fetch failed -> Show Skeleton
2325
+ team_html = _build_skeleton_leaderboard(rows=6, is_team=True)
2326
+ individual_html = _build_skeleton_leaderboard(rows=6, is_team=False)
2327
+
2328
+ else:
2329
+ # CASE 3: Returning user WITH data -> Show Real Tables
2330
+ try:
2331
+ team_html, individual_html, _, _, _, _ = generate_competitive_summary(
2332
+ full_leaderboard_df,
2333
+ team_name,
2334
+ username,
2335
+ 0, 0, -1
2336
+ )
2337
+ except Exception as e:
2338
+ print(f"Error generating summary HTML: {e}")
2339
+ team_html = "<p style='text-align:center; color:red; padding-top:20px;'>Error rendering leaderboard.</p>"
2340
+ individual_html = "<p style='text-align:center; color:red; padding-top:20px;'>Error rendering leaderboard.</p>"
2341
+
2342
+ return (
2343
+ get_model_card(DEFAULT_MODEL),
2344
+ team_html,
2345
+ individual_html,
2346
+ initial_ui["rank_message"],
2347
+ gr.update(choices=initial_ui["model_choices"], value=initial_ui["model_value"], interactive=initial_ui["model_interactive"]),
2348
+ gr.update(minimum=1, maximum=initial_ui["complexity_max"], value=initial_ui["complexity_value"]),
2349
+ gr.update(choices=initial_ui["feature_set_choices"], value=initial_ui["feature_set_value"], interactive=initial_ui["feature_set_interactive"]),
2350
+ gr.update(choices=initial_ui["data_size_choices"], value=initial_ui["data_size_value"], interactive=initial_ui["data_size_interactive"]),
2351
+ )
2352
+
2353
+
2354
+ # -------------------------------------------------------------------------
2355
+ # Conclusion helpers (dark/light mode aware)
2356
+ # -------------------------------------------------------------------------
2357
+ def build_final_conclusion_html(best_score, submissions, rank, first_score, feature_set):
2358
+ """
2359
+ Build the final conclusion HTML with performance summary.
2360
+ Colors are handled via CSS classes so that light/dark mode work correctly.
2361
+ """
2362
+ unlocked_tiers = min(3, max(0, submissions - 1)) # 0..3
2363
+ tier_names = ["Trainee", "Junior", "Senior", "Lead"]
2364
+ reached = tier_names[: unlocked_tiers + 1]
2365
+ tier_line = " → ".join([f"{t}{' ✅' if t in reached else ''}" for t in tier_names])
2366
+
2367
+ improvement = (best_score - first_score) if (first_score is not None and submissions > 1) else 0.0
2368
+ strong_predictors = {"age", "length_of_stay", "priors_count", "age_cat"}
2369
+ strong_used = [f for f in feature_set if f in strong_predictors]
2370
+
2371
+ ethical_note = (
2372
+ "You unlocked powerful predictors. Consider: Would removing demographic fields change fairness? "
2373
+ "In the next section we will begin to investigate this question further."
2374
+ )
2375
+
2376
+ # Tailor message for very few submissions
2377
+ tip_html = ""
2378
+ if submissions < 2:
2379
+ tip_html = """
2380
+ <div class="final-conclusion-tip">
2381
+ <b>Tip:</b> Try at least 2–3 submissions changing ONE setting at a time to see clear cause/effect.
2382
+ </div>
2383
+ """
2384
+
2385
+ # Add note if user reached the attempt cap
2386
+ attempt_cap_html = ""
2387
+ if submissions >= ATTEMPT_LIMIT:
2388
+ attempt_cap_html = f"""
2389
+ <div class="final-conclusion-attempt-cap">
2390
+ <p style="margin:0;">
2391
+ <b>📊 Attempt Limit Reached:</b> You used all {ATTEMPT_LIMIT} allowed submission attempts for this session.
2392
+ We will open up submissions again after you complete some new activities next.
2393
+ </p>
2394
+ </div>
2395
+ """
2396
+
2397
+ return f"""
2398
+ <div class="final-conclusion-root">
2399
+ <h1 class="final-conclusion-title">🎉 Engineering Phase Complete</h1>
2400
+ <div class="final-conclusion-card">
2401
+ <h2 class="final-conclusion-subtitle">Your Performance Snapshot</h2>
2402
+ <ul class="final-conclusion-list">
2403
+ <li>🏁 <b>Best Accuracy:</b> {(best_score * 100):.2f}%</li>
2404
+ <li>📊 <b>Rank Achieved:</b> {('#' + str(rank)) if rank > 0 else '—'}</li>
2405
+ <li>🔁 <b>Submissions Made This Session:</b> {submissions}{' / ' + str(ATTEMPT_LIMIT) if submissions >= ATTEMPT_LIMIT else ''}</li>
2406
+ <li>🧗 <b>Improvement Over First Score This Session:</b> {(improvement * 100):+.2f}</li>
2407
+ <li>🎖️ <b>Tier Progress:</b> {tier_line}</li>
2408
+ <li>🧪 <b>Strong Predictors Used:</b> {len(strong_used)} ({', '.join(strong_used) if strong_used else 'None yet'})</li>
2409
+ </ul>
2410
+
2411
+ {tip_html}
2412
+
2413
+ <div class="final-conclusion-ethics">
2414
+ <p style="margin:0;"><b>Ethical Reflection:</b> {ethical_note}</p>
2415
+ </div>
2416
+
2417
+ {attempt_cap_html}
2418
+
2419
+ <hr class="final-conclusion-divider" />
2420
+
2421
+ <div class="final-conclusion-next">
2422
+ <h2>➡️ Next: Real-World Consequences</h2>
2423
+ <p>Scroll below this app to continue. You'll examine how models like yours shape judicial outcomes.</p>
2424
+ <h1 class="final-conclusion-scroll">👇 SCROLL DOWN 👇</h1>
2425
+ </div>
2426
+ </div>
2427
+ </div>
2428
+ """
2429
+
2430
+
2431
+
2432
+ def build_conclusion_from_state(best_score, submissions, rank, first_score, feature_set):
2433
+ return build_final_conclusion_html(best_score, submissions, rank, first_score, feature_set)
2434
+ def create_model_building_game_en_final_app(theme_primary_hue: str = "indigo") -> "gr.Blocks":
2435
+ """
2436
+ Create (but do not launch) the model building game app.
2437
+ """
2438
+ start_background_init()
2439
+
2440
+ # Add missing globals (FIX)
2441
+ global submit_button, submission_feedback_display, team_leaderboard_display
2442
+ global individual_leaderboard_display, last_submission_score_state, last_rank_state
2443
+ global best_score_state, submission_count_state, first_submission_score_state
2444
+ global rank_message_display, model_type_radio, complexity_slider
2445
+ global feature_set_checkbox, data_size_radio
2446
+ global login_username, login_password, login_submit, login_error
2447
+ global attempts_tracker_display, team_name_state
2448
+ global username_state, token_state # <-- Added
2449
+ global readiness_state, was_preview_state, kpi_meta_state # <-- Added for parameter shadowing guards
2450
+ global last_seen_ts_state # <-- Added for timestamp tracking
2451
+
2452
+ css = """
2453
+ /* ------------------------------
2454
+ Shared Design Tokens (local)
2455
+ ------------------------------ */
2456
+
2457
+ /* We keep everything driven by Gradio theme vars:
2458
+ --body-background-fill, --body-text-color, --secondary-text-color,
2459
+ --border-color-primary, --block-background-fill, --color-accent,
2460
+ --shadow-drop, --prose-background-fill
2461
+ */
2462
+
2463
+ :root {
2464
+ --slide-radius-md: 12px;
2465
+ --slide-radius-lg: 16px;
2466
+ --slide-radius-xl: 18px;
2467
+ --slide-spacing-lg: 24px;
2468
+
2469
+ /* Local, non-brand tokens built *on top of* theme vars */
2470
+ --card-bg-soft: var(--block-background-fill);
2471
+ --card-bg-strong: var(--prose-background-fill, var(--block-background-fill));
2472
+ --card-border-subtle: var(--border-color-primary);
2473
+ --accent-strong: var(--color-accent);
2474
+ --text-main: var(--body-text-color);
2475
+ --text-muted: var(--secondary-text-color);
2476
+ }
2477
+
2478
+ /* ------------------------------------------------------------------
2479
+ Base Layout Helpers
2480
+ ------------------------------------------------------------------ */
2481
+
2482
+ .slide-content {
2483
+ max-width: 900px;
2484
+ margin-left: auto;
2485
+ margin-right: auto;
2486
+ }
2487
+
2488
+ /* Shared card-like panels used throughout slides */
2489
+ .panel-box {
2490
+ background: var(--card-bg-soft);
2491
+ padding: 20px;
2492
+ border-radius: var(--slide-radius-lg);
2493
+ border: 2px solid var(--card-border-subtle);
2494
+ margin-bottom: 18px;
2495
+ color: var(--text-main);
2496
+ box-shadow: var(--shadow-drop, 0 2px 4px rgba(0,0,0,0.04));
2497
+ }
2498
+
2499
+ .leaderboard-box {
2500
+ background: var(--card-bg-soft);
2501
+ padding: 20px;
2502
+ border-radius: var(--slide-radius-lg);
2503
+ border: 1px solid var(--card-border-subtle);
2504
+ margin-top: 12px;
2505
+ color: var(--text-main);
2506
+ }
2507
+
2508
+ /* For “explanatory UI” scaffolding */
2509
+ .mock-ui-box {
2510
+ background: var(--card-bg-strong);
2511
+ border: 2px solid var(--card-border-subtle);
2512
+ padding: 24px;
2513
+ border-radius: var(--slide-radius-lg);
2514
+ color: var(--text-main);
2515
+ }
2516
+
2517
+ .mock-ui-inner {
2518
+ background: var(--block-background-fill);
2519
+ border: 1px solid var(--card-border-subtle);
2520
+ padding: 24px;
2521
+ border-radius: var(--slide-radius-md);
2522
+ }
2523
+
2524
+ /* “Control box” inside the mock UI */
2525
+ .mock-ui-control-box {
2526
+ padding: 12px;
2527
+ background: var(--block-background-fill);
2528
+ border-radius: 8px;
2529
+ border: 1px solid var(--card-border-subtle);
2530
+ }
2531
+
2532
+ /* Little radio / check icons */
2533
+ .mock-ui-radio-on {
2534
+ font-size: 1.5rem;
2535
+ vertical-align: middle;
2536
+ color: var(--accent-strong);
2537
+ }
2538
+
2539
+ .mock-ui-radio-off {
2540
+ font-size: 1.5rem;
2541
+ vertical-align: middle;
2542
+ color: var(--text-muted);
2543
+ }
2544
+
2545
+ .mock-ui-slider-text {
2546
+ font-size: 1.5rem;
2547
+ margin: 0;
2548
+ color: var(--accent-strong);
2549
+ letter-spacing: 4px;
2550
+ }
2551
+
2552
+ .mock-ui-slider-bar {
2553
+ color: var(--text-muted);
2554
+ }
2555
+
2556
+ /* Simple mock button representation */
2557
+ .mock-button {
2558
+ width: 100%;
2559
+ font-size: 1.25rem;
2560
+ font-weight: 600;
2561
+ padding: 16px 24px;
2562
+ background-color: var(--accent-strong);
2563
+ color: var(--body-background-fill);
2564
+ border: none;
2565
+ border-radius: 8px;
2566
+ cursor: not-allowed;
2567
+ }
2568
+
2569
+ /* Step visuals on slides */
2570
+ .step-visual {
2571
+ display: flex;
2572
+ flex-wrap: wrap;
2573
+ justify-content: space-around;
2574
+ align-items: center;
2575
+ margin: 24px 0;
2576
+ text-align: center;
2577
+ font-size: 1rem;
2578
+ }
2579
+
2580
+ .step-visual-box {
2581
+ padding: 16px;
2582
+ background: var(--block-background-fill); /* ✅ theme-aware */
2583
+ border-radius: 8px;
2584
+ border: 2px solid var(--border-color-primary);
2585
+ margin: 5px;
2586
+ color: var(--body-text-color); /* optional, safe */
2587
+ }
2588
+
2589
+ .step-visual-arrow {
2590
+ font-size: 2rem;
2591
+ margin: 5px;
2592
+ /* no explicit color – inherit from theme or override in dark mode */
2593
+ }
2594
+
2595
+ /* ------------------------------------------------------------------
2596
+ KPI Card (score feedback)
2597
+ ------------------------------------------------------------------ */
2598
+
2599
+ .kpi-card {
2600
+ background: var(--card-bg-strong);
2601
+ border: 2px solid var(--accent-strong);
2602
+ padding: 24px;
2603
+ border-radius: var(--slide-radius-lg);
2604
+ text-align: center;
2605
+ max-width: 600px;
2606
+ margin: auto;
2607
+ color: var(--text-main);
2608
+ box-shadow: var(--shadow-drop, 0 4px 6px -1px rgba(0,0,0,0.08));
2609
+ min-height: 200px; /* prevent layout shift */
2610
+ }
2611
+
2612
+ .kpi-card-body {
2613
+ display: flex;
2614
+ flex-wrap: wrap;
2615
+ justify-content: space-around;
2616
+ align-items: flex-end;
2617
+ margin-top: 24px;
2618
+ }
2619
+
2620
+ .kpi-metric-box {
2621
+ min-width: 150px;
2622
+ margin: 10px;
2623
+ }
2624
+
2625
+ .kpi-label {
2626
+ font-size: 1rem;
2627
+ color: var(--text-muted);
2628
+ margin: 0;
2629
+ }
2630
+
2631
+ .kpi-score {
2632
+ font-size: 3rem;
2633
+ font-weight: 700;
2634
+ margin: 0;
2635
+ line-height: 1.1;
2636
+ color: var(--accent-strong);
2637
+ }
2638
+
2639
+ .kpi-subtext-muted {
2640
+ font-size: 1.2rem;
2641
+ font-weight: 500;
2642
+ color: var(--text-muted);
2643
+ margin: 0;
2644
+ padding-top: 8px;
2645
+ }
2646
+
2647
+ /* Small variants to hint semantic state without hard-coded colors */
2648
+ .kpi-card--neutral {
2649
+ border-color: var(--card-border-subtle);
2650
+ }
2651
+
2652
+ .kpi-card--subtle-accent {
2653
+ border-color: var(--accent-strong);
2654
+ }
2655
+
2656
+ .kpi-score--muted {
2657
+ color: var(--text-muted);
2658
+ }
2659
+
2660
+ /* ------------------------------------------------------------------
2661
+ Leaderboard Table + Placeholder
2662
+ ------------------------------------------------------------------ */
2663
+
2664
+ .leaderboard-html-table {
2665
+ width: 100%;
2666
+ border-collapse: collapse;
2667
+ text-align: left;
2668
+ font-size: 1rem;
2669
+ color: var(--text-main);
2670
+ min-height: 300px; /* Stable height */
2671
+ }
2672
+
2673
+ .leaderboard-html-table thead {
2674
+ background: var(--block-background-fill);
2675
+ }
2676
+
2677
+ .leaderboard-html-table th {
2678
+ padding: 12px 16px;
2679
+ font-size: 0.9rem;
2680
+ color: var(--text-muted);
2681
+ font-weight: 500;
2682
+ }
2683
+
2684
+ .leaderboard-html-table tbody tr {
2685
+ border-bottom: 1px solid var(--card-border-subtle);
2686
+ }
2687
+
2688
+ .leaderboard-html-table td {
2689
+ padding: 12px 16px;
2690
+ }
2691
+
2692
+ .leaderboard-html-table .user-row-highlight {
2693
+ background: rgba( var(--color-accent-rgb, 59,130,246), 0.1 );
2694
+ font-weight: 600;
2695
+ color: var(--accent-strong);
2696
+ }
2697
+
2698
+ /* Static placeholder (no shimmer, no animation) */
2699
+ .lb-placeholder {
2700
+ min-height: 300px;
2701
+ display: flex;
2702
+ flex-direction: column;
2703
+ align-items: center;
2704
+ justify-content: center;
2705
+ background: var(--block-background-fill);
2706
+ border: 1px solid var(--card-border-subtle);
2707
+ border-radius: 12px;
2708
+ padding: 40px 20px;
2709
+ text-align: center;
2710
+ }
2711
+
2712
+ .lb-placeholder-title {
2713
+ font-size: 1.25rem;
2714
+ font-weight: 500;
2715
+ color: var(--text-muted);
2716
+ margin-bottom: 8px;
2717
+ }
2718
+
2719
+ .lb-placeholder-sub {
2720
+ font-size: 1rem;
2721
+ color: var(--text-muted);
2722
+ }
2723
+
2724
+ /* ------------------------------------------------------------------
2725
+ Processing / “Experiment running” status
2726
+ ------------------------------------------------------------------ */
2727
+
2728
+ .processing-status {
2729
+ background: var(--block-background-fill);
2730
+ border: 2px solid var(--accent-strong);
2731
+ border-radius: 16px;
2732
+ padding: 30px;
2733
+ text-align: center;
2734
+ box-shadow: var(--shadow-drop, 0 4px 6px rgba(0,0,0,0.12));
2735
+ animation: pulse-indigo 2s infinite;
2736
+ color: var(--text-main);
2737
+ }
2738
+
2739
+ .processing-icon {
2740
+ font-size: 4rem;
2741
+ margin-bottom: 10px;
2742
+ display: block;
2743
+ animation: spin-slow 3s linear infinite;
2744
+ }
2745
+
2746
+ .processing-text {
2747
+ font-size: 1.5rem;
2748
+ font-weight: 700;
2749
+ color: var(--accent-strong);
2750
+ }
2751
+
2752
+ .processing-subtext {
2753
+ font-size: 1.1rem;
2754
+ color: var(--text-muted);
2755
+ margin-top: 8px;
2756
+ }
2757
+
2758
+ /* Pulse & spin animations */
2759
+ @keyframes pulse-indigo {
2760
+ 0% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.4); }
2761
+ 70% { box-shadow: 0 0 0 15px rgba(99, 102, 241, 0); }
2762
+ 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0); }
2763
+ }
2764
+
2765
+ @keyframes spin-slow {
2766
+ from { transform: rotate(0deg); }
2767
+ to { transform: rotate(360deg); }
2768
+ }
2769
+
2770
+ /* Conclusion arrow pulse */
2771
+ @keyframes pulseArrow {
2772
+ 0% { transform: scale(1); opacity: 1; }
2773
+ 50% { transform: scale(1.08); opacity: 0.85; }
2774
+ 100% { transform: scale(1); opacity: 1; }
2775
+ }
2776
+
2777
+ @media (prefers-reduced-motion: reduce) {
2778
+ [style*='pulseArrow'] {
2779
+ animation: none !important;
2780
+ }
2781
+ .processing-status,
2782
+ .processing-icon {
2783
+ animation: none !important;
2784
+ }
2785
+ }
2786
+
2787
+ /* ------------------------------------------------------------------
2788
+ Attempts Tracker + Init Banner + Alerts
2789
+ ------------------------------------------------------------------ */
2790
+
2791
+ .init-banner {
2792
+ background: var(--card-bg-strong);
2793
+ padding: 12px;
2794
+ border-radius: 8px;
2795
+ text-align: center;
2796
+ margin-bottom: 16px;
2797
+ border: 1px solid var(--card-border-subtle);
2798
+ color: var(--text-main);
2799
+ }
2800
+
2801
+ .init-banner__text {
2802
+ margin: 0;
2803
+ font-weight: 500;
2804
+ color: var(--text-muted);
2805
+ }
2806
+
2807
+ /* Attempts tracker shell */
2808
+ .attempts-tracker {
2809
+ text-align: center;
2810
+ padding: 8px;
2811
+ margin: 8px 0;
2812
+ background: var(--block-background-fill);
2813
+ border-radius: 8px;
2814
+ border: 1px solid var(--card-border-subtle);
2815
+ }
2816
+
2817
+ .attempts-tracker__text {
2818
+ margin: 0;
2819
+ font-weight: 600;
2820
+ font-size: 1rem;
2821
+ color: var(--accent-strong);
2822
+ }
2823
+
2824
+ /* Limit reached variant – we *still* stick to theme colors */
2825
+ .attempts-tracker--limit .attempts-tracker__text {
2826
+ color: var(--text-main);
2827
+ }
2828
+
2829
+ /* Generic alert helpers used in inline login messages */
2830
+ .alert {
2831
+ padding: 12px 16px;
2832
+ border-radius: 8px;
2833
+ margin-top: 12px;
2834
+ text-align: left;
2835
+ font-size: 0.95rem;
2836
+ }
2837
+
2838
+ .alert--error {
2839
+ border-left: 4px solid var(--accent-strong);
2840
+ background: var(--block-background-fill);
2841
+ color: var(--text-main);
2842
+ }
2843
+
2844
+ .alert--success {
2845
+ border-left: 4px solid var(--accent-strong);
2846
+ background: var(--block-background-fill);
2847
+ color: var(--text-main);
2848
+ }
2849
+
2850
+ .alert__title {
2851
+ margin: 0;
2852
+ font-weight: 600;
2853
+ color: var(--text-main);
2854
+ }
2855
+
2856
+ .alert__body {
2857
+ margin: 8px 0 0 0;
2858
+ color: var(--text-muted);
2859
+ }
2860
+
2861
+ /* ------------------------------------------------------------------
2862
+ Navigation Loading Overlay
2863
+ ------------------------------------------------------------------ */
2864
+
2865
+ #nav-loading-overlay {
2866
+ position: fixed;
2867
+ top: 0;
2868
+ left: 0;
2869
+ width: 100%;
2870
+ height: 100%;
2871
+ background: color-mix(in srgb, var(--body-background-fill) 90%, transparent);
2872
+ z-index: 9999;
2873
+ display: none;
2874
+ flex-direction: column;
2875
+ align-items: center;
2876
+ justify-content: center;
2877
+ opacity: 0;
2878
+ transition: opacity 0.3s ease;
2879
+ }
2880
+
2881
+ .nav-spinner {
2882
+ width: 50px;
2883
+ height: 50px;
2884
+ border: 5px solid var(--card-border-subtle);
2885
+ border-top: 5px solid var(--accent-strong);
2886
+ border-radius: 50%;
2887
+ animation: nav-spin 1s linear infinite;
2888
+ margin-bottom: 20px;
2889
+ }
2890
+
2891
+ @keyframes nav-spin {
2892
+ 0% { transform: rotate(0deg); }
2893
+ 100% { transform: rotate(360deg); }
2894
+ }
2895
+
2896
+ #nav-loading-text {
2897
+ font-size: 1.3rem;
2898
+ font-weight: 600;
2899
+ color: var(--accent-strong);
2900
+ }
2901
+
2902
+ /* ------------------------------------------------------------------
2903
+ Utility: Image inversion for dark mode (if needed)
2904
+ ------------------------------------------------------------------ */
2905
+
2906
+ .dark-invert-image {
2907
+ filter: invert(0);
2908
+ }
2909
+
2910
+ @media (prefers-color-scheme: dark) {
2911
+ .dark-invert-image {
2912
+ filter: invert(1) hue-rotate(180deg);
2913
+ }
2914
+ }
2915
+
2916
+ /* ------------------------------------------------------------------
2917
+ Dark Mode Specific Fine Tuning
2918
+ ------------------------------------------------------------------ */
2919
+
2920
+ @media (prefers-color-scheme: dark) {
2921
+ .panel-box,
2922
+ .leaderboard-box,
2923
+ .mock-ui-box,
2924
+ .mock-ui-inner,
2925
+ .processing-status,
2926
+ .kpi-card {
2927
+ background: color-mix(in srgb, var(--block-background-fill) 85%, #000 15%);
2928
+ border-color: color-mix(in srgb, var(--card-border-subtle) 70%, var(--accent-strong) 30%);
2929
+ }
2930
+
2931
+ .leaderboard-html-table thead {
2932
+ background: color-mix(in srgb, var(--block-background-fill) 75%, #000 25%);
2933
+ }
2934
+
2935
+ .lb-placeholder {
2936
+ background: color-mix(in srgb, var(--block-background-fill) 75%, #000 25%);
2937
+ }
2938
+
2939
+ #nav-loading-overlay {
2940
+ background: color-mix(in srgb, #000 70%, var(--body-background-fill) 30%);
2941
+ }
2942
+ }
2943
+
2944
+ /* ---------- Conclusion Card Theme Tokens ---------- */
2945
+
2946
+ /* Light theme defaults */
2947
+ :root,
2948
+ :root[data-theme="light"] {
2949
+ --conclusion-card-bg: #e0f2fe; /* light sky */
2950
+ --conclusion-card-border: #0369a1; /* sky-700 */
2951
+ --conclusion-card-fg: #0f172a; /* slate-900 */
2952
+
2953
+ --conclusion-tip-bg: #fef9c3; /* amber-100 */
2954
+ --conclusion-tip-border: #f59e0b; /* amber-500 */
2955
+ --conclusion-tip-fg: #713f12; /* amber-900 */
2956
+
2957
+ --conclusion-ethics-bg: #fef2f2; /* red-50 */
2958
+ --conclusion-ethics-border: #ef4444; /* red-500 */
2959
+ --conclusion-ethics-fg: #7f1d1d; /* red-900 */
2960
+
2961
+ --conclusion-attempt-bg: #fee2e2; /* red-100 */
2962
+ --conclusion-attempt-border: #ef4444; /* red-500 */
2963
+ --conclusion-attempt-fg: #7f1d1d; /* red-900 */
2964
+
2965
+ --conclusion-next-fg: #0f172a; /* main text color */
2966
+ }
2967
+
2968
+ /* Dark theme overrides – keep contrast high on dark background */
2969
+ [data-theme="dark"] {
2970
+ --conclusion-card-bg: #020617; /* slate-950 */
2971
+ --conclusion-card-border: #38bdf8; /* sky-400 */
2972
+ --conclusion-card-fg: #e5e7eb; /* slate-200 */
2973
+
2974
+ --conclusion-tip-bg: rgba(250, 204, 21, 0.08); /* soft amber tint */
2975
+ --conclusion-tip-border: #facc15; /* amber-400 */
2976
+ --conclusion-tip-fg: #facc15;
2977
+
2978
+ --conclusion-ethics-bg: rgba(248, 113, 113, 0.10); /* soft red tint */
2979
+ --conclusion-ethics-border: #f97373; /* red-ish */
2980
+ --conclusion-ethics-fg: #fecaca;
2981
+
2982
+ --conclusion-attempt-bg: rgba(248, 113, 113, 0.16);
2983
+ --conclusion-attempt-border: #f97373;
2984
+ --conclusion-attempt-fg: #fee2e2;
2985
+
2986
+ --conclusion-next-fg: #e5e7eb;
2987
+ }
2988
+
2989
+ /* ---------- Conclusion Layout ---------- */
2990
+
2991
+ .app-conclusion-wrapper {
2992
+ text-align: center;
2993
+ }
2994
+
2995
+ .app-conclusion-title {
2996
+ font-size: 2.4rem;
2997
+ margin: 0;
2998
+ }
2999
+
3000
+ .app-conclusion-card {
3001
+ margin-top: 24px;
3002
+ max-width: 950px;
3003
+ margin-left: auto;
3004
+ margin-right: auto;
3005
+ padding: 28px;
3006
+ border-radius: 18px;
3007
+ border-width: 3px;
3008
+ border-style: solid;
3009
+ background: var(--conclusion-card-bg);
3010
+ border-color: var(--conclusion-card-border);
3011
+ color: var(--conclusion-card-fg);
3012
+ box-shadow: 0 20px 40px rgba(15, 23, 42, 0.25);
3013
+ }
3014
+
3015
+ .app-conclusion-subtitle {
3016
+ margin-top: 0;
3017
+ font-size: 1.5rem;
3018
+ }
3019
+
3020
+ .app-conclusion-metrics {
3021
+ list-style: none;
3022
+ padding: 0;
3023
+ font-size: 1.05rem;
3024
+ text-align: left;
3025
+ max-width: 640px;
3026
+ margin: 20px auto;
3027
+ }
3028
+
3029
+ /* ---------- Generic panel helpers reused here ---------- */
3030
+
3031
+ .app-panel-tip,
3032
+ .app-panel-critical,
3033
+ .app-panel-warning {
3034
+ padding: 16px;
3035
+ border-radius: 12px;
3036
+ border-left-width: 6px;
3037
+ border-left-style: solid;
3038
+ text-align: left;
3039
+ font-size: 0.98rem;
3040
+ line-height: 1.4;
3041
+ margin-top: 16px;
3042
+ }
3043
+
3044
+ .app-panel-title {
3045
+ margin: 0 0 4px 0;
3046
+ font-weight: 700;
3047
+ }
3048
+
3049
+ .app-panel-body {
3050
+ margin: 0;
3051
+ }
3052
+
3053
+ /* Specific variants */
3054
+
3055
+ .app-conclusion-tip.app-panel-tip {
3056
+ background: var(--conclusion-tip-bg);
3057
+ border-left-color: var(--conclusion-tip-border);
3058
+ color: var(--conclusion-tip-fg);
3059
+ }
3060
+
3061
+ .app-conclusion-ethics.app-panel-critical {
3062
+ background: var(--conclusion-ethics-bg);
3063
+ border-left-color: var(--conclusion-ethics-border);
3064
+ color: var(--conclusion-ethics-fg);
3065
+ }
3066
+
3067
+ .app-conclusion-attempt-cap.app-panel-warning {
3068
+ background: var(--conclusion-attempt-bg);
3069
+ border-left-color: var(--conclusion-attempt-border);
3070
+ color: var(--conclusion-attempt-fg);
3071
+ }
3072
+
3073
+ /* Divider + next section */
3074
+
3075
+ .app-conclusion-divider {
3076
+ margin: 28px 0;
3077
+ border: 0;
3078
+ border-top: 2px solid rgba(148, 163, 184, 0.8); /* slate-400-ish */
3079
+ }
3080
+
3081
+ .app-conclusion-next-title {
3082
+ margin: 0;
3083
+ color: var(--conclusion-next-fg);
3084
+ }
3085
+
3086
+ .app-conclusion-next-body {
3087
+ font-size: 1rem;
3088
+ color: var(--conclusion-next-fg);
3089
+ }
3090
+
3091
+ /* Arrow inherits the same color, keeps pulse animation defined earlier */
3092
+ .app-conclusion-arrow {
3093
+ margin: 12px 0;
3094
+ font-size: 3rem;
3095
+ animation: pulseArrow 2.5s infinite;
3096
+ color: var(--conclusion-next-fg);
3097
+ }
3098
+
3099
+ /* ---------------------------------------------------- */
3100
+ /* Final Conclusion Slide (Light Mode Defaults) */
3101
+ /* ---------------------------------------------------- */
3102
+
3103
+ .final-conclusion-root {
3104
+ text-align: center;
3105
+ color: var(--body-text-color);
3106
+ }
3107
+
3108
+ .final-conclusion-title {
3109
+ font-size: 2.4rem;
3110
+ margin: 0;
3111
+ }
3112
+
3113
+ .final-conclusion-card {
3114
+ background-color: var(--block-background-fill);
3115
+ color: var(--body-text-color);
3116
+ padding: 28px;
3117
+ border-radius: 18px;
3118
+ border: 2px solid var(--border-color-primary);
3119
+ margin-top: 24px;
3120
+ max-width: 950px;
3121
+ margin-left: auto;
3122
+ margin-right: auto;
3123
+ box-shadow: var(--shadow-drop, 0 4px 10px rgba(15, 23, 42, 0.08));
3124
+ }
3125
+
3126
+ .final-conclusion-subtitle {
3127
+ margin-top: 0;
3128
+ margin-bottom: 8px;
3129
+ }
3130
+
3131
+ .final-conclusion-list {
3132
+ list-style: none;
3133
+ padding: 0;
3134
+ font-size: 1.05rem;
3135
+ text-align: left;
3136
+ max-width: 640px;
3137
+ margin: 20px auto;
3138
+ }
3139
+
3140
+ .final-conclusion-list li {
3141
+ margin: 4px 0;
3142
+ }
3143
+
3144
+ .final-conclusion-tip {
3145
+ margin-top: 16px;
3146
+ padding: 16px;
3147
+ border-radius: 12px;
3148
+ border-left: 6px solid var(--color-accent);
3149
+ background-color: color-mix(in srgb, var(--color-accent) 12%, transparent);
3150
+ text-align: left;
3151
+ font-size: 0.98rem;
3152
+ line-height: 1.4;
3153
+ }
3154
+
3155
+ .final-conclusion-ethics {
3156
+ margin-top: 16px;
3157
+ padding: 18px;
3158
+ border-radius: 12px;
3159
+ border-left: 6px solid #ef4444;
3160
+ background-color: color-mix(in srgb, #ef4444 10%, transparent);
3161
+ text-align: left;
3162
+ font-size: 0.98rem;
3163
+ line-height: 1.4;
3164
+ }
3165
+
3166
+ .final-conclusion-attempt-cap {
3167
+ margin-top: 16px;
3168
+ padding: 16px;
3169
+ border-radius: 12px;
3170
+ border-left: 6px solid #ef4444;
3171
+ background-color: color-mix(in srgb, #ef4444 16%, transparent);
3172
+ text-align: left;
3173
+ font-size: 0.98rem;
3174
+ line-height: 1.4;
3175
+ }
3176
+
3177
+ .final-conclusion-divider {
3178
+ margin: 28px 0;
3179
+ border: 0;
3180
+ border-top: 2px solid var(--border-color-primary);
3181
+ }
3182
+
3183
+ .final-conclusion-next h2 {
3184
+ margin: 0;
3185
+ }
3186
+
3187
+ .final-conclusion-next p {
3188
+ font-size: 1rem;
3189
+ margin-top: 4px;
3190
+ margin-bottom: 0;
3191
+ }
3192
+
3193
+ .final-conclusion-scroll {
3194
+ margin: 12px 0 0 0;
3195
+ font-size: 3rem;
3196
+ animation: pulseArrow 2.5s infinite;
3197
+ }
3198
+
3199
+ /* ---------------------------------------------------- */
3200
+ /* Dark Mode Overrides for Final Slide */
3201
+ /* ---------------------------------------------------- */
3202
+
3203
+ @media (prefers-color-scheme: dark) {
3204
+ .final-conclusion-card {
3205
+ background-color: #0b1120; /* deep slate */
3206
+ color: white; /* 100% contrast confidence */
3207
+ border-color: #38bdf8;
3208
+ box-shadow: none;
3209
+ }
3210
+
3211
+ .final-conclusion-tip {
3212
+ background-color: rgba(56, 189, 248, 0.18);
3213
+ }
3214
+
3215
+ .final-conclusion-ethics {
3216
+ background-color: rgba(248, 113, 113, 0.18);
3217
+ }
3218
+
3219
+ .final-conclusion-attempt-cap {
3220
+ background-color: rgba(248, 113, 113, 0.26);
3221
+ }
3222
+ }
3223
+ /* ---------------------------------------------------- */
3224
+ /* Slide 3: INPUT → MODEL → OUTPUT flow (theme-aware) */
3225
+ /* ---------------------------------------------------- */
3226
+
3227
+
3228
+ .model-flow {
3229
+ text-align: center;
3230
+ font-weight: 600;
3231
+ font-size: 1.2rem;
3232
+ margin: 20px 0;
3233
+ /* No explicit color – inherit from the card */
3234
+ }
3235
+
3236
+ .model-flow-label {
3237
+ padding: 0 0.1rem;
3238
+ /* No explicit color – inherit */
3239
+ }
3240
+
3241
+ .model-flow-arrow {
3242
+ margin: 0 0.35rem;
3243
+ font-size: 1.4rem;
3244
+ /* No explicit color – inherit */
3245
+ }
3246
+
3247
+ @media (prefers-color-scheme: dark) {
3248
+ .model-flow {
3249
+ color: var(--body-text-color);
3250
+ }
3251
+ .model-flow-arrow {
3252
+ /* In dark mode, nudge arrows toward accent for contrast/confidence */
3253
+ color: color-mix(in srgb, var(--color-accent) 75%, var(--body-text-color) 25%);
3254
+ }
3255
+ }
3256
+ """
3257
+
3258
+
3259
+ # Define globals for yield
3260
+ global submit_button, submission_feedback_display, team_leaderboard_display
3261
+ # --- THIS IS THE FIXED LINE ---
3262
+ global individual_leaderboard_display, last_submission_score_state, last_rank_state, best_score_state, submission_count_state, first_submission_score_state
3263
+ # --- END OF FIX ---
3264
+ global rank_message_display, model_type_radio, complexity_slider
3265
+ global feature_set_checkbox, data_size_radio
3266
+ global login_username, login_password, login_submit, login_error
3267
+ global attempts_tracker_display, team_name_state
3268
+
3269
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), css=css) as demo:
3270
+ # Persistent top anchor for scroll-to-top navigation
3271
+ gr.HTML("<div id='app_top_anchor' style='height:0;'></div>")
3272
+
3273
+ # Navigation loading overlay with spinner and dynamic message
3274
+ gr.HTML("""
3275
+ <div id='nav-loading-overlay'>
3276
+ <div class='nav-spinner'></div>
3277
+ <span id='nav-loading-text'>Loading...</span>
3278
+ </div>
3279
+ """)
3280
+
3281
+ # Concurrency Note: Do NOT read per-user state from os.environ here.
3282
+ # Username and other per-user data are managed via gr.State objects
3283
+ # and populated during handle_load_with_session_auth.
3284
+
3285
+ # Loading screen
3286
+ with gr.Column(visible=False) as loading_screen:
3287
+ gr.Markdown(
3288
+ """
3289
+ <div style='text-align:center; padding:100px 0;'>
3290
+ <h2 style='font-size:2rem; color:#6b7280;'>⏳ Loading...</h2>
3291
+ </div>
3292
+ """
3293
+ )
3294
+
3295
+ # --- Briefing Slideshow (Updated with New Cards) ---
3296
+
3297
+ # Slide 7: The Final Transition
3298
+ with gr.Column(visible=True, elem_id="intro-slide") as intro_slide:
3299
+ gr.Markdown("<h1 style='text-align:center;'>🚀 The Final Frontier</h1>")
3300
+
3301
+ gr.HTML(
3302
+ """
3303
+ <div class='slide-content'>
3304
+ <div class='panel-box'>
3305
+ <div style="text-align:center; margin-bottom: 25px;">
3306
+ <p style="font-size:1.15rem; line-height:1.6;">
3307
+ You have explored the ethics. You understand the risks.
3308
+ <br>
3309
+ Now, it is time to prove you have the technical <strong>Skill</strong>.
3310
+ </p>
3311
+ </div>
3312
+
3313
+ <div style="background:linear-gradient(to right, #eff6ff, white); border:2px solid #3b82f6; border-radius:12px; padding:24px; margin-bottom: 25px;">
3314
+ <h3 style="margin-top:0; color:#1e40af; text-align:center; font-size:1.4rem;">🛠️ The Accuracy Competition</h3>
3315
+ <div style="font-size:1.1rem; line-height:1.6; color:#1f2937;">
3316
+ <p>Your <strong>final mission</strong> is to compete against your peers to build the <strong>most accurate model possible</strong>.</p>
3317
+
3318
+ <p>✨ <strong>Unrestricted Access:</strong> You are now a Lead Engineer. All data inputs and modeling tools are <strong>unlocked immediately</strong>.</p>
3319
+
3320
+ <p>Use every tool at your disposal to climb the leaderboard, but remember the lessons you just learned:
3321
+ <em>Accuracy is the goal, but data choices have consequences.</em></p>
3322
+ </div>
3323
+ </div>
3324
+
3325
+ <div style="text-align:center; margin-top:20px; padding-top:10px; border-top: 1px solid #e5e7eb;">
3326
+ <p style="font-size:1.2rem; font-weight:700; color:#4b5563; margin-bottom:5px;">
3327
+ Ready to begin?
3328
+ </p>
3329
+ <p style="font-size:1rem; color:#6b7280; margin-top:0;">
3330
+ 👇 Click the <b>"Enter the Arena"</b> button below.
3331
+ </p>
3332
+ </div>
3333
+ </div>
3334
+ </div>
3335
+ """
3336
+ )
3337
+
3338
+ # Only ONE button needed now
3339
+ intro_next_btn = gr.Button("Enter the Arena ▶️", variant="primary", size="lg")
3340
+
3341
+ # --- End Briefing Slideshow ---
3342
+
3343
+
3344
+ # Model Building App (Main Interface)
3345
+ with gr.Column(visible=False, elem_id="model-step") as model_building_step:
3346
+ gr.Markdown("<h1 style='text-align:center;'>🛠️ Model Building Arena</h1>")
3347
+
3348
+ # Status panel for initialization progress - HIDDEN
3349
+ init_status_display = gr.HTML(value="", visible=False)
3350
+
3351
+ # Banner for UI state
3352
+
3353
+ init_banner = gr.HTML(
3354
+ value=(
3355
+ "<div class='init-banner'>"
3356
+ "<p class='init-banner__text'>"
3357
+ "⏳ Initializing data & leaderboard… you can explore but must wait for readiness to submit."
3358
+ "</p>"
3359
+ "</div>"
3360
+ ),
3361
+ visible=True)
3362
+
3363
+ # Session-based authentication state objects
3364
+ # Concurrency Note: These are initialized to None/empty and populated
3365
+ # during handle_load_with_session_auth. Do NOT use os.environ here.
3366
+ username_state = gr.State(None)
3367
+ token_state = gr.State(None)
3368
+
3369
+ team_name_state = gr.State(None) # Populated via handle_load_with_session_auth
3370
+ last_submission_score_state = gr.State(0.0)
3371
+ last_rank_state = gr.State(0)
3372
+ best_score_state = gr.State(0.0)
3373
+ submission_count_state = gr.State(0)
3374
+ first_submission_score_state = gr.State(None)
3375
+
3376
+ # New states for readiness gating and preview tracking
3377
+ readiness_state = gr.State(False)
3378
+ was_preview_state = gr.State(False)
3379
+ kpi_meta_state = gr.State({})
3380
+ last_seen_ts_state = gr.State(None) # Track last seen user timestamp
3381
+
3382
+ # Buffered states for all dynamic inputs
3383
+ model_type_state = gr.State(DEFAULT_MODEL)
3384
+ complexity_state = gr.State(2)
3385
+ feature_set_state = gr.State(DEFAULT_FEATURE_SET)
3386
+ data_size_state = gr.State(DEFAULT_DATA_SIZE)
3387
+
3388
+ rank_message_display = gr.Markdown("### Rank loading...")
3389
+ with gr.Row():
3390
+ with gr.Column(scale=1):
3391
+
3392
+ model_type_radio = gr.Radio(
3393
+ label="1. Model Strategy",
3394
+ choices=[],
3395
+ value=None,
3396
+ interactive=False
3397
+ )
3398
+ model_card_display = gr.Markdown(get_model_card(DEFAULT_MODEL))
3399
+
3400
+ gr.Markdown("---") # Separator
3401
+
3402
+ complexity_slider = gr.Slider(
3403
+ label="2. Model Complexity (1–10)",
3404
+ minimum=1, maximum=3, step=1, value=2,
3405
+ info="Higher values allow deeper pattern learning; very high values may overfit."
3406
+ )
3407
+
3408
+ gr.Markdown("---") # Separator
3409
+
3410
+ feature_set_checkbox = gr.CheckboxGroup(
3411
+ label="3. Select Data Ingredients",
3412
+ choices=FEATURE_SET_ALL_OPTIONS,
3413
+ value=DEFAULT_FEATURE_SET,
3414
+ interactive=False,
3415
+ info="More ingredients unlock as you rank up!"
3416
+ )
3417
+
3418
+ gr.Markdown("---") # Separator
3419
+
3420
+ data_size_radio = gr.Radio(
3421
+ label="4. Data Size",
3422
+ choices=[DEFAULT_DATA_SIZE],
3423
+ value=DEFAULT_DATA_SIZE,
3424
+ interactive=False
3425
+ )
3426
+
3427
+ gr.Markdown("---") # Separator
3428
+
3429
+ # Attempt tracker display
3430
+ attempts_tracker_display = gr.HTML(
3431
+ value="<div style='text-align:center; padding:8px; margin:8px 0; background:#f0f9ff; border-radius:8px; border:1px solid #bae6fd;'>"
3432
+ "<p style='margin:0; color:#0369a1; font-weight:600; font-size:1rem;'>📊 Attempts used: 0/10</p>"
3433
+ "</div>",
3434
+ visible=True
3435
+ )
3436
+
3437
+ submit_button = gr.Button(
3438
+ value="5. 🔬 Build & Submit Model",
3439
+ variant="primary",
3440
+ size="lg"
3441
+ )
3442
+
3443
+ with gr.Column(scale=1):
3444
+ gr.HTML(
3445
+ """
3446
+ <div class='leaderboard-box'>
3447
+ <h3 style='margin-top:0;'>🏆 Live Standings</h3>
3448
+ <p style='margin:0;'>Submit a model to see your rank.</p>
3449
+ </div>
3450
+ """
3451
+ )
3452
+
3453
+ # KPI Card
3454
+ submission_feedback_display = gr.HTML(
3455
+ "<p style='text-align:center; color:#6b7280; padding:20px 0;'>Submit your first model to get feedback!</p>"
3456
+ )
3457
+
3458
+ # Inline Login Components (initially hidden)
3459
+ login_username = gr.Textbox(
3460
+ label="Username",
3461
+ placeholder="Enter your modelshare.ai username",
3462
+ visible=False
3463
+ )
3464
+ login_password = gr.Textbox(
3465
+ label="Password",
3466
+ type="password",
3467
+ placeholder="Enter your password",
3468
+ visible=False
3469
+ )
3470
+ login_submit = gr.Button(
3471
+ "Sign In & Submit",
3472
+ variant="primary",
3473
+ visible=False
3474
+ )
3475
+ login_error = gr.HTML(
3476
+ value="",
3477
+ visible=False
3478
+ )
3479
+
3480
+ with gr.Tabs():
3481
+ with gr.TabItem("Team Standings"):
3482
+ team_leaderboard_display = gr.HTML(
3483
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Submit a model to see team rankings.</p>"
3484
+ )
3485
+ with gr.TabItem("Individual Standings"):
3486
+ individual_leaderboard_display = gr.HTML(
3487
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Submit a model to see individual rankings.</p>"
3488
+ )
3489
+
3490
+ # REMOVED: Ethical Reminder HTML Block
3491
+ step_2_next = gr.Button("Finish & Reflect ▶️", variant="secondary")
3492
+
3493
+ # Conclusion Step
3494
+ with gr.Column(visible=False, elem_id="conclusion-step") as conclusion_step:
3495
+ gr.Markdown("<h1 style='text-align:center;'>✅ Section Complete</h1>")
3496
+ final_score_display = gr.HTML(value="<p>Preparing final summary...</p>")
3497
+ step_3_back = gr.Button("◀️ Back to Experiment")
3498
+
3499
+ # --- Navigation Logic ---
3500
+ all_steps_nav = [
3501
+ intro_slide,
3502
+ model_building_step,
3503
+ conclusion_step,
3504
+ loading_screen
3505
+ ]
3506
+
3507
+ def create_nav(current_step, next_step):
3508
+ """
3509
+ Simplified navigation: directly switches visibility without artificial loading screen.
3510
+ Loading screen only shown when entering arena if not yet ready.
3511
+ """
3512
+ def _nav():
3513
+ # Direct single-step navigation
3514
+ updates = {next_step: gr.update(visible=True)}
3515
+ for s in all_steps_nav:
3516
+ if s != next_step:
3517
+ updates[s] = gr.update(visible=False)
3518
+ return updates
3519
+ return _nav
3520
+
3521
+ def finalize_and_show_conclusion(best_score, submissions, rank, first_score, feature_set):
3522
+ """Build dynamic conclusion HTML and navigate to conclusion step."""
3523
+ html = build_final_conclusion_html(best_score, submissions, rank, first_score, feature_set)
3524
+ updates = {
3525
+ conclusion_step: gr.update(visible=True),
3526
+ final_score_display: gr.update(value=html)
3527
+ }
3528
+ for s in all_steps_nav:
3529
+ if s != conclusion_step:
3530
+ updates[s] = gr.update(visible=False)
3531
+ return [updates[s] if s in updates else gr.update() for s in all_steps_nav] + [html]
3532
+
3533
+ # Helper function to generate navigation JS with loading overlay
3534
+ def nav_js(target_id: str, message: str, min_show_ms: int = 1200) -> str:
3535
+ """
3536
+ Generate JavaScript for enhanced slide navigation with loading overlay.
3537
+
3538
+ Args:
3539
+ target_id: Element ID of the target slide (e.g., 'slide-2', 'model-step')
3540
+ message: Loading message to display during transition
3541
+ min_show_ms: Minimum time to show overlay (prevents flicker)
3542
+
3543
+ Returns:
3544
+ JavaScript arrow function string for Gradio's js parameter
3545
+ """
3546
+ return f"""
3547
+ ()=>{{
3548
+ try {{
3549
+ // Show overlay immediately
3550
+ const overlay = document.getElementById('nav-loading-overlay');
3551
+ const messageEl = document.getElementById('nav-loading-text');
3552
+ if(overlay && messageEl) {{
3553
+ messageEl.textContent = '{message}';
3554
+ overlay.style.display = 'flex';
3555
+ setTimeout(() => {{ overlay.style.opacity = '1'; }}, 10);
3556
+ }}
3557
+
3558
+ const startTime = Date.now();
3559
+
3560
+ // Scroll to top after brief delay
3561
+ setTimeout(() => {{
3562
+ const anchor = document.getElementById('app_top_anchor');
3563
+ const container = document.querySelector('.gradio-container') || document.scrollingElement || document.documentElement;
3564
+
3565
+ function doScroll() {{
3566
+ if(anchor) {{ anchor.scrollIntoView({{behavior:'smooth', block:'start'}}); }}
3567
+ else {{ container.scrollTo({{top:0, behavior:'smooth'}}); }}
3568
+
3569
+ // Best-effort Colab iframe scroll
3570
+ try {{
3571
+ if(window.parent && window.parent !== window && window.frameElement) {{
3572
+ const top = window.frameElement.getBoundingClientRect().top + window.parent.scrollY;
3573
+ window.parent.scrollTo({{top: Math.max(top - 10, 0), behavior:'smooth'}});
3574
+ }}
3575
+ }} catch(e2) {{}}
3576
+ }}
3577
+
3578
+ doScroll();
3579
+ // Retry scroll to combat layout shifts
3580
+ let scrollAttempts = 0;
3581
+ const scrollInterval = setInterval(() => {{
3582
+ scrollAttempts++;
3583
+ doScroll();
3584
+ if(scrollAttempts >= 3) clearInterval(scrollInterval);
3585
+ }}, 130);
3586
+ }}, 40);
3587
+
3588
+ // Poll for target visibility and minimum display time
3589
+ const targetId = '{target_id}';
3590
+ const minShowMs = {min_show_ms};
3591
+ let pollCount = 0;
3592
+ const maxPolls = 77; // ~7 seconds max
3593
+
3594
+ const pollInterval = setInterval(() => {{
3595
+ pollCount++;
3596
+ const elapsed = Date.now() - startTime;
3597
+ const target = document.getElementById(targetId);
3598
+ const isVisible = target && target.offsetParent !== null &&
3599
+ window.getComputedStyle(target).display !== 'none';
3600
+
3601
+ // Hide overlay when target is visible AND minimum time elapsed
3602
+ if((isVisible && elapsed >= minShowMs) || pollCount >= maxPolls) {{
3603
+ clearInterval(pollInterval);
3604
+ if(overlay) {{
3605
+ overlay.style.opacity = '0';
3606
+ setTimeout(() => {{ overlay.style.display = 'none'; }}, 300);
3607
+ }}
3608
+ }}
3609
+ }}, 90);
3610
+
3611
+ }} catch(e) {{ console.warn('nav-js error', e); }}
3612
+ }}
3613
+ """
3614
+ # Final wiring
3615
+ intro_next_btn.click(
3616
+ fn=create_nav(intro_slide, model_building_step),
3617
+ inputs=None, outputs=all_steps_nav,
3618
+ js=nav_js("model-step", "Entering model arena...")
3619
+ )
3620
+
3621
+ # App -> Conclusion (unchanged)
3622
+ step_2_next.click(
3623
+ fn=finalize_and_show_conclusion,
3624
+ inputs=[
3625
+ best_score_state,
3626
+ submission_count_state,
3627
+ last_rank_state,
3628
+ first_submission_score_state,
3629
+ feature_set_state
3630
+ ],
3631
+ outputs=all_steps_nav + [final_score_display],
3632
+ js=nav_js("conclusion-step", "Generating performance summary...")
3633
+ )
3634
+
3635
+ # Conclusion -> App (unchanged)
3636
+ step_3_back.click(
3637
+ fn=create_nav(conclusion_step, model_building_step),
3638
+ inputs=None, outputs=all_steps_nav,
3639
+ js=nav_js("model-step", "Returning to experiment workspace...")
3640
+ )
3641
+
3642
+ # Events
3643
+ model_type_radio.change(
3644
+ fn=get_model_card,
3645
+ inputs=model_type_radio,
3646
+ outputs=model_card_display
3647
+ )
3648
+ model_type_radio.change(
3649
+ fn=lambda v: v or DEFAULT_MODEL,
3650
+ inputs=model_type_radio,
3651
+ outputs=model_type_state
3652
+ )
3653
+ complexity_slider.change(fn=lambda v: v, inputs=complexity_slider, outputs=complexity_state)
3654
+
3655
+ feature_set_checkbox.change(
3656
+ fn=lambda v: v or [],
3657
+ inputs=feature_set_checkbox,
3658
+ outputs=feature_set_state
3659
+ )
3660
+ data_size_radio.change(
3661
+ fn=lambda v: v or DEFAULT_DATA_SIZE,
3662
+ inputs=data_size_radio,
3663
+ outputs=data_size_state
3664
+ )
3665
+
3666
+ all_outputs = [
3667
+ submission_feedback_display,
3668
+ team_leaderboard_display,
3669
+ individual_leaderboard_display,
3670
+ last_submission_score_state,
3671
+ last_rank_state,
3672
+ best_score_state,
3673
+ submission_count_state,
3674
+ first_submission_score_state,
3675
+ rank_message_display,
3676
+ model_type_radio,
3677
+ complexity_slider,
3678
+ feature_set_checkbox,
3679
+ data_size_radio,
3680
+ submit_button,
3681
+ login_username,
3682
+ login_password,
3683
+ login_submit,
3684
+ login_error,
3685
+ attempts_tracker_display,
3686
+ was_preview_state,
3687
+ kpi_meta_state,
3688
+ last_seen_ts_state
3689
+ ]
3690
+
3691
+ # Wire up login button
3692
+ login_submit.click(
3693
+ fn=perform_inline_login,
3694
+ inputs=[login_username, login_password],
3695
+ outputs=[
3696
+ login_username,
3697
+ login_password,
3698
+ login_submit,
3699
+ login_error,
3700
+ submit_button,
3701
+ submission_feedback_display,
3702
+ team_name_state,
3703
+ username_state, # NEW
3704
+ token_state # NEW
3705
+ ]
3706
+ )
3707
+
3708
+ # Removed gr.State(username) from the inputs list
3709
+ submit_button.click(
3710
+ fn=run_experiment,
3711
+ inputs=[
3712
+ model_type_state,
3713
+ complexity_state,
3714
+ feature_set_state,
3715
+ data_size_state,
3716
+ team_name_state,
3717
+ last_submission_score_state,
3718
+ last_rank_state,
3719
+ submission_count_state,
3720
+ first_submission_score_state,
3721
+ best_score_state,
3722
+ username_state, # NEW: Session-based auth
3723
+ token_state, # NEW: Session-based auth
3724
+ readiness_state, # Renamed to readiness_flag in function signature
3725
+ was_preview_state, # Renamed to was_preview_prev in function signature
3726
+ # kpi_meta_state removed from inputs - used only as output
3727
+ ],
3728
+ outputs=all_outputs,
3729
+ show_progress="full",
3730
+ js=nav_js("model-step", "Running experiment...", 500)
3731
+ )
3732
+
3733
+ # Timer for polling initialization status
3734
+ status_timer = gr.Timer(value=0.5, active=True) # Poll every 0.5 seconds
3735
+
3736
+ def update_init_status():
3737
+ """
3738
+ Poll initialization status and update UI elements.
3739
+ Returns status HTML, banner visibility, submit button state, data size choices, and readiness_state.
3740
+ """
3741
+ status_html, ready = poll_init_status()
3742
+
3743
+ # Update banner visibility - hide when ready
3744
+ banner_visible = not ready
3745
+
3746
+ # Update submit button
3747
+ if ready:
3748
+ submit_label = "5. 🔬 Build & Submit Model"
3749
+ submit_interactive = True
3750
+ else:
3751
+ submit_label = "⏳ Waiting for data..."
3752
+ submit_interactive = False
3753
+
3754
+ # Get available data sizes based on init progress
3755
+ available_sizes = get_available_data_sizes()
3756
+
3757
+ # Stop timer once fully initialized
3758
+ timer_active = not (ready and INIT_FLAGS.get("pre_samples_full", False))
3759
+
3760
+ return (
3761
+ status_html,
3762
+ gr.update(visible=banner_visible),
3763
+ gr.update(value=submit_label, interactive=submit_interactive),
3764
+ gr.update(choices=available_sizes),
3765
+ timer_active,
3766
+ ready # readiness_state
3767
+ )
3768
+
3769
+ status_timer.tick(
3770
+ fn=update_init_status,
3771
+ inputs=None,
3772
+ outputs=[init_status_display, init_banner, submit_button, data_size_radio, status_timer, readiness_state]
3773
+ )
3774
+
3775
+ # Handle session-based authentication on page load
3776
+ def handle_load_with_session_auth(request: "gr.Request"):
3777
+ """
3778
+ Check for session token, auto-login if present, then load initial UI with stats.
3779
+
3780
+ Concurrency Note: This function does NOT set per-user values in os.environ.
3781
+ All authentication state is returned via gr.State objects (username_state,
3782
+ token_state, team_name_state) to prevent cross-user data leakage.
3783
+ """
3784
+ success, username, token = _try_session_based_auth(request)
3785
+
3786
+ if success and username and token:
3787
+ _log(f"Session auth successful on load for {username}")
3788
+
3789
+ # Get user stats and team from cache/leaderboard
3790
+ stats = _compute_user_stats(username, token)
3791
+ team_name = stats.get("team_name", "")
3792
+
3793
+ # Concurrency Note: Do NOT set os.environ for per-user values.
3794
+ # Return state via gr.State objects exclusively.
3795
+
3796
+ # Hide login form since user is authenticated via session
3797
+ # Return initial load results plus login form hidden
3798
+ # Pass token explicitly for authenticated leaderboard fetch
3799
+ initial_results = on_initial_load(username, token=token, team_name=team_name)
3800
+ return initial_results + (
3801
+ gr.update(visible=False), # login_username
3802
+ gr.update(visible=False), # login_password
3803
+ gr.update(visible=False), # login_submit
3804
+ gr.update(visible=False), # login_error (hide any messages)
3805
+ username, # username_state
3806
+ token, # token_state
3807
+ team_name, # team_name_state
3808
+ )
3809
+ else:
3810
+ _log("No valid session on load, showing login form")
3811
+ # No valid session, proceed with normal load (show login form)
3812
+ # No token available, call without token
3813
+ initial_results = on_initial_load(None, token=None, team_name="")
3814
+ return initial_results + (
3815
+ gr.update(visible=True), # login_username
3816
+ gr.update(visible=True), # login_password
3817
+ gr.update(visible=True), # login_submit
3818
+ gr.update(visible=False), # login_error
3819
+ None, # username_state
3820
+ None, # token_state
3821
+ "", # team_name_state
3822
+ )
3823
+
3824
+ demo.load(
3825
+ fn=handle_load_with_session_auth,
3826
+ inputs=None, # Request is auto-injected
3827
+ outputs=[
3828
+ model_card_display,
3829
+ team_leaderboard_display,
3830
+ individual_leaderboard_display,
3831
+ rank_message_display,
3832
+ model_type_radio,
3833
+ complexity_slider,
3834
+ feature_set_checkbox,
3835
+ data_size_radio,
3836
+ login_username,
3837
+ login_password,
3838
+ login_submit,
3839
+ login_error,
3840
+ username_state, # NEW
3841
+ token_state, # NEW
3842
+ team_name_state, # NEW
3843
+ ]
3844
+ )
3845
+
3846
+ return demo
3847
+
3848
+ # -------------------------------------------------------------------------
3849
+ # 4. Convenience Launcher
3850
+ # -------------------------------------------------------------------------
3851
+
3852
+ def launch_model_building_game_en_final_app(height: int = 1200, share: bool = False, debug: bool = False) -> None:
3853
+ """
3854
+ Create and directly launch the Model Building Game app inline (e.g., in notebooks).
3855
+ """
3856
+ global playground, X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST
3857
+ if playground is None:
3858
+ try:
3859
+ playground = Competition(MY_PLAYGROUND_ID)
3860
+ except Exception as e:
3861
+ print(f"WARNING: Could not connect to playground: {e}")
3862
+ playground = None
3863
+
3864
+ if X_TRAIN_RAW is None:
3865
+ X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST = load_and_prep_data()
3866
+
3867
+ demo = create_model_building_game_en_final_app()
3868
+ port = int(os.environ.get("PORT", 8080))
3869
+ demo.launch(share=share, inline=True, debug=debug, height=height, server_port=port)