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,4351 @@
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
+ # CACHE CONFIGURATION (Optimized: Thread-Safe SQLite)
66
+ # -------------------------------------------------------------------------
67
+ import sqlite3
68
+
69
+ CACHE_DB_FILE = "prediction_cache.sqlite"
70
+
71
+ def get_cached_prediction(key):
72
+ """
73
+ Lightning-fast lookup from SQLite database.
74
+ THREAD-SAFE FIX: Opens a new connection for every lookup.
75
+ """
76
+ # 1. Check if DB exists
77
+ if not os.path.exists(CACHE_DB_FILE):
78
+ return None
79
+
80
+ try:
81
+ # Use a context manager ('with') to ensure the connection
82
+ # is ALWAYS closed, releasing file locks immediately.
83
+ # timeout=10 ensures we don't wait forever if the file is busy.
84
+ with sqlite3.connect(CACHE_DB_FILE, timeout=10.0) as conn:
85
+ cursor = conn.cursor()
86
+ cursor.execute("SELECT value FROM cache WHERE key=?", (key,))
87
+ result = cursor.fetchone()
88
+
89
+ if result:
90
+ return result[0]
91
+ else:
92
+ return None
93
+
94
+ except sqlite3.OperationalError as e:
95
+ # Handle locking errors gracefully
96
+ print(f"⚠️ CACHE LOCK ERROR: {e}. Falling back to training.", flush=True)
97
+ return None
98
+
99
+ except Exception as e:
100
+ print(f"⚠️ DB READ ERROR: {e}", flush=True)
101
+ return None
102
+
103
+ print("✅ App configured for Thread-Safe SQLite Cache.")
104
+
105
+ LEADERBOARD_CACHE_SECONDS = int(os.environ.get("LEADERBOARD_CACHE_SECONDS", "45"))
106
+ MAX_LEADERBOARD_ENTRIES = os.environ.get("MAX_LEADERBOARD_ENTRIES")
107
+ MAX_LEADERBOARD_ENTRIES = int(MAX_LEADERBOARD_ENTRIES) if MAX_LEADERBOARD_ENTRIES else None
108
+ DEBUG_LOG = os.environ.get("DEBUG_LOG", "false").lower() == "true"
109
+
110
+ # In-memory caches (per container instance)
111
+ # Each cache has its own lock for thread safety under concurrent requests
112
+ _cache_lock = threading.Lock() # Protects _leaderboard_cache
113
+ _user_stats_lock = threading.Lock() # Protects _user_stats_cache
114
+ _auth_lock = threading.Lock() # Protects get_aws_token() credential injection
115
+
116
+ # Auth-aware leaderboard cache: separate entries for authenticated vs anonymous
117
+ # Structure: {"anon": {"data": df, "timestamp": float}, "auth": {"data": df, "timestamp": float}}
118
+ _leaderboard_cache: Dict[str, Dict[str, Any]] = {
119
+ "anon": {"data": None, "timestamp": 0.0},
120
+ "auth": {"data": None, "timestamp": 0.0},
121
+ }
122
+ _user_stats_cache: Dict[str, Dict[str, Any]] = {}
123
+ USER_STATS_TTL = LEADERBOARD_CACHE_SECONDS
124
+
125
+ # -------------------------------------------------------------------------
126
+ # Retry Helper for External API Calls
127
+ # -------------------------------------------------------------------------
128
+
129
+ T = TypeVar("T")
130
+
131
+ def _retry_with_backoff(
132
+ func: Callable[[], T],
133
+ max_attempts: int = 3,
134
+ base_delay: float = 0.5,
135
+ description: str = "operation"
136
+ ) -> T:
137
+ """
138
+ Execute a function with exponential backoff retry on failure.
139
+
140
+ Concurrency Note: This helper provides resilience against transient
141
+ network failures when calling external APIs (Competition.get_leaderboard,
142
+ playground.submit_model). Essential for Cloud Run deployments where
143
+ network calls may occasionally fail under load.
144
+
145
+ Args:
146
+ func: Callable to execute (should take no arguments)
147
+ max_attempts: Maximum number of attempts (default: 3)
148
+ base_delay: Initial delay in seconds, doubled each retry (default: 0.5)
149
+ description: Human-readable description for logging
150
+
151
+ Returns:
152
+ Result from successful function call
153
+
154
+ Raises:
155
+ Last exception if all attempts fail
156
+ """
157
+ last_exception: Optional[Exception] = None
158
+ delay = base_delay
159
+
160
+ for attempt in range(1, max_attempts + 1):
161
+ try:
162
+ return func()
163
+ except Exception as e:
164
+ last_exception = e
165
+ if attempt < max_attempts:
166
+ _log(f"{description} attempt {attempt} failed: {e}. Retrying in {delay}s...")
167
+ time.sleep(delay)
168
+ delay *= 2 # Exponential backoff
169
+ else:
170
+ _log(f"{description} failed after {max_attempts} attempts: {e}")
171
+
172
+ # Loop always runs at least once (max_attempts >= 1), so last_exception is set
173
+ raise last_exception # type: ignore[misc]
174
+
175
+ def _log(msg: str):
176
+ """Log message if DEBUG_LOG is enabled."""
177
+ if DEBUG_LOG:
178
+ print(f"[ModelBuildingGame] {msg}")
179
+
180
+ def _normalize_team_name(name: str) -> str:
181
+ """Normalize team name for consistent comparison and storage."""
182
+ if not name:
183
+ return ""
184
+ return " ".join(str(name).strip().split())
185
+
186
+ def _get_leaderboard_with_optional_token(playground_instance: Optional["Competition"], token: Optional[str] = None) -> Optional[pd.DataFrame]:
187
+ """
188
+ Fetch fresh leaderboard with optional token authentication and retry logic.
189
+
190
+ This is a helper function that centralizes the pattern of fetching
191
+ a fresh (non-cached) leaderboard with optional token authentication.
192
+ Use this for user-facing flows that require fresh, full data.
193
+
194
+ Concurrency Note: Uses _retry_with_backoff for resilience against
195
+ transient network failures.
196
+
197
+ Args:
198
+ playground_instance: The Competition playground instance (or None)
199
+ token: Optional authentication token for the fetch
200
+
201
+ Returns:
202
+ DataFrame with leaderboard data, or None if fetch fails or playground is None
203
+ """
204
+ if playground_instance is None:
205
+ return None
206
+
207
+ def _fetch():
208
+ if token:
209
+ return playground_instance.get_leaderboard(token=token)
210
+ return playground_instance.get_leaderboard()
211
+
212
+ try:
213
+ return _retry_with_backoff(_fetch, description="leaderboard fetch")
214
+ except Exception as e:
215
+ _log(f"Leaderboard fetch failed after retries: {e}")
216
+ return None
217
+
218
+ def _fetch_leaderboard(token: Optional[str]) -> Optional[pd.DataFrame]:
219
+ """
220
+ Fetch leaderboard with auth-aware caching (TTL: LEADERBOARD_CACHE_SECONDS).
221
+
222
+ Concurrency Note: Cache is keyed by auth scope ("anon" vs "auth") to prevent
223
+ cross-user data leakage. Authenticated users share a single "auth" cache entry
224
+ to avoid unbounded cache growth. Protected by _cache_lock.
225
+ """
226
+ # Determine cache key based on authentication status
227
+ cache_key = "auth" if token else "anon"
228
+ now = time.time()
229
+
230
+ with _cache_lock:
231
+ cache_entry = _leaderboard_cache[cache_key]
232
+ if (
233
+ cache_entry["data"] is not None
234
+ and now - cache_entry["timestamp"] < LEADERBOARD_CACHE_SECONDS
235
+ ):
236
+ _log(f"Leaderboard cache hit ({cache_key})")
237
+ return cache_entry["data"]
238
+
239
+ _log(f"Fetching fresh leaderboard ({cache_key})...")
240
+ df = None
241
+ try:
242
+ playground_id = "https://cf3wdpkg0d.execute-api.us-east-1.amazonaws.com/prod/m"
243
+ playground_instance = Competition(playground_id)
244
+
245
+ def _fetch():
246
+ return playground_instance.get_leaderboard(token=token) if token else playground_instance.get_leaderboard()
247
+
248
+ df = _retry_with_backoff(_fetch, description="leaderboard fetch")
249
+ if df is not None and not df.empty and MAX_LEADERBOARD_ENTRIES:
250
+ df = df.head(MAX_LEADERBOARD_ENTRIES)
251
+ _log(f"Leaderboard fetched ({cache_key}): {len(df) if df is not None else 0} entries")
252
+ except Exception as e:
253
+ _log(f"Leaderboard fetch failed ({cache_key}): {e}")
254
+ df = None
255
+
256
+ with _cache_lock:
257
+ _leaderboard_cache[cache_key]["data"] = df
258
+ _leaderboard_cache[cache_key]["timestamp"] = time.time()
259
+ return df
260
+
261
+ def _get_or_assign_team(username: str, leaderboard_df: Optional[pd.DataFrame]) -> Tuple[str, bool]:
262
+ """Get existing team from leaderboard or assign random team."""
263
+ # TEAM_NAMES is defined in configuration section below
264
+ try:
265
+ if leaderboard_df is not None and not leaderboard_df.empty and "Team" in leaderboard_df.columns:
266
+ user_submissions = leaderboard_df[leaderboard_df["username"] == username]
267
+ if not user_submissions.empty:
268
+ if "timestamp" in user_submissions.columns:
269
+ try:
270
+ user_submissions = user_submissions.copy()
271
+ user_submissions["timestamp"] = pd.to_datetime(
272
+ user_submissions["timestamp"], errors="coerce"
273
+ )
274
+ user_submissions = user_submissions.sort_values("timestamp", ascending=False)
275
+ _log(f"Sorted {len(user_submissions)} submissions by timestamp for {username}")
276
+ except Exception as ts_err:
277
+ _log(f"Timestamp sort error: {ts_err}")
278
+ existing_team = user_submissions.iloc[0]["Team"]
279
+ if pd.notna(existing_team) and str(existing_team).strip():
280
+ normalized = _normalize_team_name(existing_team)
281
+ _log(f"Found existing team for {username}: {normalized}")
282
+ return normalized, False
283
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
284
+ _log(f"Assigning new team to {username}: {new_team}")
285
+ return new_team, True
286
+ except Exception as e:
287
+ _log(f"Team assignment error: {e}")
288
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
289
+ return new_team, True
290
+
291
+ def _try_session_based_auth(request: "gr.Request") -> Tuple[bool, Optional[str], Optional[str]]:
292
+ """Attempt to authenticate via session token. Returns (success, username, token)."""
293
+ try:
294
+ session_id = request.query_params.get("sessionid") if request else None
295
+ if not session_id:
296
+ _log("No sessionid in request")
297
+ return False, None, None
298
+
299
+ from aimodelshare.aws import get_token_from_session, _get_username_from_token
300
+
301
+ token = get_token_from_session(session_id)
302
+ if not token:
303
+ _log("Failed to get token from session")
304
+ return False, None, None
305
+
306
+ username = _get_username_from_token(token)
307
+ if not username:
308
+ _log("Failed to extract username from token")
309
+ return False, None, None
310
+
311
+ _log(f"Session auth successful for {username}")
312
+ return True, username, token
313
+
314
+ except Exception as e:
315
+ _log(f"Session auth failed: {e}")
316
+ return False, None, None
317
+
318
+
319
+
320
+ # -------------------------------------------------------------------------
321
+ # UPDATED FUNCTION
322
+ # -------------------------------------------------------------------------
323
+ def _compute_user_stats(username: str, token: str) -> Dict[str, Any]:
324
+ """
325
+ Compute user statistics with caching.
326
+
327
+ Concurrency Note: Protected by _user_stats_lock for thread-safe
328
+ cache reads and writes.
329
+ """
330
+ now = time.time()
331
+
332
+ # Thread-safe cache check
333
+ with _user_stats_lock:
334
+ cached = _user_stats_cache.get(username)
335
+ if cached and (now - cached.get("_ts", 0) < USER_STATS_TTL):
336
+ _log(f"User stats cache hit for {username}")
337
+ # Return shallow copy to prevent caller mutations from affecting cache.
338
+ # Stats dict contains only primitives (float, int, str), so shallow copy is sufficient.
339
+ return cached.copy()
340
+
341
+ _log(f"Computing fresh stats for {username}")
342
+ leaderboard_df = _fetch_leaderboard(token)
343
+ team_name, _ = _get_or_assign_team(username, leaderboard_df)
344
+
345
+ stats = {
346
+ "best_score": 0.0,
347
+ "rank": 0,
348
+ "team_name": team_name,
349
+ "submission_count": 0,
350
+ "last_score": 0.0,
351
+ "_ts": time.time()
352
+ }
353
+
354
+ try:
355
+ if leaderboard_df is not None and not leaderboard_df.empty:
356
+ user_submissions = leaderboard_df[leaderboard_df["username"] == username]
357
+ if not user_submissions.empty:
358
+ stats["submission_count"] = len(user_submissions)
359
+ if "accuracy" in user_submissions.columns:
360
+ stats["best_score"] = float(user_submissions["accuracy"].max())
361
+ if "timestamp" in user_submissions.columns:
362
+ try:
363
+ user_submissions = user_submissions.copy()
364
+ user_submissions["timestamp"] = pd.to_datetime(
365
+ user_submissions["timestamp"], errors="coerce"
366
+ )
367
+ recent = user_submissions.sort_values("timestamp", ascending=False).iloc[0]
368
+ stats["last_score"] = float(recent["accuracy"])
369
+ except:
370
+ stats["last_score"] = stats["best_score"]
371
+ else:
372
+ stats["last_score"] = stats["best_score"]
373
+
374
+ if "accuracy" in leaderboard_df.columns:
375
+ user_bests = leaderboard_df.groupby("username")["accuracy"].max()
376
+ ranked = user_bests.sort_values(ascending=False)
377
+ try:
378
+ stats["rank"] = int(ranked.index.get_loc(username) + 1)
379
+ except KeyError:
380
+ stats["rank"] = 0
381
+ except Exception as e:
382
+ _log(f"Error computing stats for {username}: {e}")
383
+
384
+ # Thread-safe cache update
385
+ with _user_stats_lock:
386
+ _user_stats_cache[username] = stats
387
+ _log(f"Stats for {username}: {stats}")
388
+ return stats
389
+
390
+
391
+ def _build_attempts_tracker_html(current_count, limit=10):
392
+ """
393
+ Generate HTML for the attempts tracker display.
394
+ Shows current attempt count vs limit with color coding.
395
+
396
+ Args:
397
+ current_count: Number of attempts used so far
398
+ limit: Maximum allowed attempts (default: ATTEMPT_LIMIT)
399
+
400
+ Returns:
401
+ str: HTML string for the tracker display
402
+ """
403
+ if current_count >= limit:
404
+ # Limit reached - red styling
405
+ bg_color = "#f0f9ff"
406
+ border_color = "#bae6fd"
407
+ text_color = "#0369a1"
408
+ icon = "🛑"
409
+ label = f"Última oportunidad (por ahora) para mejorar tu puntuación: {current_count}/{limit}"
410
+ else:
411
+ # Normal - blue styling
412
+ bg_color = "#f0f9ff"
413
+ border_color = "#bae6fd"
414
+ text_color = "#0369a1"
415
+ icon = "📊"
416
+ label = f"Intentos usados: {current_count}/{limit}"
417
+
418
+ return f"""<div style='text-align:center; padding:8px; margin:8px 0; background:{bg_color}; border-radius:8px; border:1px solid {border_color};'>
419
+ <p style='margin:0; color:{text_color}; font-weight:600; font-size:1rem;'>{icon} {label}</p>
420
+ </div>"""
421
+
422
+ def check_attempt_limit(submission_count: int, limit: int = None) -> Tuple[bool, str]:
423
+ """Check if submission count exceeds limit."""
424
+ # ATTEMPT_LIMIT is defined in configuration section below
425
+ if limit is None:
426
+ limit = ATTEMPT_LIMIT
427
+
428
+ if submission_count >= limit:
429
+ msg = f"⚠️ Límite de intentos alcanzado ({submission_count}/{limit})"
430
+ return False, msg
431
+ return True, f"Intentos: {submission_count}/{limit}"
432
+
433
+ # -------------------------------------------------------------------------
434
+ # Future: Fairness Metrics
435
+ # -------------------------------------------------------------------------
436
+
437
+ # def compute_fairness_metrics(y_true, y_pred, sensitive_attrs):
438
+ # """
439
+ # Compute fairness metrics for model predictions.
440
+ #
441
+ # Args:
442
+ # y_true: Ground truth labels
443
+ # y_pred: Model predictions
444
+ # sensitive_attrs: DataFrame with sensitive attributes (race, sex, age)
445
+ #
446
+ # Returns:
447
+ # dict: Fairness metrics including demographic parity, equalized odds
448
+ #
449
+ # TODO: Implement using fairlearn or aif360
450
+ # """
451
+ # pass
452
+
453
+
454
+
455
+ # -------------------------------------------------------------------------
456
+ # 1. Configuration
457
+ # -------------------------------------------------------------------------
458
+
459
+ MY_PLAYGROUND_ID = "https://cf3wdpkg0d.execute-api.us-east-1.amazonaws.com/prod/m"
460
+
461
+ # --- Submission Limit Configuration ---
462
+ # Maximum number of successful leaderboard submissions per user per session.
463
+ # Preview runs (pre-login) and failed/invalid attempts do NOT count toward this limit.
464
+ # Only actual successful playground.submit_model() calls increment the count.
465
+ #
466
+ # TODO: Server-side persistent enforcement recommended
467
+ # The current attempt limit is stored in gr.State (per-session) and can be bypassed
468
+ # by refreshing the browser. For production use with 100+ concurrent users,
469
+ # consider implementing server-side persistence via Redis or Firestore to track
470
+ # attempt counts per user across sessions.
471
+ ATTEMPT_LIMIT = 10
472
+
473
+ # --- Leaderboard Polling Configuration ---
474
+ # After a real authenticated submission, we poll the leaderboard to detect eventual consistency.
475
+ # This prevents the "stuck on first preview KPI" issue where the leaderboard hasn't updated yet.
476
+ # Increased from 12 to 60 to better tolerate backend latency and cold starts.
477
+ # If polling times out, optimistic fallback logic will provide provisional UI updates.
478
+ LEADERBOARD_POLL_TRIES = 60 # Number of polling attempts (increased to handle backend latency/cold starts)
479
+ LEADERBOARD_POLL_SLEEP = 1.0 # Sleep duration between polls (seconds)
480
+ ENABLE_AUTO_RESUBMIT_AFTER_READY = False # Future feature flag for auto-resubmit
481
+
482
+ # --- 1. MODEL CONFIGURATION (Keys match Database - English) ---
483
+ # --- 1. MODEL CONFIGURATION (Keys match Database - English) ---
484
+ MODEL_TYPES = {
485
+ "The Balanced Generalist": {
486
+ "model_builder": lambda: LogisticRegression(
487
+ max_iter=500, random_state=42, class_weight="balanced"
488
+ ),
489
+ "card_es": "Este modelo es rápido, fiable y equilibrat. Buen punto de partida; suele dar resultados estables en muchos casos."
490
+ },
491
+ "The Rule-Maker": {
492
+ "model_builder": lambda: DecisionTreeClassifier(
493
+ random_state=42, class_weight="balanced"
494
+ ),
495
+ "card_es": "Este modelo aprende reglas simples del tipo «si/entonces». Fácil de entender, pero le cuesta captar patrones complejos."
496
+ },
497
+ "The 'Nearest Neighbor'": {
498
+ "model_builder": lambda: KNeighborsClassifier(),
499
+ "card_es": "Este modelo se basa en los ejemplos más parecidos del pasado. «Si te pareces a estos casos, predeciré el mismo resultado»."
500
+ },
501
+ "The Deep Pattern-Finder": {
502
+ "model_builder": lambda: RandomForestClassifier(
503
+ random_state=42, class_weight="balanced"
504
+ ),
505
+ "card_es": "Este modelo combina muchos árboles de decisión para encontrar patrones complejos. Es potente, pero conviene no pasarse con la complejidad."
506
+ }
507
+ }
508
+
509
+ DEFAULT_MODEL = "The Balanced Generalist"
510
+
511
+ # --- 2. TRANSLATION MAPS (UI Display -> Database Key) ---
512
+
513
+ # Map English Keys to Spanish Display Names
514
+ MODEL_DISPLAY_MAP = {
515
+ "The Balanced Generalist": "El Generalista Equilibrado",
516
+ "The Rule-Maker": "El Creador de Reglas",
517
+ "The 'Nearest Neighbor'": "El 'Vecino Más Cercano'",
518
+ "The Deep Pattern-Finder": "El Buscador de Patrones Profundo"
519
+ }
520
+
521
+ # --- THIS WAS MISSING ---
522
+ # Create the Choices List as Tuples: [(Spanish Label, English Value)]
523
+ MODEL_RADIO_CHOICES = [(label, key) for key, label in MODEL_DISPLAY_MAP.items()]
524
+ # ------------------------
525
+
526
+ # Map Spanish Data Sizes (UI) to English Keys (Database)
527
+ DATA_SIZE_DB_MAP = {
528
+ "Pequeño (20%)": "Small (20%)",
529
+ "Medio (60%)": "Medium (60%)",
530
+ "Grande (80%)": "Large (80%)",
531
+ "Completo (100%)": "Full (100%)"
532
+ }
533
+
534
+ TEAM_NAMES = [
535
+ "The Moral Champions", "The Justice League", "The Data Detectives",
536
+ "The Ethical Explorers", "The Fairness Finders", "The Accuracy Avengers"
537
+ ]
538
+ CURRENT_TEAM_NAME = random.choice(TEAM_NAMES)
539
+
540
+ # Team name translations for UI display only (Spanish)
541
+ # Internal logic (ranking, caching, grouping) always uses canonical English names
542
+ TEAM_NAME_TRANSLATIONS = {
543
+ "en": {
544
+ "The Justice League": "The Justice League",
545
+ "The Moral Champions": "The Moral Champions",
546
+ "The Data Detectives": "The Data Detectives",
547
+ "The Ethical Explorers": "The Ethical Explorers",
548
+ "The Fairness Finders": "The Fairness Finders",
549
+ "The Accuracy Avengers": "The Accuracy Avengers"
550
+ },
551
+ "es": {
552
+ "The Justice League": "La Liga de la Justicia",
553
+ "The Moral Champions": "Los Campeones Morales",
554
+ "The Data Detectives": "Los Detectives de Datos",
555
+ "The Ethical Explorers": "Los Exploradores Éticos",
556
+ "The Fairness Finders": "Los Buscadores de Equidad",
557
+ "The Accuracy Avengers": "Los Vengadores de Precisión"
558
+ }
559
+ }
560
+
561
+ # UI language for team name display
562
+ UI_TEAM_LANG = "es"
563
+
564
+
565
+ # --- Feature groups for scaffolding (Weak -> Medium -> Strong) ---
566
+ FEATURE_SET_ALL_OPTIONS = [
567
+ ("Número de delitos graves juveniles", "juv_fel_count"),
568
+ ("Número de delitos leves juveniles", "juv_misd_count"),
569
+ ("Otros delitos juveniles", "juv_other_count"),
570
+ ("Origen étnico", "race"),
571
+ ("Sexo", "sex"),
572
+ ("Gravedad del cargo (leve / grave)", "c_charge_degree"),
573
+ ("Días antes del arresto", "days_b_screening_arrest"),
574
+ ("Edad", "age"),
575
+ ("Días en prisión", "length_of_stay"),
576
+ ("Número de delitos previos", "priors_count"),
577
+ ]
578
+ FEATURE_SET_GROUP_1_VALS = [
579
+ "juv_fel_count", "juv_misd_count", "juv_other_count", "race", "sex",
580
+ "c_charge_degree", "days_b_screening_arrest"
581
+ ]
582
+ FEATURE_SET_GROUP_2_VALS = ["c_charge_desc", "age"]
583
+ FEATURE_SET_GROUP_3_VALS = ["length_of_stay", "priors_count"]
584
+ ALL_NUMERIC_COLS = [
585
+ "juv_fel_count", "juv_misd_count", "juv_other_count",
586
+ "days_b_screening_arrest", "age", "length_of_stay", "priors_count"
587
+ ]
588
+ ALL_CATEGORICAL_COLS = [
589
+ "race", "sex", "c_charge_degree"
590
+ ]
591
+ DEFAULT_FEATURE_SET = FEATURE_SET_GROUP_1_VALS
592
+
593
+
594
+ # --- Data Size config ---
595
+ DATA_SIZE_MAP = {
596
+ "Pequeño (20%)": 0.2,
597
+ "Medio (60%)": 0.6,
598
+ "Grande (80%)": 0.8,
599
+ "Completo (100%)": 1.0
600
+ }
601
+ DEFAULT_DATA_SIZE = "Pequeño (20%)"
602
+
603
+
604
+ MAX_ROWS = 4000
605
+ TOP_N_CHARGE_CATEGORICAL = 50
606
+ WARM_MINI_ROWS = 300 # Small warm dataset for instant preview
607
+ CACHE_MAX_AGE_HOURS = 24 # Cache validity duration
608
+ np.random.seed(42)
609
+
610
+ # Global state containers (populated during initialization)
611
+ playground = None
612
+ X_TRAIN_RAW = None # Keep this for 100%
613
+ X_TEST_RAW = None
614
+ Y_TRAIN = None
615
+ Y_TEST = None
616
+ # Add a container for our pre-sampled data
617
+ X_TRAIN_SAMPLES_MAP = {}
618
+ Y_TRAIN_SAMPLES_MAP = {}
619
+
620
+ # Warm mini dataset for instant preview
621
+ X_TRAIN_WARM = None
622
+ Y_TRAIN_WARM = None
623
+
624
+ # Cache for transformed test sets (for future performance improvements)
625
+ TEST_CACHE = {}
626
+
627
+ # Initialization flags to track readiness state
628
+ INIT_FLAGS = {
629
+ "competition": False,
630
+ "dataset_core": False,
631
+ "pre_samples_small": False,
632
+ "pre_samples_medium": False,
633
+ "pre_samples_large": False,
634
+ "pre_samples_full": False,
635
+ "leaderboard": False,
636
+ "default_preprocessor": False,
637
+ "warm_mini": False,
638
+ "errors": []
639
+ }
640
+
641
+ # Lock for thread-safe flag updates
642
+ INIT_LOCK = threading.Lock()
643
+
644
+ # -------------------------------------------------------------------------
645
+ # 2. Data & Backend Utilities
646
+ # -------------------------------------------------------------------------
647
+
648
+ def _get_cache_dir():
649
+ """Get or create the cache directory for datasets."""
650
+ cache_dir = Path.home() / ".aimodelshare_cache"
651
+ cache_dir.mkdir(exist_ok=True)
652
+ return cache_dir
653
+
654
+ def _safe_request_csv(url, cache_filename="compas.csv"):
655
+ """
656
+ Request CSV from URL with local caching.
657
+ Reuses cached file if it exists and is less than CACHE_MAX_AGE_HOURS old.
658
+ """
659
+ cache_dir = _get_cache_dir()
660
+ cache_path = cache_dir / cache_filename
661
+
662
+ # Check if cache exists and is fresh
663
+ if cache_path.exists():
664
+ file_time = datetime.fromtimestamp(cache_path.stat().st_mtime)
665
+ if datetime.now() - file_time < timedelta(hours=CACHE_MAX_AGE_HOURS):
666
+ return pd.read_csv(cache_path)
667
+
668
+ # Download fresh data
669
+ response = requests.get(url, timeout=30)
670
+ response.raise_for_status()
671
+ df = pd.read_csv(StringIO(response.text))
672
+
673
+ # Save to cache
674
+ df.to_csv(cache_path, index=False)
675
+
676
+ return df
677
+
678
+ def safe_int(value, default=1):
679
+ """
680
+ Safely coerce a value to int, returning default if value is None or invalid.
681
+ Protects against TypeError when Gradio sliders receive None.
682
+ """
683
+ if value is None:
684
+ return default
685
+ try:
686
+ return int(value)
687
+ except (ValueError, TypeError):
688
+ return default
689
+
690
+ def load_and_prep_data(use_cache=True):
691
+ """
692
+ Load, sample, and prepare raw COMPAS dataset.
693
+ NOW PRE-SAMPLES ALL DATA SIZES and creates warm mini dataset.
694
+ """
695
+ url = "https://raw.githubusercontent.com/propublica/compas-analysis/master/compas-scores-two-years.csv"
696
+
697
+ # Use cached version if available
698
+ if use_cache:
699
+ try:
700
+ df = _safe_request_csv(url)
701
+ except Exception as e:
702
+ print(f"Cache failed, fetching directly: {e}")
703
+ response = requests.get(url)
704
+ df = pd.read_csv(StringIO(response.text))
705
+ else:
706
+ response = requests.get(url)
707
+ df = pd.read_csv(StringIO(response.text))
708
+
709
+ # Calculate length_of_stay
710
+ try:
711
+ df['c_jail_in'] = pd.to_datetime(df['c_jail_in'])
712
+ df['c_jail_out'] = pd.to_datetime(df['c_jail_out'])
713
+ df['length_of_stay'] = (df['c_jail_out'] - df['c_jail_in']).dt.total_seconds() / (24 * 60 * 60) # in days
714
+ except Exception:
715
+ df['length_of_stay'] = np.nan
716
+
717
+ if df.shape[0] > MAX_ROWS:
718
+ df = df.sample(n=MAX_ROWS, random_state=42)
719
+
720
+ feature_columns = ALL_NUMERIC_COLS + ALL_CATEGORICAL_COLS
721
+ feature_columns = sorted(list(set(feature_columns)))
722
+
723
+ target_column = "two_year_recid"
724
+
725
+ if "c_charge_desc" in df.columns:
726
+ top_charges = df["c_charge_desc"].value_counts().head(TOP_N_CHARGE_CATEGORICAL).index
727
+ df["c_charge_desc"] = df["c_charge_desc"].apply(
728
+ lambda x: x if pd.notna(x) and x in top_charges else "OTHER"
729
+ )
730
+
731
+ for col in feature_columns:
732
+ if col not in df.columns:
733
+ if col == 'length_of_stay' and 'length_of_stay' in df.columns:
734
+ continue
735
+ df[col] = np.nan
736
+
737
+ X = df[feature_columns].copy()
738
+ y = df[target_column].copy()
739
+
740
+ X_train_raw, X_test_raw, y_train, y_test = train_test_split(
741
+ X, y, test_size=0.25, random_state=42, stratify=y
742
+ )
743
+
744
+ # Pre-sample all data sizes
745
+ global X_TRAIN_SAMPLES_MAP, Y_TRAIN_SAMPLES_MAP, X_TRAIN_WARM, Y_TRAIN_WARM
746
+
747
+ X_TRAIN_SAMPLES_MAP["Completo (100%)"] = X_train_raw
748
+ Y_TRAIN_SAMPLES_MAP["Completo (100%)"] = y_train
749
+
750
+ for label, frac in DATA_SIZE_MAP.items():
751
+ if frac < 1.0:
752
+ X_train_sampled = X_train_raw.sample(frac=frac, random_state=42)
753
+ y_train_sampled = y_train.loc[X_train_sampled.index]
754
+ X_TRAIN_SAMPLES_MAP[label] = X_train_sampled
755
+ Y_TRAIN_SAMPLES_MAP[label] = y_train_sampled
756
+
757
+ # Create warm mini dataset for instant preview
758
+ warm_size = min(WARM_MINI_ROWS, len(X_train_raw))
759
+ X_TRAIN_WARM = X_train_raw.sample(n=warm_size, random_state=42)
760
+ Y_TRAIN_WARM = y_train.loc[X_TRAIN_WARM.index]
761
+
762
+
763
+
764
+ return X_train_raw, X_test_raw, y_train, y_test
765
+
766
+ def _background_initializer():
767
+ """
768
+ Background thread that performs sequential initialization tasks.
769
+ Updates INIT_FLAGS dict with readiness booleans and captures errors.
770
+
771
+ Initialization sequence:
772
+ 1. Competition object connection
773
+ 2. Dataset cached download and core split
774
+ 3. Warm mini dataset creation
775
+ 4. Progressive sampling: small -> medium -> large -> full
776
+ 5. Leaderboard prefetch
777
+ 6. Default preprocessor fit on small sample
778
+ """
779
+ global playground, X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST
780
+
781
+ try:
782
+ # Step 1: Connect to competition
783
+ with INIT_LOCK:
784
+ if playground is None:
785
+ playground = Competition(MY_PLAYGROUND_ID)
786
+ INIT_FLAGS["competition"] = True
787
+ except Exception as e:
788
+ with INIT_LOCK:
789
+ INIT_FLAGS["errors"].append(f"Competition connection failed: {str(e)}")
790
+
791
+ try:
792
+ # Step 2: Load dataset core (train/test split)
793
+ X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST = load_and_prep_data(use_cache=True)
794
+ with INIT_LOCK:
795
+ INIT_FLAGS["dataset_core"] = True
796
+ except Exception as e:
797
+ with INIT_LOCK:
798
+ INIT_FLAGS["errors"].append(f"Dataset loading failed: {str(e)}")
799
+ return # Cannot proceed without data
800
+
801
+ try:
802
+ # Step 3: Warm mini dataset (already created in load_and_prep_data)
803
+ if X_TRAIN_WARM is not None and len(X_TRAIN_WARM) > 0:
804
+ with INIT_LOCK:
805
+ INIT_FLAGS["warm_mini"] = True
806
+ except Exception as e:
807
+ with INIT_LOCK:
808
+ INIT_FLAGS["errors"].append(f"Warm mini dataset failed: {str(e)}")
809
+
810
+ # Progressive sampling - samples are already created in load_and_prep_data
811
+ # Just mark them as ready sequentially with delays to simulate progressive loading
812
+
813
+ try:
814
+ # Step 4a: Small sample (20%)
815
+ time.sleep(0.5) # Simulate processing
816
+ with INIT_LOCK:
817
+ INIT_FLAGS["pre_samples_small"] = True
818
+ except Exception as e:
819
+ with INIT_LOCK:
820
+ INIT_FLAGS["errors"].append(f"Small sample failed: {str(e)}")
821
+
822
+ try:
823
+ # Step 4b: Medium sample (60%)
824
+ time.sleep(0.5)
825
+ with INIT_LOCK:
826
+ INIT_FLAGS["pre_samples_medium"] = True
827
+ except Exception as e:
828
+ with INIT_LOCK:
829
+ INIT_FLAGS["errors"].append(f"Medium sample failed: {str(e)}")
830
+
831
+ try:
832
+ # Step 4c: Large sample (80%)
833
+ time.sleep(0.5)
834
+ with INIT_LOCK:
835
+ INIT_FLAGS["pre_samples_large"] = True
836
+ except Exception as e:
837
+ with INIT_LOCK:
838
+ INIT_FLAGS["errors"].append(f"Large sample failed: {str(e)}")
839
+ print(f"✗ Large sample failed: {e}")
840
+
841
+ try:
842
+ # Step 4d: Full sample (100%)
843
+ print("Background init: Full sample (100%)...")
844
+ time.sleep(0.5)
845
+ with INIT_LOCK:
846
+ INIT_FLAGS["pre_samples_full"] = True
847
+ except Exception as e:
848
+ with INIT_LOCK:
849
+ INIT_FLAGS["errors"].append(f"Full sample failed: {str(e)}")
850
+
851
+ try:
852
+ # Step 5: Leaderboard prefetch (best-effort, unauthenticated)
853
+ # Concurrency Note: Do NOT use os.environ for ambient token - prefetch
854
+ # anonymously to warm the cache for initial page loads.
855
+ if playground is not None:
856
+ _ = _get_leaderboard_with_optional_token(playground, None)
857
+ with INIT_LOCK:
858
+ INIT_FLAGS["leaderboard"] = True
859
+ except Exception as e:
860
+ with INIT_LOCK:
861
+ INIT_FLAGS["errors"].append(f"Leaderboard prefetch failed: {str(e)}")
862
+
863
+ try:
864
+ # Step 6: Default preprocessor on small sample
865
+ _fit_default_preprocessor()
866
+ with INIT_LOCK:
867
+ INIT_FLAGS["default_preprocessor"] = True
868
+ except Exception as e:
869
+ with INIT_LOCK:
870
+ INIT_FLAGS["errors"].append(f"Default preprocessor failed: {str(e)}")
871
+ print(f"✗ Default preprocessor failed: {e}")
872
+
873
+
874
+ def _fit_default_preprocessor():
875
+ """
876
+ Pre-fit a default preprocessor on the small sample with default features.
877
+ Uses memoized preprocessor builder for efficiency.
878
+ """
879
+ if "Pequeño (20%)" not in X_TRAIN_SAMPLES_MAP:
880
+ return
881
+
882
+ X_sample = X_TRAIN_SAMPLES_MAP["Pequeño (20%)"]
883
+
884
+ # Use default feature set
885
+ numeric_cols = [f for f in DEFAULT_FEATURE_SET if f in ALL_NUMERIC_COLS]
886
+ categorical_cols = [f for f in DEFAULT_FEATURE_SET if f in ALL_CATEGORICAL_COLS]
887
+
888
+ if not numeric_cols and not categorical_cols:
889
+ return
890
+
891
+ # Use memoized builder
892
+ preprocessor, selected_cols = build_preprocessor(numeric_cols, categorical_cols)
893
+ preprocessor.fit(X_sample[selected_cols])
894
+
895
+ def start_background_init():
896
+ """
897
+ Start the background initialization thread.
898
+ Should be called once at app creation.
899
+ """
900
+ thread = threading.Thread(target=_background_initializer, daemon=True)
901
+ thread.start()
902
+
903
+ def poll_init_status():
904
+ """
905
+ Poll the initialization status and return readiness bool.
906
+ Returns empty string for HTML so users don't see the checklist.
907
+
908
+ Returns:
909
+ tuple: (status_html, ready_bool)
910
+ """
911
+ with INIT_LOCK:
912
+ flags = INIT_FLAGS.copy()
913
+
914
+ # Determine if minimum requirements met
915
+ ready = flags["competition"] and flags["dataset_core"] and flags["pre_samples_small"]
916
+
917
+ return "", ready
918
+
919
+ def get_available_data_sizes():
920
+ """
921
+ Return list of data sizes that are currently available based on init flags.
922
+ """
923
+ with INIT_LOCK:
924
+ flags = INIT_FLAGS.copy()
925
+
926
+ available = []
927
+ if flags["pre_samples_small"]:
928
+ available.append("Pequeño (20%)")
929
+ if flags["pre_samples_medium"]:
930
+ available.append("Medio (60%)")
931
+ if flags["pre_samples_large"]:
932
+ available.append("Grande (80%)")
933
+ if flags["pre_samples_full"]:
934
+ available.append("Completo (100%)")
935
+
936
+ return available if available else ["Pequeño (20%)"] # Fallback
937
+
938
+ def _is_ready() -> bool:
939
+ """
940
+ Check if initialization is complete and system is ready for real submissions.
941
+
942
+ Returns:
943
+ bool: True if competition, dataset, and small sample are initialized
944
+ """
945
+ with INIT_LOCK:
946
+ flags = INIT_FLAGS.copy()
947
+ return flags["competition"] and flags["dataset_core"] and flags["pre_samples_small"]
948
+
949
+ def _get_user_latest_accuracy(df: Optional[pd.DataFrame], username: str) -> Optional[float]:
950
+ """
951
+ Extract the user's latest submission accuracy from the leaderboard.
952
+
953
+ Uses timestamp sorting when available; otherwise assumes last row is latest.
954
+
955
+ Args:
956
+ df: Leaderboard DataFrame
957
+ username: Username to extract accuracy for
958
+
959
+ Returns:
960
+ float: Latest submission accuracy, or None if not found/invalid
961
+ """
962
+ if df is None or df.empty:
963
+ return None
964
+
965
+ try:
966
+ user_rows = df[df["username"] == username]
967
+ if user_rows.empty or "accuracy" not in user_rows.columns:
968
+ return None
969
+
970
+ # Try timestamp-based sorting if available
971
+ if "timestamp" in user_rows.columns:
972
+ user_rows = user_rows.copy()
973
+ user_rows["__parsed_ts"] = pd.to_datetime(user_rows["timestamp"], errors="coerce")
974
+ valid_ts = user_rows[user_rows["__parsed_ts"].notna()]
975
+
976
+ if not valid_ts.empty:
977
+ # Sort by timestamp and get latest
978
+ latest_row = valid_ts.sort_values("__parsed_ts", ascending=False).iloc[0]
979
+ return float(latest_row["accuracy"])
980
+
981
+ # Fallback: assume last row is latest (append order)
982
+ return float(user_rows.iloc[-1]["accuracy"])
983
+
984
+ except Exception as e:
985
+ _log(f"Error extracting latest accuracy for {username}: {e}")
986
+ return None
987
+
988
+ def _get_user_latest_ts(df: Optional[pd.DataFrame], username: str) -> Optional[float]:
989
+ """
990
+ Extract the user's latest valid timestamp from the leaderboard.
991
+
992
+ Args:
993
+ df: Leaderboard DataFrame
994
+ username: Username to extract timestamp for
995
+
996
+ Returns:
997
+ float: Latest timestamp as unix epoch, or None if not found/invalid
998
+ """
999
+ if df is None or df.empty:
1000
+ return None
1001
+
1002
+ try:
1003
+ user_rows = df[df["username"] == username]
1004
+ if user_rows.empty or "timestamp" not in user_rows.columns:
1005
+ return None
1006
+
1007
+ # Parse timestamps and get the latest
1008
+ user_rows = user_rows.copy()
1009
+ user_rows["__parsed_ts"] = pd.to_datetime(user_rows["timestamp"], errors="coerce")
1010
+ valid_ts = user_rows[user_rows["__parsed_ts"].notna()]
1011
+
1012
+ if valid_ts.empty:
1013
+ return None
1014
+
1015
+ latest_ts = valid_ts["__parsed_ts"].max()
1016
+ return latest_ts.timestamp() if pd.notna(latest_ts) else None
1017
+ except Exception as e:
1018
+ _log(f"Error extracting latest timestamp for {username}: {e}")
1019
+ return None
1020
+
1021
+ def _user_rows_changed(
1022
+ refreshed_leaderboard: Optional[pd.DataFrame],
1023
+ username: str,
1024
+ old_row_count: int,
1025
+ old_best_score: float,
1026
+ old_latest_ts: Optional[float] = None,
1027
+ old_latest_score: Optional[float] = None
1028
+ ) -> bool:
1029
+ """
1030
+ Check if user's leaderboard entries have changed after submission.
1031
+
1032
+ Used after polling to detect if the leaderboard has updated with the new submission.
1033
+ Checks row count (new submission added), best score (score improved), latest timestamp,
1034
+ and latest accuracy (handles backend overwrite without append).
1035
+
1036
+ Args:
1037
+ refreshed_leaderboard: Fresh leaderboard data
1038
+ username: Username to check for
1039
+ old_row_count: Previous number of submissions for this user
1040
+ old_best_score: Previous best accuracy score
1041
+ old_latest_ts: Previous latest timestamp (unix epoch), optional
1042
+ old_latest_score: Previous latest submission accuracy, optional
1043
+
1044
+ Returns:
1045
+ bool: True if user has more rows, better score, newer timestamp, or changed latest accuracy
1046
+ """
1047
+ if refreshed_leaderboard is None or refreshed_leaderboard.empty:
1048
+ return False
1049
+
1050
+ try:
1051
+ user_rows = refreshed_leaderboard[refreshed_leaderboard["username"] == username]
1052
+ if user_rows.empty:
1053
+ return False
1054
+
1055
+ new_row_count = len(user_rows)
1056
+ new_best_score = float(user_rows["accuracy"].max()) if "accuracy" in user_rows.columns else 0.0
1057
+ new_latest_ts = _get_user_latest_ts(refreshed_leaderboard, username)
1058
+ new_latest_score = _get_user_latest_accuracy(refreshed_leaderboard, username)
1059
+
1060
+ # Changed if we have more submissions, better score, newer timestamp, or changed latest accuracy
1061
+ changed = (new_row_count > old_row_count) or (new_best_score > old_best_score + 0.0001)
1062
+
1063
+ # Check timestamp if available
1064
+ if old_latest_ts is not None and new_latest_ts is not None:
1065
+ changed = changed or (new_latest_ts > old_latest_ts)
1066
+
1067
+ # Check latest accuracy change (handles overwrite-without-append case)
1068
+ if old_latest_score is not None and new_latest_score is not None:
1069
+ accuracy_changed = abs(new_latest_score - old_latest_score) >= 0.00001
1070
+ if accuracy_changed:
1071
+ _log(f"Latest accuracy changed: {old_latest_score:.4f} -> {new_latest_score:.4f}")
1072
+ changed = changed or accuracy_changed
1073
+
1074
+ if changed:
1075
+ _log(f"User rows changed for {username}:")
1076
+ _log(f" Row count: {old_row_count} -> {new_row_count}")
1077
+ _log(f" Best score: {old_best_score:.4f} -> {new_best_score:.4f}")
1078
+ _log(f" Latest score: {old_latest_score if old_latest_score else 'N/A'} -> {new_latest_score if new_latest_score else 'N/A'}")
1079
+ _log(f" Timestamp: {old_latest_ts} -> {new_latest_ts}")
1080
+
1081
+ return changed
1082
+ except Exception as e:
1083
+ _log(f"Error checking user rows: {e}")
1084
+ return False
1085
+
1086
+ @functools.lru_cache(maxsize=32)
1087
+ def _get_cached_preprocessor_config(numeric_cols_tuple, categorical_cols_tuple):
1088
+ """
1089
+ Create and return preprocessor configuration (memoized).
1090
+ Uses tuples for hashability in lru_cache.
1091
+
1092
+ Concurrency Note: Uses sparse_output=True for OneHotEncoder to reduce memory
1093
+ footprint under concurrent requests. Downstream models that require dense
1094
+ arrays (DecisionTree, RandomForest) will convert via .toarray() as needed.
1095
+ LogisticRegression and KNeighborsClassifier handle sparse matrices natively.
1096
+
1097
+ Returns tuple of (transformers_list, selected_columns) ready for ColumnTransformer.
1098
+ """
1099
+ numeric_cols = list(numeric_cols_tuple)
1100
+ categorical_cols = list(categorical_cols_tuple)
1101
+
1102
+ transformers = []
1103
+ selected_cols = []
1104
+
1105
+ if numeric_cols:
1106
+ num_tf = Pipeline(steps=[
1107
+ ("imputer", SimpleImputer(strategy="median")),
1108
+ ("scaler", StandardScaler())
1109
+ ])
1110
+ transformers.append(("num", num_tf, numeric_cols))
1111
+ selected_cols.extend(numeric_cols)
1112
+
1113
+ if categorical_cols:
1114
+ # Use sparse_output=True to reduce memory footprint
1115
+ cat_tf = Pipeline(steps=[
1116
+ ("imputer", SimpleImputer(strategy="constant", fill_value="missing")),
1117
+ ("onehot", OneHotEncoder(handle_unknown="ignore", sparse_output=True))
1118
+ ])
1119
+ transformers.append(("cat", cat_tf, categorical_cols))
1120
+ selected_cols.extend(categorical_cols)
1121
+
1122
+ return transformers, selected_cols
1123
+
1124
+ def build_preprocessor(numeric_cols, categorical_cols):
1125
+ """
1126
+ Build a preprocessor using cached configuration.
1127
+ The configuration (pipeline structure) is memoized; the actual fit is not.
1128
+
1129
+ Note: Returns sparse matrices when categorical columns are present.
1130
+ Use _ensure_dense() helper if model requires dense input.
1131
+ """
1132
+ # Convert to tuples for caching
1133
+ numeric_tuple = tuple(sorted(numeric_cols))
1134
+ categorical_tuple = tuple(sorted(categorical_cols))
1135
+
1136
+ transformers, selected_cols = _get_cached_preprocessor_config(numeric_tuple, categorical_tuple)
1137
+
1138
+ # Create new ColumnTransformer with cached config
1139
+ preprocessor = ColumnTransformer(transformers=transformers, remainder="drop")
1140
+
1141
+ return preprocessor, selected_cols
1142
+
1143
+ def _ensure_dense(X):
1144
+ """
1145
+ Convert sparse matrix to dense if necessary.
1146
+
1147
+ Helper function for models that don't support sparse input
1148
+ (DecisionTree, RandomForest). LogisticRegression and KNN
1149
+ handle sparse matrices natively.
1150
+ """
1151
+ from scipy import sparse
1152
+ if sparse.issparse(X):
1153
+ return X.toarray()
1154
+ return X
1155
+
1156
+ def tune_model_complexity(model, level):
1157
+ """
1158
+ Map a 1–10 slider value to model hyperparameters.
1159
+ Levels 1–3: Conservative / simple
1160
+ Levels 4–7: Balanced
1161
+ Levels 8–10: Aggressive / risk of overfitting
1162
+ """
1163
+ level = int(level)
1164
+ if isinstance(model, LogisticRegression):
1165
+ 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}
1166
+ model.C = c_map.get(level, 1.0)
1167
+ model.max_iter = max(getattr(model, "max_iter", 0), 500)
1168
+ elif isinstance(model, RandomForestClassifier):
1169
+ depth_map = {1: 3, 2: 5, 3: 7, 4: 9, 5: 11, 6: 15, 7: 20, 8: 25, 9: None, 10: None}
1170
+ est_map = {1: 20, 2: 30, 3: 40, 4: 60, 5: 80, 6: 100, 7: 120, 8: 150, 9: 180, 10: 220}
1171
+ model.max_depth = depth_map.get(level, 10)
1172
+ model.n_estimators = est_map.get(level, 100)
1173
+ elif isinstance(model, DecisionTreeClassifier):
1174
+ depth_map = {1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 8, 7: 10, 8: 12, 9: 15, 10: None}
1175
+ model.max_depth = depth_map.get(level, 6)
1176
+ elif isinstance(model, KNeighborsClassifier):
1177
+ k_map = {1: 100, 2: 75, 3: 60, 4: 50, 5: 40, 6: 30, 7: 25, 8: 15, 9: 7, 10: 3}
1178
+ model.n_neighbors = k_map.get(level, 25)
1179
+ return model
1180
+
1181
+ # --- New Helper Functions for HTML Generation ---
1182
+
1183
+ def _normalize_team_name(name: str) -> str:
1184
+ """
1185
+ Normalize team name for consistent comparison and storage.
1186
+
1187
+ Strips leading/trailing whitespace and collapses multiple spaces into single spaces.
1188
+ This ensures consistent formatting across environment variables, state, and leaderboard rendering.
1189
+
1190
+ Args:
1191
+ name: Team name to normalize (can be None or empty)
1192
+
1193
+ Returns:
1194
+ str: Normalized team name, or empty string if input is None/empty
1195
+
1196
+ Examples:
1197
+ >>> _normalize_team_name(" The Ethical Explorers ")
1198
+ 'The Ethical Explorers'
1199
+ >>> _normalize_team_name("The Moral Champions")
1200
+ 'The Moral Champions'
1201
+ >>> _normalize_team_name(None)
1202
+ ''
1203
+ """
1204
+ if not name:
1205
+ return ""
1206
+ return " ".join(str(name).strip().split())
1207
+
1208
+
1209
+ # Team name translation helpers for UI display (Spanish)
1210
+ def translate_team_name_for_display(team_en: str, lang: str = "es") -> str:
1211
+ """
1212
+ Translate a canonical English team name to the specified language for UI display.
1213
+ Fallback to English if translation not found.
1214
+
1215
+ Internal logic always uses canonical English names. This is only for UI display.
1216
+ """
1217
+ if lang not in TEAM_NAME_TRANSLATIONS:
1218
+ lang = "en"
1219
+ return TEAM_NAME_TRANSLATIONS[lang].get(team_en, team_en)
1220
+
1221
+
1222
+ def translate_team_name_to_english(display_name: str, lang: str = "es") -> str:
1223
+ """
1224
+ Reverse lookup: given a localized team name, return the canonical English name.
1225
+ Returns the original display_name if not found.
1226
+
1227
+ For future use if user input needs to be normalized back to English.
1228
+ """
1229
+ if lang not in TEAM_NAME_TRANSLATIONS:
1230
+ return display_name # Already English or unknown
1231
+
1232
+ translations = TEAM_NAME_TRANSLATIONS[lang]
1233
+ for english_name, localized_name in translations.items():
1234
+ if localized_name == display_name:
1235
+ return english_name
1236
+ return display_name
1237
+
1238
+
1239
+ def _format_leaderboard_for_display(df: Optional[pd.DataFrame], lang: str = "es") -> Optional[pd.DataFrame]:
1240
+ """
1241
+ Create a copy of the leaderboard DataFrame with team names translated for display.
1242
+ Does not mutate the original DataFrame.
1243
+
1244
+ For potential future use when displaying full leaderboard.
1245
+ Internal logic should always use the original DataFrame with English team names.
1246
+ """
1247
+ if df is None:
1248
+ return None
1249
+
1250
+ if df.empty or "Team" not in df.columns:
1251
+ return df.copy()
1252
+
1253
+ df_display = df.copy()
1254
+ df_display["Team"] = df_display["Team"].apply(lambda t: translate_team_name_for_display(t, lang))
1255
+ return df_display
1256
+
1257
+
1258
+ def _build_skeleton_leaderboard(rows=6, is_team=True, submit_button_label="5. 🔬 Construir y enviar el modelo"):
1259
+ context_label = "Equipo" if is_team else "Individual"
1260
+ return f"""
1261
+ <div class='lb-placeholder' aria-live='polite'>
1262
+ <div class='lb-placeholder-title'>{context_label} · Clasificación pendiente</div>
1263
+ <div class='lb-placeholder-sub'>
1264
+ <p style='margin:0 0 6px 0;'>Envía tu primer modelo para desbloquear la clasificación.</p>
1265
+ <p style='margin:0;'><strong>Haz clic en «{submit_button_label}» (abajo a la izquierda)</strong> para comenzar!</p>
1266
+ </div>
1267
+ </div>
1268
+ """
1269
+ # --- FIX APPLIED HERE ---
1270
+ def build_login_prompt_html():
1271
+ """
1272
+ Generate HTML for the login prompt text *only*.
1273
+ The styled preview card will be prepended to this.
1274
+ """
1275
+ return f"""
1276
+ <h2 style='color: #111827; margin-top:20px; border-top: 2px solid #e5e7eb; padding-top: 20px;'>🔐 Sign in to submit & rank</h2>
1277
+ <div style='margin-top:16px; text-align:left; font-size:1rem; line-height:1.6; color:#374151;'>
1278
+ <p style='margin:12px 0;'>
1279
+ This is a preview run only. Sign in to publish your score to the live leaderboard,
1280
+ earn promotions, and contribute team points.
1281
+ </p>
1282
+ <p style='margin:12px 0;'>
1283
+ <strong>New user?</strong> Create a free account at
1284
+ <a href='https://www.modelshare.ai/login' target='_blank'
1285
+ style='color:#4f46e5; text-decoration:underline;'>modelshare.ai/login</a>
1286
+ </p>
1287
+ </div>
1288
+ """
1289
+ # --- END OF FIX ---
1290
+
1291
+ 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):
1292
+ """Generates the HTML for the KPI feedback card. Supports preview mode label and pending state."""
1293
+
1294
+ # Handle pending state - show processing message with provisional diff
1295
+ if is_pending:
1296
+ title = "⏳ Submission Processing"
1297
+ acc_color = "#3b82f6" # Blue
1298
+ acc_text = f"{(local_test_accuracy * 100):.2f}%" if local_test_accuracy is not None else "N/A"
1299
+
1300
+ # Compute provisional diff between local (new) and last score
1301
+ if local_test_accuracy is not None and last_score is not None and last_score > 0:
1302
+ score_diff = local_test_accuracy - last_score
1303
+ if abs(score_diff) < 0.0001:
1304
+ 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>"
1305
+ elif score_diff > 0:
1306
+ 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>"
1307
+ else:
1308
+ 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>"
1309
+ else:
1310
+ # No last score available - just show pending message
1311
+ acc_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>Pending leaderboard update...</p>"
1312
+
1313
+ border_color = acc_color
1314
+ rank_color = "#6b7280" # Gray
1315
+ rank_text = "Pending"
1316
+ rank_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0;'>Calculating rank...</p>"
1317
+
1318
+ # Handle preview mode - Styled to match "success" card
1319
+ elif is_preview:
1320
+ title = "🔬 Successful Preview Run!"
1321
+ acc_color = "#16a34a" # Green (like success)
1322
+ acc_text = f"{(new_score * 100):.2f}%" if new_score > 0 else "N/A"
1323
+ 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
1324
+ border_color = acc_color # Green border
1325
+ rank_color = "#3b82f6" # Blue (like rank)
1326
+ rank_text = "N/A" # Placeholder
1327
+ rank_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0;'>Not ranked (preview)</p>" # Neutral color
1328
+
1329
+ # 1. Handle First Submission
1330
+ elif submission_count == 0:
1331
+ title = "🎉 First Model Submitted!"
1332
+ acc_color = "#16a34a" # green
1333
+ acc_text = f"{(new_score * 100):.2f}%"
1334
+ acc_diff_html = "<p style='font-size: 1.2rem; font-weight: 500; color: #6b7280; margin:0; padding-top: 8px;'>(Your first score!)</p>"
1335
+
1336
+ rank_color = "#3b82f6" # blue
1337
+ rank_text = f"#{new_rank}"
1338
+ rank_diff_html = "<p style='font-size: 1.5rem; font-weight: 600; color: #3b82f6; margin:0;'>You're on the board!</p>"
1339
+ border_color = acc_color
1340
+
1341
+ else:
1342
+ # 2. Handle Score Changes
1343
+ score_diff = new_score - last_score
1344
+ if abs(score_diff) < 0.0001:
1345
+ title = "✅ Submission Successful"
1346
+ acc_color = "#6b7280" # gray
1347
+ acc_text = f"{(new_score * 100):.2f}%"
1348
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {acc_color}; margin:0;'>No Change (↔)</p>"
1349
+ border_color = acc_color
1350
+ elif score_diff > 0:
1351
+ title = "✅ Submission Successful!"
1352
+ acc_color = "#16a34a" # green
1353
+ acc_text = f"{(new_score * 100):.2f}%"
1354
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {acc_color}; margin:0;'>+{(score_diff * 100):.2f} (⬆️)</p>"
1355
+ border_color = acc_color
1356
+ else:
1357
+ title = "📉 Score Dropped"
1358
+ acc_color = "#ef4444" # red
1359
+ acc_text = f"{(new_score * 100):.2f}%"
1360
+ acc_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {acc_color}; margin:0;'>{(score_diff * 100):.2f} (⬇️)</p>"
1361
+ border_color = acc_color
1362
+
1363
+ # 3. Handle Rank Changes
1364
+ rank_diff = last_rank - new_rank
1365
+ rank_color = "#3b82f6" # blue
1366
+ rank_text = f"#{new_rank}"
1367
+ if last_rank == 0: # Handle first rank
1368
+ rank_diff_html = "<p style='font-size: 1.5rem; font-weight: 600; color: #3b82f6; margin:0;'>You're on the board!</p>"
1369
+ elif rank_diff > 0:
1370
+ 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>"
1371
+ elif rank_diff < 0:
1372
+ 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>"
1373
+ else:
1374
+ rank_diff_html = f"<p style='font-size: 1.5rem; font-weight: 600; color: {rank_color}; margin:0;'>No Change (↔)</p>"
1375
+
1376
+ return f"""
1377
+ <div class='kpi-card' style='border-color: {border_color};'>
1378
+ <h2 style='color: #111827; margin-top:0;'>{title}</h2>
1379
+ <div class='kpi-card-body'>
1380
+ <div class='kpi-metric-box'>
1381
+ <p class='kpi-label'>New Accuracy</p>
1382
+ <p class='kpi-score' style='color: {acc_color};'>{acc_text}</p>
1383
+ {acc_diff_html}
1384
+ </div>
1385
+ <div class='kpi-metric-box'>
1386
+ <p class='kpi-label'>Your Rank</p>
1387
+ <p class='kpi-score' style='color: {rank_color};'>{rank_text}</p>
1388
+ {rank_diff_html}
1389
+ </div>
1390
+ </div>
1391
+ </div>
1392
+ """
1393
+
1394
+ def _build_team_html(team_summary_df, team_name):
1395
+ """
1396
+ Generates the HTML for the team leaderboard.
1397
+
1398
+ Uses normalized, case-insensitive comparison to highlight the user's team row,
1399
+ ensuring reliable highlighting even with whitespace or casing variations.
1400
+
1401
+ Team names are translated to Spanish for display only. Internal comparisons
1402
+ use the unmodified English team names from the DataFrame.
1403
+ """
1404
+ if team_summary_df is None or team_summary_df.empty:
1405
+ return "<p style='text-align:center; color:#6b7280; padding-top:20px;'>No team submissions yet.</p>"
1406
+
1407
+ # Normalize the current user's team name for comparison (using English names)
1408
+ normalized_user_team = _normalize_team_name(team_name).lower()
1409
+
1410
+ header = """
1411
+ <table class='leaderboard-html-table'>
1412
+ <thead>
1413
+ <tr>
1414
+ <th>Rank</th>
1415
+ <th>Team</th>
1416
+ <th>Best_Score</th>
1417
+ <th>Avg_Score</th>
1418
+ <th>Submissions</th>
1419
+ </tr>
1420
+ </thead>
1421
+ <tbody>
1422
+ """
1423
+
1424
+ body = ""
1425
+ for index, row in team_summary_df.iterrows():
1426
+ # Normalize the row's team name and compare case-insensitively (using English names)
1427
+ normalized_row_team = _normalize_team_name(row["Team"]).lower()
1428
+ is_user_team = normalized_row_team == normalized_user_team
1429
+ row_class = "class='user-row-highlight'" if is_user_team else ""
1430
+
1431
+ # Translate team name to Spanish for display only
1432
+ display_team_name = translate_team_name_for_display(row["Team"], UI_TEAM_LANG)
1433
+
1434
+ body += f"""
1435
+ <tr {row_class}>
1436
+ <td>{index}</td>
1437
+ <td>{display_team_name}</td>
1438
+ <td>{(row['Best_Score'] * 100):.2f}%</td>
1439
+ <td>{(row['Avg_Score'] * 100):.2f}%</td>
1440
+ <td>{row['Submissions']}</td>
1441
+ </tr>
1442
+ """
1443
+
1444
+ footer = "</tbody></table>"
1445
+ return header + body + footer
1446
+
1447
+ def _build_individual_html(individual_summary_df, username):
1448
+ """Generates the HTML for the individual leaderboard."""
1449
+ if individual_summary_df is None or individual_summary_df.empty:
1450
+ return "<p style='text-align:center; color:#6b7280; padding-top:20px;'>No individual submissions yet.</p>"
1451
+
1452
+ header = """
1453
+ <table class='leaderboard-html-table'>
1454
+ <thead>
1455
+ <tr>
1456
+ <th>Rank</th>
1457
+ <th>Engineer</th>
1458
+ <th>Best_Score</th>
1459
+ <th>Submissions</th>
1460
+ </tr>
1461
+ </thead>
1462
+ <tbody>
1463
+ """
1464
+
1465
+ body = ""
1466
+ for index, row in individual_summary_df.iterrows():
1467
+ is_user = row["Engineer"] == username
1468
+ row_class = "class='user-row-highlight'" if is_user else ""
1469
+ body += f"""
1470
+ <tr {row_class}>
1471
+ <td>{index}</td>
1472
+ <td>{row['Engineer']}</td>
1473
+ <td>{(row['Best_Score'] * 100):.2f}%</td>
1474
+ <td>{row['Submissions']}</td>
1475
+ </tr>
1476
+ """
1477
+
1478
+ footer = "</tbody></table>"
1479
+ return header + body + footer
1480
+
1481
+
1482
+
1483
+
1484
+ # --- End Helper Functions ---
1485
+
1486
+
1487
+ def generate_competitive_summary(leaderboard_df, team_name, username, last_submission_score, last_rank, submission_count):
1488
+ """
1489
+ Build summaries, HTML, and KPI card.
1490
+
1491
+ Concurrency Note: Uses the team_name parameter directly for team highlighting,
1492
+ NOT os.environ, to prevent cross-user data leakage under concurrent requests.
1493
+
1494
+ Returns (team_html, individual_html, kpi_card_html, new_best_accuracy, new_rank, this_submission_score).
1495
+ """
1496
+ team_summary_df = pd.DataFrame(columns=["Team", "Best_Score", "Avg_Score", "Submissions"])
1497
+ individual_summary_df = pd.DataFrame(columns=["Engineer", "Best_Score", "Submissions"])
1498
+
1499
+ if leaderboard_df is None or leaderboard_df.empty or "accuracy" not in leaderboard_df.columns:
1500
+ return (
1501
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Leaderboard empty.</p>",
1502
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Leaderboard empty.</p>",
1503
+ _build_kpi_card_html(0, 0, 0, 0, 0, is_preview=False, is_pending=False, local_test_accuracy=None),
1504
+ 0.0, 0, 0.0
1505
+ )
1506
+
1507
+ # Team summary
1508
+ if "Team" in leaderboard_df.columns:
1509
+ team_summary_df = (
1510
+ leaderboard_df.groupby("Team")["accuracy"]
1511
+ .agg(Best_Score="max", Avg_Score="mean", Submissions="count")
1512
+ .reset_index()
1513
+ .sort_values("Best_Score", ascending=False)
1514
+ .reset_index(drop=True)
1515
+ )
1516
+ team_summary_df.index = team_summary_df.index + 1
1517
+
1518
+ # Individual summary
1519
+ user_bests = leaderboard_df.groupby("username")["accuracy"].max()
1520
+ user_counts = leaderboard_df.groupby("username")["accuracy"].count()
1521
+ individual_summary_df = pd.DataFrame(
1522
+ {"Engineer": user_bests.index, "Best_Score": user_bests.values, "Submissions": user_counts.values}
1523
+ ).sort_values("Best_Score", ascending=False).reset_index(drop=True)
1524
+ individual_summary_df.index = individual_summary_df.index + 1
1525
+
1526
+ # Get stats for KPI card
1527
+ new_rank = 0
1528
+ new_best_accuracy = 0.0
1529
+ this_submission_score = 0.0
1530
+
1531
+ try:
1532
+ # All submissions for this user
1533
+ user_rows = leaderboard_df[leaderboard_df["username"] == username].copy()
1534
+
1535
+ if not user_rows.empty:
1536
+ # Attempt robust timestamp parsing
1537
+ if "timestamp" in user_rows.columns:
1538
+ parsed_ts = pd.to_datetime(user_rows["timestamp"], errors="coerce")
1539
+
1540
+ if parsed_ts.notna().any():
1541
+ # At least one valid timestamp → use parsed ordering
1542
+ user_rows["__parsed_ts"] = parsed_ts
1543
+ user_rows = user_rows.sort_values("__parsed_ts", ascending=False)
1544
+ this_submission_score = float(user_rows.iloc[0]["accuracy"])
1545
+ else:
1546
+ # All timestamps invalid → assume append order, take last as "latest"
1547
+ this_submission_score = float(user_rows.iloc[-1]["accuracy"])
1548
+ else:
1549
+ # No timestamp column → fallback to last row
1550
+ this_submission_score = float(user_rows.iloc[-1]["accuracy"])
1551
+
1552
+ # Rank & best accuracy (unchanged logic, but make sure we use the same best row)
1553
+ my_rank_row = None
1554
+ # Build individual summary before this block (already done above)
1555
+ my_rank_row = individual_summary_df[individual_summary_df["Engineer"] == username]
1556
+ if not my_rank_row.empty:
1557
+ new_rank = my_rank_row.index[0]
1558
+ new_best_accuracy = float(my_rank_row["Best_Score"].iloc[0])
1559
+
1560
+ except Exception as e:
1561
+ _log(f"Latest submission score extraction failed: {e}")
1562
+
1563
+ # Generate HTML outputs
1564
+ # Concurrency Note: Use team_name parameter directly, not os.environ
1565
+ team_html = _build_team_html(team_summary_df, team_name)
1566
+ individual_html = _build_individual_html(individual_summary_df, username)
1567
+ kpi_card_html = _build_kpi_card_html(
1568
+ this_submission_score, last_submission_score, new_rank, last_rank, submission_count,
1569
+ is_preview=False, is_pending=False, local_test_accuracy=None
1570
+ )
1571
+
1572
+ return team_html, individual_html, kpi_card_html, new_best_accuracy, new_rank, this_submission_score
1573
+
1574
+
1575
+ def get_model_card(model_name):
1576
+ # Retrieve the Spanish description stored in "card_es"
1577
+ return MODEL_TYPES.get(model_name, {}).get("card_es", "Descripción no disponible.")
1578
+
1579
+ def compute_rank_settings(
1580
+ submission_count,
1581
+ current_model,
1582
+ current_complexity,
1583
+ current_feature_set,
1584
+ current_data_size
1585
+ ):
1586
+ """
1587
+ Returns rank gating settings.
1588
+ Adapted for Spanish UI: Returns Tuple choices [(Display, Value)]
1589
+ """
1590
+
1591
+ def get_choices_for_rank(rank):
1592
+ if rank == 0:
1593
+ return [opt for opt in FEATURE_SET_ALL_OPTIONS if opt[1] in FEATURE_SET_GROUP_1_VALS]
1594
+ if rank == 1:
1595
+ return [opt for opt in FEATURE_SET_ALL_OPTIONS if opt[1] in (FEATURE_SET_GROUP_1_VALS + FEATURE_SET_GROUP_2_VALS)]
1596
+ return FEATURE_SET_ALL_OPTIONS
1597
+
1598
+ # Helper to generate Model Radio Tuples [(Spanish, English)]
1599
+ def get_model_tuples(available_english_keys):
1600
+ return [(MODEL_DISPLAY_MAP[k], k) for k in available_english_keys if k in MODEL_DISPLAY_MAP]
1601
+
1602
+ # Rank 0: Trainee
1603
+ if submission_count == 0:
1604
+ avail_keys = ["The Balanced Generalist"]
1605
+ return {
1606
+ "rank_message": "# 🧑‍🎓 Rango: Ingeniero en Pràcticas\n<p style='font-size:24px; line-height:1.4;'>Para tu primer envío, simplemente haz clic en el botón grande '🔬 Construir y Enviar Modelo' abajo.</p>",
1607
+ "model_choices": get_model_tuples(avail_keys),
1608
+ "model_value": "The Balanced Generalist",
1609
+ "model_interactive": False,
1610
+ "complexity_max": 3,
1611
+ "complexity_value": min(current_complexity, 3),
1612
+ "feature_set_choices": get_choices_for_rank(0),
1613
+ "feature_set_value": FEATURE_SET_GROUP_1_VALS,
1614
+ "feature_set_interactive": False,
1615
+ "data_size_choices": ["Pequeño (20%)"],
1616
+ "data_size_value": "Pequeño (20%)",
1617
+ "data_size_interactive": False,
1618
+ }
1619
+
1620
+ # Rank 1: Junior
1621
+ elif submission_count == 1:
1622
+ avail_keys = ["The Balanced Generalist", "The Rule-Maker", "The 'Nearest Neighbor'"]
1623
+ return {
1624
+ "rank_message": "# 🎉 ¡Subida de Rango! Ingeniero Junior\n<p style='font-size:24px; line-height:1.4;'>¡Nuevos modelos, tamaños de datos e ingredientes de datos desbloqueados!</p>",
1625
+ "model_choices": get_model_tuples(avail_keys),
1626
+ "model_value": current_model if current_model in avail_keys else "The Balanced Generalist",
1627
+ "model_interactive": True,
1628
+ "complexity_max": 6,
1629
+ "complexity_value": min(current_complexity, 6),
1630
+ "feature_set_choices": get_choices_for_rank(1),
1631
+ "feature_set_value": current_feature_set,
1632
+ "feature_set_interactive": True,
1633
+ "data_size_choices": ["Pequeño (20%)", "Medio (60%)"],
1634
+ "data_size_value": current_data_size if current_data_size in ["Pequeño (20%)", "Medio (60%)"] else "Pequeño (20%)",
1635
+ "data_size_interactive": True,
1636
+ }
1637
+
1638
+ # Rank 2: Senior
1639
+ elif submission_count == 2:
1640
+ avail_keys = list(MODEL_TYPES.keys())
1641
+ return {
1642
+ "rank_message": "# 🌟 ¡Subida de Rango! Ingeniero Senior\n<p style='font-size:24px; line-height:1.4;'>¡Ingredientes de datos más potentes desbloqueados! Los predictores más fuertes (como 'Edad' y 'Historial Delictivo') ya están disponibles. Probablemente mejorarán tu precisión, pero recuerda que a menudo conllevan mayor sesgo social.</p>",
1643
+ "model_choices": get_model_tuples(avail_keys),
1644
+ "model_value": current_model if current_model in avail_keys else "The Deep Pattern-Finder",
1645
+ "model_interactive": True,
1646
+ "complexity_max": 8,
1647
+ "complexity_value": min(current_complexity, 8),
1648
+ "feature_set_choices": get_choices_for_rank(2),
1649
+ "feature_set_value": current_feature_set,
1650
+ "feature_set_interactive": True,
1651
+ "data_size_choices": ["Pequeño (20%)", "Medio (60%)", "Grande (80%)", "Completo (100%)"],
1652
+ "data_size_value": current_data_size if current_data_size in DATA_SIZE_DB_MAP else "Pequeño (20%)",
1653
+ "data_size_interactive": True,
1654
+ }
1655
+
1656
+ # Rank 3+: Lead
1657
+ else:
1658
+ avail_keys = list(MODEL_TYPES.keys())
1659
+ return {
1660
+ "rank_message": "# 👑 Rango: Ingeniero Líder\n<p style='font-size:24px; line-height:1.4;'>Todas las herramientas desbloqueadas — ¡optimiza libremente!</p>",
1661
+ "model_choices": get_model_tuples(avail_keys),
1662
+ "model_value": current_model if current_model in avail_keys else "The Balanced Generalist",
1663
+ "model_interactive": True,
1664
+ "complexity_max": 10,
1665
+ "complexity_value": current_complexity,
1666
+ "feature_set_choices": get_choices_for_rank(3),
1667
+ "feature_set_value": current_feature_set,
1668
+ "feature_set_interactive": True,
1669
+ "data_size_choices": ["Pequeño (20%)", "Medio (60%)", "Grande (80%)", "Completo (100%)"],
1670
+ "data_size_value": current_data_size if current_data_size in DATA_SIZE_DB_MAP else "Pequeño (20%)",
1671
+ "data_size_interactive": True,
1672
+ }
1673
+
1674
+ # Find components by name to yield updates
1675
+ # --- Existing global component placeholders ---
1676
+ submit_button = None
1677
+ submission_feedback_display = None
1678
+ team_leaderboard_display = None
1679
+ individual_leaderboard_display = None
1680
+ last_submission_score_state = None
1681
+ last_rank_state = None
1682
+ best_score_state = None
1683
+ submission_count_state = None
1684
+ rank_message_display = None
1685
+ model_type_radio = None
1686
+ complexity_slider = None
1687
+ feature_set_checkbox = None
1688
+ data_size_radio = None
1689
+ attempts_tracker_display = None
1690
+ team_name_state = None
1691
+ # Login components
1692
+ login_username = None
1693
+ login_password = None
1694
+ login_submit = None
1695
+ login_error = None
1696
+ # Add missing placeholders for auth states (FIX)
1697
+ username_state = None
1698
+ token_state = None
1699
+ first_submission_score_state = None # (already commented as "will be assigned globally")
1700
+ # Add state placeholders for readiness gating and preview tracking
1701
+ readiness_state = None
1702
+ was_preview_state = None
1703
+ kpi_meta_state = None
1704
+ last_seen_ts_state = None # Track last seen user timestamp from leaderboard
1705
+
1706
+
1707
+ def get_or_assign_team(username, token=None):
1708
+ """
1709
+ Get the existing team for a user from the leaderboard, or assign a new random team.
1710
+
1711
+ Queries the playground leaderboard to check if the user has prior submissions with
1712
+ a team assignment. If found, returns that team (most recent if multiple submissions).
1713
+ Otherwise assigns a random team. All team names are normalized for consistency.
1714
+
1715
+ Args:
1716
+ username: str, the username to check for existing team
1717
+ token: str, optional authentication token for leaderboard fetch
1718
+
1719
+ Returns:
1720
+ tuple: (team_name: str, is_new: bool)
1721
+ - team_name: The normalized team name (existing or newly assigned)
1722
+ - is_new: True if newly assigned, False if existing team recovered
1723
+ """
1724
+ try:
1725
+ # Query the leaderboard
1726
+ if playground is None:
1727
+ # Fallback to random assignment if playground not available
1728
+ print("Playground not available, assigning random team")
1729
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
1730
+ return new_team, True
1731
+
1732
+ # Use centralized helper for authenticated leaderboard fetch
1733
+ leaderboard_df = _get_leaderboard_with_optional_token(playground, token)
1734
+
1735
+ # Check if leaderboard has data and Team column
1736
+ if leaderboard_df is not None and not leaderboard_df.empty and "Team" in leaderboard_df.columns:
1737
+ # Filter for this user's submissions
1738
+ user_submissions = leaderboard_df[leaderboard_df["username"] == username]
1739
+
1740
+ if not user_submissions.empty:
1741
+ # Sort by timestamp (most recent first) if timestamp column exists
1742
+ # Use contextlib.suppress for resilient timestamp parsing
1743
+ if "timestamp" in user_submissions.columns:
1744
+ try:
1745
+ # Attempt to coerce timestamp column to datetime and sort descending
1746
+ user_submissions = user_submissions.copy()
1747
+ user_submissions["timestamp"] = pd.to_datetime(user_submissions["timestamp"], errors='coerce')
1748
+ user_submissions = user_submissions.sort_values("timestamp", ascending=False)
1749
+ print(f"Sorted {len(user_submissions)} submissions by timestamp for {username}")
1750
+ except Exception as ts_error:
1751
+ # If timestamp parsing fails, continue with unsorted DataFrame
1752
+ print(f"Warning: Could not sort by timestamp for {username}: {ts_error}")
1753
+
1754
+ # Get the most recent team assignment (first row after sorting)
1755
+ existing_team = user_submissions.iloc[0]["Team"]
1756
+
1757
+ # Check if team value is valid (not null/empty)
1758
+ if pd.notna(existing_team) and existing_team and str(existing_team).strip():
1759
+ normalized_team = _normalize_team_name(existing_team)
1760
+ print(f"Found existing team for {username}: {normalized_team}")
1761
+ return normalized_team, False
1762
+
1763
+ # No existing team found - assign random
1764
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
1765
+ print(f"Assigning new team to {username}: {new_team}")
1766
+ return new_team, True
1767
+
1768
+ except Exception as e:
1769
+ # On any error, fall back to random assignment
1770
+ print(f"Error checking leaderboard for team: {e}")
1771
+ new_team = _normalize_team_name(random.choice(TEAM_NAMES))
1772
+ print(f"Fallback: assigning random team to {username}: {new_team}")
1773
+ return new_team, True
1774
+
1775
+ def perform_inline_login(username_input, password_input):
1776
+ """
1777
+ Perform inline authentication and return credentials via gr.State updates.
1778
+
1779
+ Concurrency Note: This function NO LONGER stores per-user credentials in
1780
+ os.environ to prevent cross-user data leakage. Authentication state is
1781
+ returned exclusively via gr.State updates (username_state, token_state,
1782
+ team_name_state). Password is never stored server-side.
1783
+
1784
+ Args:
1785
+ username_input: str, the username entered by user
1786
+ password_input: str, the password entered by user
1787
+
1788
+ Returns:
1789
+ dict: Gradio component updates for login UI elements and submit button
1790
+ - On success: hides login form, shows success message, enables submit
1791
+ - On failure: keeps login form visible, shows error with signup link
1792
+ """
1793
+ from aimodelshare.aws import get_aws_token
1794
+
1795
+ # Validate inputs
1796
+ if not username_input or not username_input.strip():
1797
+ error_html = """
1798
+ <div style='background:#fef2f2; padding:12px; border-radius:8px; border-left:4px solid #ef4444; margin-top:12px;'>
1799
+ <p style='margin:0; color:#991b1b; font-weight:500;'>⚠️ Username is required</p>
1800
+ </div>
1801
+ """
1802
+ return {
1803
+ login_username: gr.update(),
1804
+ login_password: gr.update(),
1805
+ login_submit: gr.update(),
1806
+ login_error: gr.update(value=error_html, visible=True),
1807
+ submit_button: gr.update(),
1808
+ submission_feedback_display: gr.update(),
1809
+ team_name_state: gr.update(),
1810
+ username_state: gr.update(),
1811
+ token_state: gr.update()
1812
+ }
1813
+
1814
+ if not password_input or not password_input.strip():
1815
+ error_html = """
1816
+ <div style='background:#fef2f2; padding:12px; border-radius:8px; border-left:4px solid #ef4444; margin-top:12px;'>
1817
+ <p style='margin:0; color:#991b1b; font-weight:500;'>⚠️ Password is required</p>
1818
+ </div>
1819
+ """
1820
+ return {
1821
+ login_username: gr.update(),
1822
+ login_password: gr.update(),
1823
+ login_submit: gr.update(),
1824
+ login_error: gr.update(value=error_html, visible=True),
1825
+ submit_button: gr.update(),
1826
+ submission_feedback_display: gr.update(),
1827
+ team_name_state: gr.update(),
1828
+ username_state: gr.update(),
1829
+ token_state: gr.update()
1830
+ }
1831
+
1832
+ # Concurrency Note: get_aws_token() reads credentials from os.environ, which creates
1833
+ # a race condition in multi-threaded environments. We use _auth_lock to serialize
1834
+ # credential injection, preventing concurrent requests from seeing each other's
1835
+ # credentials. The password is immediately cleared after the auth attempt.
1836
+ #
1837
+ # FUTURE: Ideally get_aws_token() would be refactored to accept credentials as
1838
+ # parameters instead of reading from os.environ. This lock is a workaround.
1839
+ username_clean = username_input.strip()
1840
+
1841
+ # Attempt to get AWS token with serialized credential injection
1842
+ try:
1843
+ with _auth_lock:
1844
+ os.environ["username"] = username_clean
1845
+ os.environ["password"] = password_input.strip() # Only for get_aws_token() call
1846
+ try:
1847
+ token = get_aws_token()
1848
+ finally:
1849
+ # SECURITY: Always clear credentials from environment, even on exception
1850
+ # Also clear stale env vars from previous implementations within the lock
1851
+ # to prevent any race conditions during cleanup
1852
+ os.environ.pop("password", None)
1853
+ os.environ.pop("username", None)
1854
+ os.environ.pop("AWS_TOKEN", None)
1855
+ os.environ.pop("TEAM_NAME", None)
1856
+
1857
+ # Get or assign team for this user with explicit token (already normalized by get_or_assign_team)
1858
+ team_name, is_new_team = get_or_assign_team(username_clean, token=token)
1859
+ # Normalize team name before storing (defensive - already normalized by get_or_assign_team)
1860
+ team_name = _normalize_team_name(team_name)
1861
+
1862
+ # Translate team name for display only (keep team_name_state in English)
1863
+ display_team_name = translate_team_name_for_display(team_name, UI_TEAM_LANG)
1864
+
1865
+ # Build success message based on whether team is new or existing
1866
+ if is_new_team:
1867
+ team_message = f"Has sido asignado a un nuevo equipo: <b>{display_team_name}</b> 🎉"
1868
+ else:
1869
+ team_message = f"¡Bienvenido de vuelta! Permaneces en el equipo: <b>{display_team_name}</b> ✅"
1870
+
1871
+ # Success: hide login form, show success message with team info, enable submit button
1872
+ success_html = f"""
1873
+ <div style='background:#f0fdf4; padding:16px; border-radius:8px; border-left:4px solid #16a34a; margin-top:12px;'>
1874
+ <p style='margin:0; color:#15803d; font-weight:600; font-size:1.1rem;'>✓ ¡Sesión iniciada con éxito!</p>
1875
+ <p style='margin:8px 0 0 0; color:#166534; font-size:0.95rem;'>
1876
+ {team_message}
1877
+ </p>
1878
+ <p style='margin:8px 0 0 0; color:#166534; font-size:0.95rem;'>
1879
+ Haz clic en "Build & Submit Model" nuevamente para publicar tu puntuación.
1880
+ </p>
1881
+ </div>
1882
+ """
1883
+ return {
1884
+ login_username: gr.update(visible=False),
1885
+ login_password: gr.update(visible=False),
1886
+ login_submit: gr.update(visible=False),
1887
+ login_error: gr.update(value=success_html, visible=True),
1888
+ submit_button: gr.update(value="🔬 Build & Submit Model", interactive=True),
1889
+ submission_feedback_display: gr.update(visible=False),
1890
+ team_name_state: gr.update(value=team_name),
1891
+ username_state: gr.update(value=username_clean),
1892
+ token_state: gr.update(value=token)
1893
+ }
1894
+
1895
+ except Exception as e:
1896
+ # Note: Credentials are already cleaned up by the finally block in the try above.
1897
+ # The lock ensures no race condition during cleanup.
1898
+
1899
+ # Authentication failed: show error with signup link
1900
+ error_html = f"""
1901
+ <div style='background:#fef2f2; padding:16px; border-radius:8px; border-left:4px solid #ef4444; margin-top:12px;'>
1902
+ <p style='margin:0; color:#991b1b; font-weight:600; font-size:1.1rem;'>⚠️ Authentication failed</p>
1903
+ <p style='margin:8px 0; color:#7f1d1d; font-size:0.95rem;'>
1904
+ Could not verify your credentials. Please check your username and password.
1905
+ </p>
1906
+ <p style='margin:8px 0 0 0; color:#7f1d1d; font-size:0.95rem;'>
1907
+ <strong>New user?</strong> Create a free account at
1908
+ <a href='https://www.modelshare.ai/login' target='_blank'
1909
+ style='color:#dc2626; text-decoration:underline;'>modelshare.ai/login</a>
1910
+ </p>
1911
+ <details style='margin-top:12px; font-size:0.85rem; color:#7f1d1d;'>
1912
+ <summary style='cursor:pointer;'>Technical details</summary>
1913
+ <pre style='margin-top:8px; padding:8px; background:#fee; border-radius:4px; overflow-x:auto;'>{str(e)}</pre>
1914
+ </details>
1915
+ </div>
1916
+ """
1917
+ return {
1918
+ login_username: gr.update(visible=True),
1919
+ login_password: gr.update(visible=True),
1920
+ login_submit: gr.update(visible=True),
1921
+ login_error: gr.update(value=error_html, visible=True),
1922
+ submit_button: gr.update(),
1923
+ submission_feedback_display: gr.update(),
1924
+ team_name_state: gr.update(),
1925
+ username_state: gr.update(),
1926
+ token_state: gr.update()
1927
+ }
1928
+
1929
+ def run_experiment(
1930
+ model_name_key, # Recieves ENGLISH KEY (e.g., "The Balanced Generalist")
1931
+ complexity_level,
1932
+ feature_set,
1933
+ data_size_str, # Recieves SPANISH LABEL (e.g., "Pequeño (20%)")
1934
+ team_name,
1935
+ last_submission_score,
1936
+ last_rank,
1937
+ submission_count,
1938
+ first_submission_score,
1939
+ best_score,
1940
+ username=None,
1941
+ token=None,
1942
+ readiness_flag=None,
1943
+ was_preview_prev=None,
1944
+ progress=gr.Progress()
1945
+ ):
1946
+ """
1947
+ Core experiment: Uses 'yield' for visual updates and progress bar.
1948
+ Updated to translate Spanish inputs to English keys for Cache/DB lookup.
1949
+ """
1950
+ # --- COLLISION GUARDS ---
1951
+ if isinstance(submit_button, dict) or isinstance(submission_feedback_display, dict) or isinstance(kpi_meta_state, dict) or isinstance(was_preview_state, dict):
1952
+ error_html = "<div style='color:red'>Configuration Error: Parameter shadowing. Refresh page.</div>"
1953
+ yield { submission_feedback_display: gr.update(value=error_html, visible=True) }
1954
+ return
1955
+
1956
+ # --- TRANSLATION LOGIC ---
1957
+ # 1. Translate Data Size to English for DB/Cache Lookup
1958
+ db_data_size = DATA_SIZE_DB_MAP.get(data_size_str, "Small (20%)")
1959
+
1960
+ # Sanitize feature_set
1961
+ sanitized_feature_set = []
1962
+ for feat in (feature_set or []):
1963
+ sanitized_feature_set.append(feat.get("value", str(feat)) if isinstance(feat, dict) else (feat[1] if isinstance(feat, tuple) else str(feat)))
1964
+ feature_set = sanitized_feature_set
1965
+
1966
+ if readiness_flag is not None: ready = readiness_flag
1967
+ else: ready = _is_ready()
1968
+
1969
+ if not username: username = "Unknown_User"
1970
+
1971
+ def get_status_html(step_num, title, subtitle):
1972
+ return f"<div class='processing-status'><span class='processing-icon'>⚙️</span><div class='processing-text'>Step {step_num}/5: {title}</div><div class='processing-subtext'>{subtitle}</div></div>"
1973
+
1974
+ # --- Stage 1: Lock UI ---
1975
+ progress(0.1, desc="Starting Experiment...")
1976
+ yield {
1977
+ submit_button: gr.update(value="⏳ Experiment Running...", interactive=False),
1978
+ submission_feedback_display: gr.update(value=get_status_html(1, "Inicializando", "Preparando tus ingredientes de datos..."), visible=True),
1979
+ login_error: gr.update(visible=False),
1980
+ attempts_tracker_display: gr.update(value=_build_attempts_tracker_html(submission_count))
1981
+ }
1982
+
1983
+ if not model_name_key or model_name_key not in MODEL_TYPES:
1984
+ model_name_key = DEFAULT_MODEL
1985
+ complexity_level = safe_int(complexity_level, 2)
1986
+
1987
+ # Check readiness
1988
+ if playground is None or not ready:
1989
+ settings = compute_rank_settings(submission_count, model_name_key, complexity_level, feature_set, data_size_str)
1990
+ error_msg = "<p style='text-align:center; color:red; padding:20px 0;'>Los datos aún se están inicializando. Por favor, espera un momento y vuelve a intentarlo.</p>"
1991
+ yield { submission_feedback_display: gr.update(value=error_msg, visible=True), submit_button: gr.update(value="🔬 Construir y Enviar Modelo", interactive=True) }
1992
+ return
1993
+
1994
+ try:
1995
+ # --- Stage 2: Smart Build (Cache vs Train) ---
1996
+ progress(0.3, desc="Building Model...")
1997
+
1998
+ # 1. Generate Cache Key (ENGLISH KEYS)
1999
+ sanitized_features = sorted([str(f) for f in feature_set])
2000
+ feature_key = ",".join(sanitized_features)
2001
+ cache_key = f"{model_name_key}|{complexity_level}|{db_data_size}|{feature_key}"
2002
+
2003
+ _log(f"Generated Key: {cache_key}")
2004
+
2005
+ # 2. Check Cache
2006
+ cached_predictions = get_cached_prediction(cache_key)
2007
+
2008
+ predictions = None
2009
+ tuned_model = None
2010
+ preprocessor = None
2011
+
2012
+ if cached_predictions:
2013
+ # === FAST PATH (Zero CPU) ===
2014
+ _log(f"⚡ CACHE HIT: {cache_key}")
2015
+ yield {
2016
+ submission_feedback_display: gr.update(value=get_status_html(2, "Entrenando Modelo", "⚡ La máquina está aprendiendo de la historia..."), visible=True),
2017
+ login_error: gr.update(visible=False)
2018
+ }
2019
+ if isinstance(cached_predictions, str):
2020
+ predictions = [int(c) for c in cached_predictions]
2021
+ else:
2022
+ predictions = cached_predictions
2023
+ tuned_model = None
2024
+ preprocessor = None
2025
+
2026
+ else:
2027
+ # === CACHE MISS (Training Disabled for Safety) ===
2028
+ msg = f"❌ CACHE MISS: {cache_key}"
2029
+ _log(msg)
2030
+
2031
+ error_html = f"""
2032
+ <div style='background:#fee2e2; padding:16px; border-radius:8px; border:2px solid #ef4444; color:#991b1b; text-align:center;'>
2033
+ <h3 style='margin:0;'>⚠️ Configuración no encontrada</h3>
2034
+ <p style='margin:8px 0;'>Esta combinación específica de parámetros no se ha encontrado en nuestra base de datos.</p>
2035
+ <p style='font-size:0.9em;'>Para garantizar la estabilidad del sistema, el entrenamiento en tiempo real está desactivado. Por favor, ajusta la configuración (por ejemplo, cambia el "Tamaño de datos" o la "Estrategia del modelo") y vuelve a intentarlo.</p>
2036
+ </div>
2037
+ """
2038
+
2039
+ yield {
2040
+ submission_feedback_display: gr.update(value=error_html, visible=True),
2041
+ submit_button: gr.update(value="🔬 Construir y Enviar Modelo", interactive=True),
2042
+ login_error: gr.update(visible=False)
2043
+ }
2044
+ return
2045
+
2046
+ # --- Stage 3: Submit (API Call 1) ---
2047
+ # AUTHENTICATION GATE: Check for token before submission
2048
+
2049
+ if token is None:
2050
+ # User not authenticated - compute preview score
2051
+ progress(0.6, desc="Computing Preview Score...")
2052
+
2053
+ # NOTE: Logic updated to handle cached predictions
2054
+ from sklearn.metrics import accuracy_score
2055
+
2056
+ # Ensure format is correct (list vs array)
2057
+ if isinstance(predictions, list):
2058
+ preds_for_metric = np.array(predictions)
2059
+ else:
2060
+ preds_for_metric = predictions
2061
+
2062
+ preview_score = accuracy_score(Y_TEST, preds_for_metric)
2063
+
2064
+ # ... (Rest of preview logic remains the same) ...
2065
+ preview_kpi_meta = {
2066
+ "was_preview": True, "preview_score": preview_score, "ready_at_run_start": ready,
2067
+ "poll_iterations": 0, "local_test_accuracy": preview_score,
2068
+ "this_submission_score": None, "new_best_accuracy": None, "rank": None
2069
+ }
2070
+
2071
+ # 1. Generate the styled preview card
2072
+ preview_card_html = _build_kpi_card_html(
2073
+ new_score=preview_score, last_score=0, new_rank=0, last_rank=0,
2074
+ submission_count=-1, is_preview=True, is_pending=False, local_test_accuracy=None
2075
+ )
2076
+
2077
+ # 2. Inject login text
2078
+ login_prompt_text_html = build_login_prompt_html()
2079
+ closing_div_index = preview_card_html.rfind("</div>")
2080
+ if closing_div_index != -1:
2081
+ combined_html = preview_card_html[:closing_div_index] + login_prompt_text_html + "</div>"
2082
+ else:
2083
+ combined_html = preview_card_html + login_prompt_text_html
2084
+
2085
+ settings = compute_rank_settings(submission_count, model_name_key, complexity_level, feature_set, data_size_str)
2086
+
2087
+ gate_updates = {
2088
+ submission_feedback_display: gr.update(value=combined_html, visible=True),
2089
+ submit_button: gr.update(value="Sign In Required", interactive=False),
2090
+ login_username: gr.update(visible=True), login_password: gr.update(visible=True),
2091
+ login_submit: gr.update(visible=True), login_error: gr.update(value="", visible=False),
2092
+ team_leaderboard_display: _build_skeleton_leaderboard(rows=6, is_team=True),
2093
+ individual_leaderboard_display: _build_skeleton_leaderboard(rows=6, is_team=False),
2094
+ last_submission_score_state: last_submission_score, last_rank_state: last_rank,
2095
+ best_score_state: best_score, submission_count_state: submission_count,
2096
+ first_submission_score_state: first_submission_score,
2097
+ rank_message_display: settings["rank_message"],
2098
+ model_type_radio: gr.update(choices=settings["model_choices"], value=settings["model_value"], interactive=settings["model_interactive"]),
2099
+ complexity_slider: gr.update(minimum=1, maximum=settings["complexity_max"], value=settings["complexity_value"]),
2100
+ feature_set_checkbox: gr.update(choices=settings["feature_set_choices"], value=settings["feature_set_value"], interactive=settings["feature_set_interactive"]),
2101
+ data_size_radio: gr.update(choices=settings["data_size_choices"], value=settings["data_size_value"], interactive=settings["data_size_interactive"]),
2102
+ attempts_tracker_display: gr.update(value=_build_attempts_tracker_html(submission_count)),
2103
+ was_preview_state: True, kpi_meta_state: preview_kpi_meta, last_seen_ts_state: None
2104
+ }
2105
+ yield gate_updates
2106
+ return # Stop here
2107
+
2108
+ # --- ATTEMPT LIMIT CHECK ---
2109
+ if submission_count >= ATTEMPT_LIMIT:
2110
+ limit_warning_html = f"""
2111
+ <div class='kpi-card' style='border-color: #ef4444;'>
2112
+ <h2 style='color: #111827; margin-top:0;'>🛑 Submission Limit Reached</h2>
2113
+ <div class='kpi-card-body'>
2114
+ <div class='kpi-metric-box'>
2115
+ <p class='kpi-label'>Attempts Used</p>
2116
+ <p class='kpi-score' style='color: #ef4444;'>{ATTEMPT_LIMIT} / {ATTEMPT_LIMIT}</p>
2117
+ </div>
2118
+ </div>
2119
+ <div style='margin-top: 16px; background:#fef2f2; padding:16px; border-radius:12px; text-align:left; font-size:0.98rem; line-height:1.4;'>
2120
+ <p style='margin:0; color:#991b1b;'><b>Nice Work!</b> Scroll down to "Finish and Reflect".</p>
2121
+ </div>
2122
+ </div>"""
2123
+ settings = compute_rank_settings(submission_count, model_name_key, complexity_level, feature_set, data_size_str)
2124
+ limit_reached_updates = {
2125
+ submission_feedback_display: gr.update(value=limit_warning_html, visible=True),
2126
+ submit_button: gr.update(value="🛑 Submission Limit Reached", interactive=False),
2127
+ model_type_radio: gr.update(interactive=False), complexity_slider: gr.update(interactive=False),
2128
+ feature_set_checkbox: gr.update(interactive=False), data_size_radio: gr.update(interactive=False),
2129
+ 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>"),
2130
+ team_leaderboard_display: team_leaderboard_display, individual_leaderboard_display: individual_leaderboard_display,
2131
+ last_submission_score_state: last_submission_score, last_rank_state: last_rank,
2132
+ best_score_state: best_score, submission_count_state: submission_count,
2133
+ first_submission_score_state: first_submission_score, rank_message_display: settings["rank_message"],
2134
+ login_username: gr.update(visible=False), login_password: gr.update(visible=False),
2135
+ login_submit: gr.update(visible=False), login_error: gr.update(visible=False),
2136
+ was_preview_state: False, kpi_meta_state: {}, last_seen_ts_state: None
2137
+ }
2138
+ yield limit_reached_updates
2139
+ return
2140
+
2141
+ progress(0.5, desc="Submitting to Cloud...")
2142
+ yield {
2143
+ submission_feedback_display: gr.update(value=get_status_html(3, "Submitting", "Sending model to the competition server..."), visible=True),
2144
+ login_error: gr.update(visible=False)
2145
+ }
2146
+
2147
+
2148
+ description = f"{model_name_key} (Cplx:{complexity_level} Size:{data_size_str})"
2149
+ tags = f"team:{team_name},model:{model_name_key}"
2150
+
2151
+ # 1. FETCH BASELINE
2152
+ baseline_leaderboard_df = _get_leaderboard_with_optional_token(playground, token)
2153
+
2154
+ from sklearn.metrics import accuracy_score
2155
+ local_test_accuracy = accuracy_score(Y_TEST, predictions)
2156
+
2157
+ # 2. SUBMIT & CAPTURE ACCURACY
2158
+ def _submit():
2159
+ return playground.submit_model(
2160
+ model=tuned_model, # This can now be None!
2161
+ preprocessor=preprocessor, # This can now be None!
2162
+ prediction_submission=predictions, # We explicitly send predictions
2163
+ input_dict={'description': description, 'tags': tags},
2164
+ custom_metadata={'Team': team_name, 'Moral_Compass': 0},
2165
+ token=token,
2166
+ return_metrics=["accuracy"]
2167
+ )
2168
+
2169
+ try:
2170
+ submit_result = _retry_with_backoff(_submit, description="model submission")
2171
+ if isinstance(submit_result, tuple) and len(submit_result) == 3:
2172
+ _, _, metrics = submit_result
2173
+ if metrics and "accuracy" in metrics and metrics["accuracy"] is not None:
2174
+ this_submission_score = float(metrics["accuracy"])
2175
+ else:
2176
+ this_submission_score = local_test_accuracy
2177
+ else:
2178
+ this_submission_score = local_test_accuracy
2179
+ except Exception as e:
2180
+ _log(f"Submission return parsing failed: {e}. Using local accuracy.")
2181
+ this_submission_score = local_test_accuracy
2182
+
2183
+ _log(f"Submission successful. Server Score: {this_submission_score}")
2184
+
2185
+ try:
2186
+ # Short timeout to trigger the lambda without hanging the UI
2187
+ _log("Triggering backend merge...")
2188
+ playground.get_leaderboard(token=token)
2189
+ except Exception:
2190
+ # We ignore errors here because the 'submit_model' post
2191
+ # already succeeded. This is just a cleanup task.
2192
+ pass
2193
+ # -------------------------------------------------------------------------
2194
+
2195
+ # Immediately increment submission count...
2196
+ new_submission_count = submission_count + 1
2197
+ new_first_submission_score = first_submission_score
2198
+ if submission_count == 0 and first_submission_score is None:
2199
+ new_first_submission_score = this_submission_score
2200
+
2201
+ # --- Stage 4: Local Rank Calculation (Optimistic) ---
2202
+ progress(0.9, desc="Calculating Rank...")
2203
+
2204
+ # 3. SIMULATE UPDATED LEADERBOARD
2205
+ simulated_df = baseline_leaderboard_df.copy() if baseline_leaderboard_df is not None else pd.DataFrame()
2206
+
2207
+ # We use pd.Timestamp.now() to ensure pandas sorting logic sees this as the absolute latest
2208
+ new_row = pd.DataFrame([{
2209
+ "username": username,
2210
+ "accuracy": this_submission_score,
2211
+ "Team": team_name,
2212
+ "timestamp": pd.Timestamp.now(),
2213
+ "version": "latest"
2214
+ }])
2215
+
2216
+ if not simulated_df.empty:
2217
+ simulated_df = pd.concat([simulated_df, new_row], ignore_index=True)
2218
+ else:
2219
+ simulated_df = new_row
2220
+
2221
+ # 4. GENERATE TABLES (Use helper for tables only)
2222
+ # We ignore the kpi_card return from this function because it might use internal sorting
2223
+ # that doesn't respect our new row perfectly.
2224
+ team_html, individual_html, _, new_best_accuracy, new_rank, _ = generate_competitive_summary(
2225
+ simulated_df, team_name, username, last_submission_score, last_rank, submission_count
2226
+ )
2227
+
2228
+ # 5. GENERATE KPI CARD EXPLICITLY (The Authority Fix)
2229
+ # We manually build the card using the score we KNOW we just got.
2230
+ kpi_card_html = _build_kpi_card_html(
2231
+ new_score=this_submission_score,
2232
+ last_score=last_submission_score,
2233
+ new_rank=new_rank,
2234
+ last_rank=last_rank,
2235
+ submission_count=submission_count,
2236
+ is_preview=False,
2237
+ is_pending=False
2238
+ )
2239
+
2240
+ # ... (Previous Stage 1-4 logic remains unchanged) ...
2241
+
2242
+ # --- Stage 5: Final UI Update ---
2243
+ progress(1.0, desc="Complete!")
2244
+
2245
+ success_kpi_meta = {
2246
+ "was_preview": False, "preview_score": None, "ready_at_run_start": ready,
2247
+ "poll_iterations": 0, "local_test_accuracy": local_test_accuracy,
2248
+ "this_submission_score": this_submission_score, "new_best_accuracy": new_best_accuracy,
2249
+ "rank": new_rank, "pending": False, "optimistic_fallback": True
2250
+ }
2251
+
2252
+ settings = compute_rank_settings(new_submission_count, model_name_key, complexity_level, feature_set, data_size_str)
2253
+
2254
+ # -------------------------------------------------------------------------
2255
+ # NEW LOGIC: Check for Limit Reached immediately AFTER this submission
2256
+ # -------------------------------------------------------------------------
2257
+ limit_reached = new_submission_count >= ATTEMPT_LIMIT
2258
+
2259
+ # Prepare the UI state based on whether limit is reached
2260
+ if limit_reached:
2261
+ # 1. Append the Limit Warning HTML *below* the Result Card
2262
+ limit_html = f"""
2263
+ <div style='margin-top: 16px; border: 2px solid #ef4444; background:#fef2f2; padding:16px; border-radius:12px; text-align:left;'>
2264
+ <h3 style='margin:0 0 8px 0; color:#991b1b;'>🛑 Submission Limit Reached ({ATTEMPT_LIMIT}/{ATTEMPT_LIMIT})</h3>
2265
+ <p style='margin:0; color:#7f1d1d; line-height:1.4;'>
2266
+ <b>You have used all your attempts for this session.</b><br>
2267
+ Review your final results above, then scroll down to "Finish and Reflect" to continue.
2268
+ </p>
2269
+ </div>
2270
+ """
2271
+ final_html_display = kpi_card_html + limit_html
2272
+
2273
+ # 2. Disable all controls
2274
+ button_update = gr.update(value="🛑 Limit Reached", interactive=False)
2275
+ interactive_state = False
2276
+ 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>"
2277
+
2278
+ else:
2279
+ # Normal State: Show just the result card and keep controls active
2280
+ final_html_display = kpi_card_html
2281
+ button_update = gr.update(value="🔬 Build & Submit Model", interactive=True)
2282
+ interactive_state = True
2283
+ tracker_html = _build_attempts_tracker_html(new_submission_count)
2284
+
2285
+ # -------------------------------------------------------------------------
2286
+
2287
+ final_updates = {
2288
+ submission_feedback_display: gr.update(value=final_html_display, visible=True),
2289
+ team_leaderboard_display: team_html,
2290
+ individual_leaderboard_display: individual_html,
2291
+ last_submission_score_state: this_submission_score,
2292
+ last_rank_state: new_rank,
2293
+ best_score_state: new_best_accuracy,
2294
+ submission_count_state: new_submission_count,
2295
+ first_submission_score_state: new_first_submission_score,
2296
+ rank_message_display: settings["rank_message"],
2297
+
2298
+ # Apply the interactive state calculated above
2299
+ model_type_radio: gr.update(choices=settings["model_choices"], value=settings["model_value"], interactive=(settings["model_interactive"] and interactive_state)),
2300
+ complexity_slider: gr.update(minimum=1, maximum=settings["complexity_max"], value=settings["complexity_value"], interactive=interactive_state),
2301
+ feature_set_checkbox: gr.update(choices=settings["feature_set_choices"], value=settings["feature_set_value"], interactive=(settings["feature_set_interactive"] and interactive_state)),
2302
+ data_size_radio: gr.update(choices=settings["data_size_choices"], value=settings["data_size_value"], interactive=(settings["data_size_interactive"] and interactive_state)),
2303
+
2304
+ submit_button: button_update,
2305
+
2306
+ login_username: gr.update(visible=False), login_password: gr.update(visible=False),
2307
+ login_submit: gr.update(visible=False), login_error: gr.update(visible=False),
2308
+ attempts_tracker_display: gr.update(value=tracker_html),
2309
+ was_preview_state: False,
2310
+ kpi_meta_state: success_kpi_meta,
2311
+ last_seen_ts_state: time.time()
2312
+ }
2313
+ yield final_updates
2314
+
2315
+ except Exception as e:
2316
+ error_msg = f"ERROR: {e}"
2317
+ _log(f"Exception in run_experiment: {error_msg}")
2318
+ settings = compute_rank_settings(
2319
+ submission_count, model_name_key, complexity_level, feature_set, data_size_str
2320
+ )
2321
+
2322
+ exception_kpi_meta = {
2323
+ "was_preview": False,
2324
+ "preview_score": None,
2325
+ "ready_at_run_start": ready if 'ready' in locals() else False,
2326
+ "poll_iterations": 0,
2327
+ "local_test_accuracy": None,
2328
+ "this_submission_score": None,
2329
+ "new_best_accuracy": None,
2330
+ "rank": None,
2331
+ "error": str(e)
2332
+ }
2333
+
2334
+ error_updates = {
2335
+ submission_feedback_display: gr.update(
2336
+ f"<p style='text-align:center; color:red; padding:20px 0;'>An error occurred: {error_msg}</p>", visible=True
2337
+ ),
2338
+ team_leaderboard_display: f"<p style='text-align:center; color:red; padding-top:20px;'>An error occurred: {error_msg}</p>",
2339
+ individual_leaderboard_display: f"<p style='text-align:center; color:red; padding-top:20px;'>An error occurred: {error_msg}</p>",
2340
+ last_submission_score_state: last_submission_score,
2341
+ last_rank_state: last_rank,
2342
+ best_score_state: best_score,
2343
+ submission_count_state: submission_count,
2344
+ first_submission_score_state: first_submission_score,
2345
+ rank_message_display: settings["rank_message"],
2346
+ model_type_radio: gr.update(choices=settings["model_choices"], value=settings["model_value"], interactive=settings["model_interactive"]),
2347
+ complexity_slider: gr.update(minimum=1, maximum=settings["complexity_max"], value=settings["complexity_value"]),
2348
+ feature_set_checkbox: gr.update(choices=settings["feature_set_choices"], value=settings["feature_set_value"], interactive=settings["feature_set_interactive"]),
2349
+ data_size_radio: gr.update(choices=settings["data_size_choices"], value=settings["data_size_value"], interactive=settings["data_size_interactive"]),
2350
+ submit_button: gr.update(value="🔬 Build & Submit Model", interactive=True),
2351
+ login_username: gr.update(visible=False),
2352
+ login_password: gr.update(visible=False),
2353
+ login_submit: gr.update(visible=False),
2354
+ login_error: gr.update(visible=False),
2355
+ attempts_tracker_display: gr.update(value=_build_attempts_tracker_html(submission_count)),
2356
+ was_preview_state: False,
2357
+ kpi_meta_state: exception_kpi_meta,
2358
+ last_seen_ts_state: None
2359
+ }
2360
+ yield error_updates
2361
+
2362
+
2363
+ def on_initial_load(username, token=None, team_name=""):
2364
+ """
2365
+ Updated to show "Welcome & CTA" if the SPECIFIC USER has 0 submissions,
2366
+ even if the leaderboard/team already has data from others.
2367
+ """
2368
+ initial_ui = compute_rank_settings(
2369
+ 0, DEFAULT_MODEL, 2, DEFAULT_FEATURE_SET, DEFAULT_DATA_SIZE
2370
+ )
2371
+
2372
+ # 1. Prepare the Welcome HTML
2373
+ # Translate team name to Spanish for display only (keep team_name in English for logic)
2374
+ display_team = translate_team_name_for_display(team_name, UI_TEAM_LANG) if team_name else "Tu Equipo"
2375
+
2376
+ welcome_html = f"""
2377
+ <div style='text-align:center; padding: 30px 20px;'>
2378
+ <div style='font-size: 3rem; margin-bottom: 10px;'>👋</div>
2379
+ <h3 style='margin: 0 0 8px 0; color: #111827; font-size: 1.5rem;'>¡Bienvenido a <b>{display_team}</b>!</h3>
2380
+ <p style='font-size: 1.1rem; color: #4b5563; margin: 0 0 20px 0;'>
2381
+ Tu equipo está esperando tu ayuda para mejorar la IA.
2382
+ </p>
2383
+
2384
+ <div style='background:#eff6ff; padding:16px; border-radius:12px; border:2px solid #bfdbfe; display:inline-block;'>
2385
+ <p style='margin:0; color:#1e40af; font-weight:bold; font-size:1.1rem;'>
2386
+ 👈 ¡Haz clic en "Build & Submit Model" para comenzar a jugar!
2387
+ </p>
2388
+ </div>
2389
+ </div>
2390
+ """
2391
+
2392
+ # Check background init
2393
+ with INIT_LOCK:
2394
+ background_ready = INIT_FLAGS["leaderboard"]
2395
+
2396
+ should_attempt_fetch = background_ready or (token is not None)
2397
+ full_leaderboard_df = None
2398
+
2399
+ if should_attempt_fetch:
2400
+ try:
2401
+ if playground:
2402
+ full_leaderboard_df = _get_leaderboard_with_optional_token(playground, token)
2403
+ except Exception as e:
2404
+ print(f"Error on initial load fetch: {e}")
2405
+ full_leaderboard_df = None
2406
+
2407
+ # -------------------------------------------------------------------------
2408
+ # LOGIC UPDATE: Check if THIS user has submitted anything
2409
+ # -------------------------------------------------------------------------
2410
+ user_has_submitted = False
2411
+ if full_leaderboard_df is not None and not full_leaderboard_df.empty:
2412
+ if "username" in full_leaderboard_df.columns and username:
2413
+ # Check if the username exists in the dataframe
2414
+ user_has_submitted = username in full_leaderboard_df["username"].values
2415
+
2416
+ # Decision Logic
2417
+ if not user_has_submitted:
2418
+ # CASE 1: New User (or first time loading session) -> FORCE WELCOME
2419
+ # regardless of whether the leaderboard has other people's data.
2420
+ team_html = welcome_html
2421
+ individual_html = "<p style='text-align:center; color:#6b7280; padding-top:40px;'>Submit your model to see where you rank!</p>"
2422
+
2423
+ elif full_leaderboard_df is None or full_leaderboard_df.empty:
2424
+ # CASE 2: Returning user, but data fetch failed -> Show Skeleton
2425
+ team_html = _build_skeleton_leaderboard(rows=6, is_team=True)
2426
+ individual_html = _build_skeleton_leaderboard(rows=6, is_team=False)
2427
+
2428
+ else:
2429
+ # CASE 3: Returning user WITH data -> Show Real Tables
2430
+ try:
2431
+ team_html, individual_html, _, _, _, _ = generate_competitive_summary(
2432
+ full_leaderboard_df,
2433
+ team_name,
2434
+ username,
2435
+ 0, 0, -1
2436
+ )
2437
+ except Exception as e:
2438
+ print(f"Error generating summary HTML: {e}")
2439
+ team_html = "<p style='text-align:center; color:red; padding-top:20px;'>Error rendering leaderboard.</p>"
2440
+ individual_html = "<p style='text-align:center; color:red; padding-top:20px;'>Error rendering leaderboard.</p>"
2441
+
2442
+ return (
2443
+ get_model_card(DEFAULT_MODEL),
2444
+ team_html,
2445
+ individual_html,
2446
+ initial_ui["rank_message"],
2447
+ gr.update(choices=initial_ui["model_choices"], value=initial_ui["model_value"], interactive=initial_ui["model_interactive"]),
2448
+ gr.update(minimum=1, maximum=initial_ui["complexity_max"], value=initial_ui["complexity_value"]),
2449
+ gr.update(choices=initial_ui["feature_set_choices"], value=initial_ui["feature_set_value"], interactive=initial_ui["feature_set_interactive"]),
2450
+ gr.update(choices=initial_ui["data_size_choices"], value=initial_ui["data_size_value"], interactive=initial_ui["data_size_interactive"]),
2451
+ )
2452
+
2453
+
2454
+ # -------------------------------------------------------------------------
2455
+ # Conclusion helpers (dark/light mode aware)
2456
+ # -------------------------------------------------------------------------
2457
+ def build_final_conclusion_html(best_score, submissions, rank, first_score, feature_set):
2458
+ """
2459
+ Build the final conclusion HTML with performance summary.
2460
+ Colors are handled via CSS classes so that light/dark mode work correctly.
2461
+ """
2462
+ unlocked_tiers = min(3, max(0, submissions - 1)) # 0..3
2463
+ tier_names = ["Trainee", "Junior", "Senior", "Lead"]
2464
+ reached = tier_names[: unlocked_tiers + 1]
2465
+ tier_line = " → ".join([f"{t}{' ✅' if t in reached else ''}" for t in tier_names])
2466
+
2467
+ improvement = (best_score - first_score) if (first_score is not None and submissions > 1) else 0.0
2468
+ strong_predictors = {"age", "length_of_stay", "priors_count", "age_cat"}
2469
+ strong_used = [f for f in feature_set if f in strong_predictors]
2470
+
2471
+ ethical_note = (
2472
+ "You unlocked powerful predictors. Consider: Would removing demographic fields change fairness? "
2473
+ "In the next section we will begin to investigate this question further."
2474
+ )
2475
+
2476
+ # Tailor message for very few submissions
2477
+ tip_html = ""
2478
+ if submissions < 2:
2479
+ tip_html = """
2480
+ <div class="final-conclusion-tip">
2481
+ <b>Tip:</b> Try at least 2–3 submissions changing ONE setting at a time to see clear cause/effect.
2482
+ </div>
2483
+ """
2484
+
2485
+ # Add note if user reached the attempt cap
2486
+ attempt_cap_html = ""
2487
+ if submissions >= ATTEMPT_LIMIT:
2488
+ attempt_cap_html = f"""
2489
+ <div class="final-conclusion-attempt-cap">
2490
+ <p style="margin:0;">
2491
+ <b>📊 Attempt Limit Reached:</b> You used all {ATTEMPT_LIMIT} allowed submission attempts for this session.
2492
+ We will open up submissions again after you complete some new activities next.
2493
+ </p>
2494
+ </div>
2495
+ """
2496
+
2497
+ return f"""
2498
+ <div class="final-conclusion-root">
2499
+ <h1 class="final-conclusion-title">🎉 Engineering Phase Complete</h1>
2500
+ <div class="final-conclusion-card">
2501
+ <h2 class="final-conclusion-subtitle">Your Performance Snapshot</h2>
2502
+ <ul class="final-conclusion-list">
2503
+ <li>🏁 <b>Best Accuracy:</b> {(best_score * 100):.2f}%</li>
2504
+ <li>📊 <b>Rank Achieved:</b> {('#' + str(rank)) if rank > 0 else '—'}</li>
2505
+ <li>🔁 <b>Submissions Made This Session:</b> {submissions}{' / ' + str(ATTEMPT_LIMIT) if submissions >= ATTEMPT_LIMIT else ''}</li>
2506
+ <li>🧗 <b>Improvement Over First Score This Session:</b> {(improvement * 100):+.2f}</li>
2507
+ <li>🎖️ <b>Tier Progress:</b> {tier_line}</li>
2508
+ <li>🧪 <b>Strong Predictors Used:</b> {len(strong_used)} ({', '.join(strong_used) if strong_used else 'None yet'})</li>
2509
+ </ul>
2510
+
2511
+ {tip_html}
2512
+
2513
+ <div class="final-conclusion-ethics">
2514
+ <p style="margin:0;"><b>Ethical Reflection:</b> {ethical_note}</p>
2515
+ </div>
2516
+
2517
+ {attempt_cap_html}
2518
+
2519
+ <hr class="final-conclusion-divider" />
2520
+
2521
+ <div class="final-conclusion-next">
2522
+ <h2>➡️ Next: Real-World Consequences</h2>
2523
+ <p>Scroll below this app to continue. You'll examine how models like yours shape judicial outcomes.</p>
2524
+ <h1 class="final-conclusion-scroll">👇 SCROLL DOWN 👇</h1>
2525
+ </div>
2526
+ </div>
2527
+ </div>
2528
+ """
2529
+
2530
+
2531
+
2532
+ def build_conclusion_from_state(best_score, submissions, rank, first_score, feature_set):
2533
+ return build_final_conclusion_html(best_score, submissions, rank, first_score, feature_set)
2534
+ def create_model_building_game_es_app(theme_primary_hue: str = "indigo") -> "gr.Blocks":
2535
+ """
2536
+ Create (but do not launch) the model building game app.
2537
+ """
2538
+ start_background_init()
2539
+
2540
+ # Add missing globals (FIX)
2541
+ global submit_button, submission_feedback_display, team_leaderboard_display
2542
+ global individual_leaderboard_display, last_submission_score_state, last_rank_state
2543
+ global best_score_state, submission_count_state, first_submission_score_state
2544
+ global rank_message_display, model_type_radio, complexity_slider
2545
+ global feature_set_checkbox, data_size_radio
2546
+ global login_username, login_password, login_submit, login_error
2547
+ global attempts_tracker_display, team_name_state
2548
+ global username_state, token_state # <-- Added
2549
+ global readiness_state, was_preview_state, kpi_meta_state # <-- Added for parameter shadowing guards
2550
+ global last_seen_ts_state # <-- Added for timestamp tracking
2551
+
2552
+ css = """
2553
+ /* ------------------------------
2554
+ Shared Design Tokens (local)
2555
+ ------------------------------ */
2556
+
2557
+ /* We keep everything driven by Gradio theme vars:
2558
+ --body-background-fill, --body-text-color, --secondary-text-color,
2559
+ --border-color-primary, --block-background-fill, --color-accent,
2560
+ --shadow-drop, --prose-background-fill
2561
+ */
2562
+
2563
+ :root {
2564
+ --slide-radius-md: 12px;
2565
+ --slide-radius-lg: 16px;
2566
+ --slide-radius-xl: 18px;
2567
+ --slide-spacing-lg: 24px;
2568
+
2569
+ /* Local, non-brand tokens built *on top of* theme vars */
2570
+ --card-bg-soft: var(--block-background-fill);
2571
+ --card-bg-strong: var(--prose-background-fill, var(--block-background-fill));
2572
+ --card-border-subtle: var(--border-color-primary);
2573
+ --accent-strong: var(--color-accent);
2574
+ --text-main: var(--body-text-color);
2575
+ --text-muted: var(--secondary-text-color);
2576
+ }
2577
+
2578
+ /* ------------------------------------------------------------------
2579
+ Base Layout Helpers
2580
+ ------------------------------------------------------------------ */
2581
+
2582
+ .slide-content {
2583
+ max-width: 900px;
2584
+ margin-left: auto;
2585
+ margin-right: auto;
2586
+ }
2587
+
2588
+ /* Shared card-like panels used throughout slides */
2589
+ .panel-box {
2590
+ background: var(--card-bg-soft);
2591
+ padding: 20px;
2592
+ border-radius: var(--slide-radius-lg);
2593
+ border: 2px solid var(--card-border-subtle);
2594
+ margin-bottom: 18px;
2595
+ color: var(--text-main);
2596
+ box-shadow: var(--shadow-drop, 0 2px 4px rgba(0,0,0,0.04));
2597
+ }
2598
+
2599
+ .leaderboard-box {
2600
+ background: var(--card-bg-soft);
2601
+ padding: 20px;
2602
+ border-radius: var(--slide-radius-lg);
2603
+ border: 1px solid var(--card-border-subtle);
2604
+ margin-top: 12px;
2605
+ color: var(--text-main);
2606
+ }
2607
+
2608
+ /* For “explanatory UI” scaffolding */
2609
+ .mock-ui-box {
2610
+ background: var(--card-bg-strong);
2611
+ border: 2px solid var(--card-border-subtle);
2612
+ padding: 24px;
2613
+ border-radius: var(--slide-radius-lg);
2614
+ color: var(--text-main);
2615
+ }
2616
+
2617
+ .mock-ui-inner {
2618
+ background: var(--block-background-fill);
2619
+ border: 1px solid var(--card-border-subtle);
2620
+ padding: 24px;
2621
+ border-radius: var(--slide-radius-md);
2622
+ }
2623
+
2624
+ /* “Control box” inside the mock UI */
2625
+ .mock-ui-control-box {
2626
+ padding: 12px;
2627
+ background: var(--block-background-fill);
2628
+ border-radius: 8px;
2629
+ border: 1px solid var(--card-border-subtle);
2630
+ }
2631
+
2632
+ /* Little radio / check icons */
2633
+ .mock-ui-radio-on {
2634
+ font-size: 1.5rem;
2635
+ vertical-align: middle;
2636
+ color: var(--accent-strong);
2637
+ }
2638
+
2639
+ .mock-ui-radio-off {
2640
+ font-size: 1.5rem;
2641
+ vertical-align: middle;
2642
+ color: var(--text-muted);
2643
+ }
2644
+
2645
+ .mock-ui-slider-text {
2646
+ font-size: 1.5rem;
2647
+ margin: 0;
2648
+ color: var(--accent-strong);
2649
+ letter-spacing: 4px;
2650
+ }
2651
+
2652
+ .mock-ui-slider-bar {
2653
+ color: var(--text-muted);
2654
+ }
2655
+
2656
+ /* Simple mock button representation */
2657
+ .mock-button {
2658
+ width: 100%;
2659
+ font-size: 1.25rem;
2660
+ font-weight: 600;
2661
+ padding: 16px 24px;
2662
+ background-color: var(--accent-strong);
2663
+ color: var(--body-background-fill);
2664
+ border: none;
2665
+ border-radius: 8px;
2666
+ cursor: not-allowed;
2667
+ }
2668
+
2669
+ /* Step visuals on slides */
2670
+ .step-visual {
2671
+ display: flex;
2672
+ flex-wrap: wrap;
2673
+ justify-content: space-around;
2674
+ align-items: center;
2675
+ margin: 24px 0;
2676
+ text-align: center;
2677
+ font-size: 1rem;
2678
+ }
2679
+
2680
+ .step-visual-box {
2681
+ padding: 16px;
2682
+ background: var(--block-background-fill); /* ✅ theme-aware */
2683
+ border-radius: 8px;
2684
+ border: 2px solid var(--border-color-primary);
2685
+ margin: 5px;
2686
+ color: var(--body-text-color); /* optional, safe */
2687
+ }
2688
+
2689
+ .step-visual-arrow {
2690
+ font-size: 2rem;
2691
+ margin: 5px;
2692
+ /* no explicit color – inherit from theme or override in dark mode */
2693
+ }
2694
+
2695
+ /* ------------------------------------------------------------------
2696
+ KPI Card (score feedback)
2697
+ ------------------------------------------------------------------ */
2698
+
2699
+ .kpi-card {
2700
+ background: var(--card-bg-strong);
2701
+ border: 2px solid var(--accent-strong);
2702
+ padding: 24px;
2703
+ border-radius: var(--slide-radius-lg);
2704
+ text-align: center;
2705
+ max-width: 600px;
2706
+ margin: auto;
2707
+ color: var(--text-main);
2708
+ box-shadow: var(--shadow-drop, 0 4px 6px -1px rgba(0,0,0,0.08));
2709
+ min-height: 200px; /* prevent layout shift */
2710
+ }
2711
+
2712
+ .kpi-card-body {
2713
+ display: flex;
2714
+ flex-wrap: wrap;
2715
+ justify-content: space-around;
2716
+ align-items: flex-end;
2717
+ margin-top: 24px;
2718
+ }
2719
+
2720
+ .kpi-metric-box {
2721
+ min-width: 150px;
2722
+ margin: 10px;
2723
+ }
2724
+
2725
+ .kpi-label {
2726
+ font-size: 1rem;
2727
+ color: var(--text-muted);
2728
+ margin: 0;
2729
+ }
2730
+
2731
+ .kpi-score {
2732
+ font-size: 3rem;
2733
+ font-weight: 700;
2734
+ margin: 0;
2735
+ line-height: 1.1;
2736
+ color: var(--accent-strong);
2737
+ }
2738
+
2739
+ .kpi-subtext-muted {
2740
+ font-size: 1.2rem;
2741
+ font-weight: 500;
2742
+ color: var(--text-muted);
2743
+ margin: 0;
2744
+ padding-top: 8px;
2745
+ }
2746
+
2747
+ /* Small variants to hint semantic state without hard-coded colors */
2748
+ .kpi-card--neutral {
2749
+ border-color: var(--card-border-subtle);
2750
+ }
2751
+
2752
+ .kpi-card--subtle-accent {
2753
+ border-color: var(--accent-strong);
2754
+ }
2755
+
2756
+ .kpi-score--muted {
2757
+ color: var(--text-muted);
2758
+ }
2759
+
2760
+ /* ------------------------------------------------------------------
2761
+ Leaderboard Table + Placeholder
2762
+ ------------------------------------------------------------------ */
2763
+
2764
+ .leaderboard-html-table {
2765
+ width: 100%;
2766
+ border-collapse: collapse;
2767
+ text-align: left;
2768
+ font-size: 1rem;
2769
+ color: var(--text-main);
2770
+ min-height: 300px; /* Stable height */
2771
+ }
2772
+
2773
+ .leaderboard-html-table thead {
2774
+ background: var(--block-background-fill);
2775
+ }
2776
+
2777
+ .leaderboard-html-table th {
2778
+ padding: 12px 16px;
2779
+ font-size: 0.9rem;
2780
+ color: var(--text-muted);
2781
+ font-weight: 500;
2782
+ }
2783
+
2784
+ .leaderboard-html-table tbody tr {
2785
+ border-bottom: 1px solid var(--card-border-subtle);
2786
+ }
2787
+
2788
+ .leaderboard-html-table td {
2789
+ padding: 12px 16px;
2790
+ }
2791
+
2792
+ .leaderboard-html-table .user-row-highlight {
2793
+ background: rgba( var(--color-accent-rgb, 59,130,246), 0.1 );
2794
+ font-weight: 600;
2795
+ color: var(--accent-strong);
2796
+ }
2797
+
2798
+ /* Static placeholder (no shimmer, no animation) */
2799
+ .lb-placeholder {
2800
+ min-height: 300px;
2801
+ display: flex;
2802
+ flex-direction: column;
2803
+ align-items: center;
2804
+ justify-content: center;
2805
+ background: var(--block-background-fill);
2806
+ border: 1px solid var(--card-border-subtle);
2807
+ border-radius: 12px;
2808
+ padding: 40px 20px;
2809
+ text-align: center;
2810
+ }
2811
+
2812
+ .lb-placeholder-title {
2813
+ font-size: 1.25rem;
2814
+ font-weight: 500;
2815
+ color: var(--text-muted);
2816
+ margin-bottom: 8px;
2817
+ }
2818
+
2819
+ .lb-placeholder-sub {
2820
+ font-size: 1rem;
2821
+ color: var(--text-muted);
2822
+ }
2823
+
2824
+ /* ------------------------------------------------------------------
2825
+ Processing / “Experiment running” status
2826
+ ------------------------------------------------------------------ */
2827
+
2828
+ .processing-status {
2829
+ background: var(--block-background-fill);
2830
+ border: 2px solid var(--accent-strong);
2831
+ border-radius: 16px;
2832
+ padding: 30px;
2833
+ text-align: center;
2834
+ box-shadow: var(--shadow-drop, 0 4px 6px rgba(0,0,0,0.12));
2835
+ animation: pulse-indigo 2s infinite;
2836
+ color: var(--text-main);
2837
+ }
2838
+
2839
+ .processing-icon {
2840
+ font-size: 4rem;
2841
+ margin-bottom: 10px;
2842
+ display: block;
2843
+ animation: spin-slow 3s linear infinite;
2844
+ }
2845
+
2846
+ .processing-text {
2847
+ font-size: 1.5rem;
2848
+ font-weight: 700;
2849
+ color: var(--accent-strong);
2850
+ }
2851
+
2852
+ .processing-subtext {
2853
+ font-size: 1.1rem;
2854
+ color: var(--text-muted);
2855
+ margin-top: 8px;
2856
+ }
2857
+
2858
+ /* Pulse & spin animations */
2859
+ @keyframes pulse-indigo {
2860
+ 0% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.4); }
2861
+ 70% { box-shadow: 0 0 0 15px rgba(99, 102, 241, 0); }
2862
+ 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0); }
2863
+ }
2864
+
2865
+ @keyframes spin-slow {
2866
+ from { transform: rotate(0deg); }
2867
+ to { transform: rotate(360deg); }
2868
+ }
2869
+
2870
+ /* Conclusion arrow pulse */
2871
+ @keyframes pulseArrow {
2872
+ 0% { transform: scale(1); opacity: 1; }
2873
+ 50% { transform: scale(1.08); opacity: 0.85; }
2874
+ 100% { transform: scale(1); opacity: 1; }
2875
+ }
2876
+
2877
+ @media (prefers-reduced-motion: reduce) {
2878
+ [style*='pulseArrow'] {
2879
+ animation: none !important;
2880
+ }
2881
+ .processing-status,
2882
+ .processing-icon {
2883
+ animation: none !important;
2884
+ }
2885
+ }
2886
+
2887
+ /* ------------------------------------------------------------------
2888
+ Attempts Tracker + Init Banner + Alerts
2889
+ ------------------------------------------------------------------ */
2890
+
2891
+ .init-banner {
2892
+ background: var(--card-bg-strong);
2893
+ padding: 12px;
2894
+ border-radius: 8px;
2895
+ text-align: center;
2896
+ margin-bottom: 16px;
2897
+ border: 1px solid var(--card-border-subtle);
2898
+ color: var(--text-main);
2899
+ }
2900
+
2901
+ .init-banner__text {
2902
+ margin: 0;
2903
+ font-weight: 500;
2904
+ color: var(--text-muted);
2905
+ }
2906
+
2907
+ /* Attempts tracker shell */
2908
+ .attempts-tracker {
2909
+ text-align: center;
2910
+ padding: 8px;
2911
+ margin: 8px 0;
2912
+ background: var(--block-background-fill);
2913
+ border-radius: 8px;
2914
+ border: 1px solid var(--card-border-subtle);
2915
+ }
2916
+
2917
+ .attempts-tracker__text {
2918
+ margin: 0;
2919
+ font-weight: 600;
2920
+ font-size: 1rem;
2921
+ color: var(--accent-strong);
2922
+ }
2923
+
2924
+ /* Limit reached variant – we *still* stick to theme colors */
2925
+ .attempts-tracker--limit .attempts-tracker__text {
2926
+ color: var(--text-main);
2927
+ }
2928
+
2929
+ /* Generic alert helpers used in inline login messages */
2930
+ .alert {
2931
+ padding: 12px 16px;
2932
+ border-radius: 8px;
2933
+ margin-top: 12px;
2934
+ text-align: left;
2935
+ font-size: 0.95rem;
2936
+ }
2937
+
2938
+ .alert--error {
2939
+ border-left: 4px solid var(--accent-strong);
2940
+ background: var(--block-background-fill);
2941
+ color: var(--text-main);
2942
+ }
2943
+
2944
+ .alert--success {
2945
+ border-left: 4px solid var(--accent-strong);
2946
+ background: var(--block-background-fill);
2947
+ color: var(--text-main);
2948
+ }
2949
+
2950
+ .alert__title {
2951
+ margin: 0;
2952
+ font-weight: 600;
2953
+ color: var(--text-main);
2954
+ }
2955
+
2956
+ .alert__body {
2957
+ margin: 8px 0 0 0;
2958
+ color: var(--text-muted);
2959
+ }
2960
+
2961
+ /* ------------------------------------------------------------------
2962
+ Navigation Loading Overlay
2963
+ ------------------------------------------------------------------ */
2964
+
2965
+ #nav-loading-overlay {
2966
+ position: fixed;
2967
+ top: 0;
2968
+ left: 0;
2969
+ width: 100%;
2970
+ height: 100%;
2971
+ background: color-mix(in srgb, var(--body-background-fill) 90%, transparent);
2972
+ z-index: 9999;
2973
+ display: none;
2974
+ flex-direction: column;
2975
+ align-items: center;
2976
+ justify-content: center;
2977
+ opacity: 0;
2978
+ transition: opacity 0.3s ease;
2979
+ }
2980
+
2981
+ .nav-spinner {
2982
+ width: 50px;
2983
+ height: 50px;
2984
+ border: 5px solid var(--card-border-subtle);
2985
+ border-top: 5px solid var(--accent-strong);
2986
+ border-radius: 50%;
2987
+ animation: nav-spin 1s linear infinite;
2988
+ margin-bottom: 20px;
2989
+ }
2990
+
2991
+ @keyframes nav-spin {
2992
+ 0% { transform: rotate(0deg); }
2993
+ 100% { transform: rotate(360deg); }
2994
+ }
2995
+
2996
+ #nav-loading-text {
2997
+ font-size: 1.3rem;
2998
+ font-weight: 600;
2999
+ color: var(--accent-strong);
3000
+ }
3001
+
3002
+ /* ------------------------------------------------------------------
3003
+ Utility: Image inversion for dark mode (if needed)
3004
+ ------------------------------------------------------------------ */
3005
+
3006
+ .dark-invert-image {
3007
+ filter: invert(0);
3008
+ }
3009
+
3010
+ @media (prefers-color-scheme: dark) {
3011
+ .dark-invert-image {
3012
+ filter: invert(1) hue-rotate(180deg);
3013
+ }
3014
+ }
3015
+
3016
+ /* ------------------------------------------------------------------
3017
+ Dark Mode Specific Fine Tuning
3018
+ ------------------------------------------------------------------ */
3019
+
3020
+ @media (prefers-color-scheme: dark) {
3021
+ .panel-box,
3022
+ .leaderboard-box,
3023
+ .mock-ui-box,
3024
+ .mock-ui-inner,
3025
+ .processing-status,
3026
+ .kpi-card {
3027
+ background: color-mix(in srgb, var(--block-background-fill) 85%, #000 15%);
3028
+ border-color: color-mix(in srgb, var(--card-border-subtle) 70%, var(--accent-strong) 30%);
3029
+ }
3030
+
3031
+ .leaderboard-html-table thead {
3032
+ background: color-mix(in srgb, var(--block-background-fill) 75%, #000 25%);
3033
+ }
3034
+
3035
+ .lb-placeholder {
3036
+ background: color-mix(in srgb, var(--block-background-fill) 75%, #000 25%);
3037
+ }
3038
+
3039
+ #nav-loading-overlay {
3040
+ background: color-mix(in srgb, #000 70%, var(--body-background-fill) 30%);
3041
+ }
3042
+ }
3043
+
3044
+ /* ---------- Conclusion Card Theme Tokens ---------- */
3045
+
3046
+ /* Light theme defaults */
3047
+ :root,
3048
+ :root[data-theme="light"] {
3049
+ --conclusion-card-bg: #e0f2fe; /* light sky */
3050
+ --conclusion-card-border: #0369a1; /* sky-700 */
3051
+ --conclusion-card-fg: #0f172a; /* slate-900 */
3052
+
3053
+ --conclusion-tip-bg: #fef9c3; /* amber-100 */
3054
+ --conclusion-tip-border: #f59e0b; /* amber-500 */
3055
+ --conclusion-tip-fg: #713f12; /* amber-900 */
3056
+
3057
+ --conclusion-ethics-bg: #fef2f2; /* red-50 */
3058
+ --conclusion-ethics-border: #ef4444; /* red-500 */
3059
+ --conclusion-ethics-fg: #7f1d1d; /* red-900 */
3060
+
3061
+ --conclusion-attempt-bg: #fee2e2; /* red-100 */
3062
+ --conclusion-attempt-border: #ef4444; /* red-500 */
3063
+ --conclusion-attempt-fg: #7f1d1d; /* red-900 */
3064
+
3065
+ --conclusion-next-fg: #0f172a; /* main text color */
3066
+ }
3067
+
3068
+ /* Dark theme overrides – keep contrast high on dark background */
3069
+ [data-theme="dark"] {
3070
+ --conclusion-card-bg: #020617; /* slate-950 */
3071
+ --conclusion-card-border: #38bdf8; /* sky-400 */
3072
+ --conclusion-card-fg: #e5e7eb; /* slate-200 */
3073
+
3074
+ --conclusion-tip-bg: rgba(250, 204, 21, 0.08); /* soft amber tint */
3075
+ --conclusion-tip-border: #facc15; /* amber-400 */
3076
+ --conclusion-tip-fg: #facc15;
3077
+
3078
+ --conclusion-ethics-bg: rgba(248, 113, 113, 0.10); /* soft red tint */
3079
+ --conclusion-ethics-border: #f97373; /* red-ish */
3080
+ --conclusion-ethics-fg: #fecaca;
3081
+
3082
+ --conclusion-attempt-bg: rgba(248, 113, 113, 0.16);
3083
+ --conclusion-attempt-border: #f97373;
3084
+ --conclusion-attempt-fg: #fee2e2;
3085
+
3086
+ --conclusion-next-fg: #e5e7eb;
3087
+ }
3088
+
3089
+ /* ---------- Conclusion Layout ---------- */
3090
+
3091
+ .app-conclusion-wrapper {
3092
+ text-align: center;
3093
+ }
3094
+
3095
+ .app-conclusion-title {
3096
+ font-size: 2.4rem;
3097
+ margin: 0;
3098
+ }
3099
+
3100
+ .app-conclusion-card {
3101
+ margin-top: 24px;
3102
+ max-width: 950px;
3103
+ margin-left: auto;
3104
+ margin-right: auto;
3105
+ padding: 28px;
3106
+ border-radius: 18px;
3107
+ border-width: 3px;
3108
+ border-style: solid;
3109
+ background: var(--conclusion-card-bg);
3110
+ border-color: var(--conclusion-card-border);
3111
+ color: var(--conclusion-card-fg);
3112
+ box-shadow: 0 20px 40px rgba(15, 23, 42, 0.25);
3113
+ }
3114
+
3115
+ .app-conclusion-subtitle {
3116
+ margin-top: 0;
3117
+ font-size: 1.5rem;
3118
+ }
3119
+
3120
+ .app-conclusion-metrics {
3121
+ list-style: none;
3122
+ padding: 0;
3123
+ font-size: 1.05rem;
3124
+ text-align: left;
3125
+ max-width: 640px;
3126
+ margin: 20px auto;
3127
+ }
3128
+
3129
+ /* ---------- Generic panel helpers reused here ---------- */
3130
+
3131
+ .app-panel-tip,
3132
+ .app-panel-critical,
3133
+ .app-panel-warning {
3134
+ padding: 16px;
3135
+ border-radius: 12px;
3136
+ border-left-width: 6px;
3137
+ border-left-style: solid;
3138
+ text-align: left;
3139
+ font-size: 0.98rem;
3140
+ line-height: 1.4;
3141
+ margin-top: 16px;
3142
+ }
3143
+
3144
+ .app-panel-title {
3145
+ margin: 0 0 4px 0;
3146
+ font-weight: 700;
3147
+ }
3148
+
3149
+ .app-panel-body {
3150
+ margin: 0;
3151
+ }
3152
+
3153
+ /* Specific variants */
3154
+
3155
+ .app-conclusion-tip.app-panel-tip {
3156
+ background: var(--conclusion-tip-bg);
3157
+ border-left-color: var(--conclusion-tip-border);
3158
+ color: var(--conclusion-tip-fg);
3159
+ }
3160
+
3161
+ .app-conclusion-ethics.app-panel-critical {
3162
+ background: var(--conclusion-ethics-bg);
3163
+ border-left-color: var(--conclusion-ethics-border);
3164
+ color: var(--conclusion-ethics-fg);
3165
+ }
3166
+
3167
+ .app-conclusion-attempt-cap.app-panel-warning {
3168
+ background: var(--conclusion-attempt-bg);
3169
+ border-left-color: var(--conclusion-attempt-border);
3170
+ color: var(--conclusion-attempt-fg);
3171
+ }
3172
+
3173
+ /* Divider + next section */
3174
+
3175
+ .app-conclusion-divider {
3176
+ margin: 28px 0;
3177
+ border: 0;
3178
+ border-top: 2px solid rgba(148, 163, 184, 0.8); /* slate-400-ish */
3179
+ }
3180
+
3181
+ .app-conclusion-next-title {
3182
+ margin: 0;
3183
+ color: var(--conclusion-next-fg);
3184
+ }
3185
+
3186
+ .app-conclusion-next-body {
3187
+ font-size: 1rem;
3188
+ color: var(--conclusion-next-fg);
3189
+ }
3190
+
3191
+ /* Arrow inherits the same color, keeps pulse animation defined earlier */
3192
+ .app-conclusion-arrow {
3193
+ margin: 12px 0;
3194
+ font-size: 3rem;
3195
+ animation: pulseArrow 2.5s infinite;
3196
+ color: var(--conclusion-next-fg);
3197
+ }
3198
+
3199
+ /* ---------------------------------------------------- */
3200
+ /* Final Conclusion Slide (Light Mode Defaults) */
3201
+ /* ---------------------------------------------------- */
3202
+
3203
+ .final-conclusion-root {
3204
+ text-align: center;
3205
+ color: var(--body-text-color);
3206
+ }
3207
+
3208
+ .final-conclusion-title {
3209
+ font-size: 2.4rem;
3210
+ margin: 0;
3211
+ }
3212
+
3213
+ .final-conclusion-card {
3214
+ background-color: var(--block-background-fill);
3215
+ color: var(--body-text-color);
3216
+ padding: 28px;
3217
+ border-radius: 18px;
3218
+ border: 2px solid var(--border-color-primary);
3219
+ margin-top: 24px;
3220
+ max-width: 950px;
3221
+ margin-left: auto;
3222
+ margin-right: auto;
3223
+ box-shadow: var(--shadow-drop, 0 4px 10px rgba(15, 23, 42, 0.08));
3224
+ }
3225
+
3226
+ .final-conclusion-subtitle {
3227
+ margin-top: 0;
3228
+ margin-bottom: 8px;
3229
+ }
3230
+
3231
+ .final-conclusion-list {
3232
+ list-style: none;
3233
+ padding: 0;
3234
+ font-size: 1.05rem;
3235
+ text-align: left;
3236
+ max-width: 640px;
3237
+ margin: 20px auto;
3238
+ }
3239
+
3240
+ .final-conclusion-list li {
3241
+ margin: 4px 0;
3242
+ }
3243
+
3244
+ .final-conclusion-tip {
3245
+ margin-top: 16px;
3246
+ padding: 16px;
3247
+ border-radius: 12px;
3248
+ border-left: 6px solid var(--color-accent);
3249
+ background-color: color-mix(in srgb, var(--color-accent) 12%, transparent);
3250
+ text-align: left;
3251
+ font-size: 0.98rem;
3252
+ line-height: 1.4;
3253
+ }
3254
+
3255
+ .final-conclusion-ethics {
3256
+ margin-top: 16px;
3257
+ padding: 18px;
3258
+ border-radius: 12px;
3259
+ border-left: 6px solid #ef4444;
3260
+ background-color: color-mix(in srgb, #ef4444 10%, transparent);
3261
+ text-align: left;
3262
+ font-size: 0.98rem;
3263
+ line-height: 1.4;
3264
+ }
3265
+
3266
+ .final-conclusion-attempt-cap {
3267
+ margin-top: 16px;
3268
+ padding: 16px;
3269
+ border-radius: 12px;
3270
+ border-left: 6px solid #ef4444;
3271
+ background-color: color-mix(in srgb, #ef4444 16%, transparent);
3272
+ text-align: left;
3273
+ font-size: 0.98rem;
3274
+ line-height: 1.4;
3275
+ }
3276
+
3277
+ .final-conclusion-divider {
3278
+ margin: 28px 0;
3279
+ border: 0;
3280
+ border-top: 2px solid var(--border-color-primary);
3281
+ }
3282
+
3283
+ .final-conclusion-next h2 {
3284
+ margin: 0;
3285
+ }
3286
+
3287
+ .final-conclusion-next p {
3288
+ font-size: 1rem;
3289
+ margin-top: 4px;
3290
+ margin-bottom: 0;
3291
+ }
3292
+
3293
+ .final-conclusion-scroll {
3294
+ margin: 12px 0 0 0;
3295
+ font-size: 3rem;
3296
+ animation: pulseArrow 2.5s infinite;
3297
+ }
3298
+
3299
+ /* ---------------------------------------------------- */
3300
+ /* Dark Mode Overrides for Final Slide */
3301
+ /* ---------------------------------------------------- */
3302
+
3303
+ @media (prefers-color-scheme: dark) {
3304
+ .final-conclusion-card {
3305
+ background-color: #0b1120; /* deep slate */
3306
+ color: white; /* 100% contrast confidence */
3307
+ border-color: #38bdf8;
3308
+ box-shadow: none;
3309
+ }
3310
+
3311
+ .final-conclusion-tip {
3312
+ background-color: rgba(56, 189, 248, 0.18);
3313
+ }
3314
+
3315
+ .final-conclusion-ethics {
3316
+ background-color: rgba(248, 113, 113, 0.18);
3317
+ }
3318
+
3319
+ .final-conclusion-attempt-cap {
3320
+ background-color: rgba(248, 113, 113, 0.26);
3321
+ }
3322
+ }
3323
+ /* ---------------------------------------------------- */
3324
+ /* Slide 3: INPUT → MODEL → OUTPUT flow (theme-aware) */
3325
+ /* ---------------------------------------------------- */
3326
+
3327
+
3328
+ .model-flow {
3329
+ text-align: center;
3330
+ font-weight: 600;
3331
+ font-size: 1.2rem;
3332
+ margin: 20px 0;
3333
+ /* No explicit color – inherit from the card */
3334
+ }
3335
+
3336
+ .model-flow-label {
3337
+ padding: 0 0.1rem;
3338
+ /* No explicit color – inherit */
3339
+ }
3340
+
3341
+ .model-flow-arrow {
3342
+ margin: 0 0.35rem;
3343
+ font-size: 1.4rem;
3344
+ /* No explicit color – inherit */
3345
+ }
3346
+
3347
+ @media (prefers-color-scheme: dark) {
3348
+ .model-flow {
3349
+ color: var(--body-text-color);
3350
+ }
3351
+ .model-flow-arrow {
3352
+ /* In dark mode, nudge arrows toward accent for contrast/confidence */
3353
+ color: color-mix(in srgb, var(--color-accent) 75%, var(--body-text-color) 25%);
3354
+ }
3355
+ }
3356
+ """
3357
+
3358
+
3359
+ # Define globals for yield
3360
+ global submit_button, submission_feedback_display, team_leaderboard_display
3361
+ # --- THIS IS THE FIXED LINE ---
3362
+ global individual_leaderboard_display, last_submission_score_state, last_rank_state, best_score_state, submission_count_state, first_submission_score_state
3363
+ # --- END OF FIX ---
3364
+ global rank_message_display, model_type_radio, complexity_slider
3365
+ global feature_set_checkbox, data_size_radio
3366
+ global login_username, login_password, login_submit, login_error
3367
+ global attempts_tracker_display, team_name_state
3368
+
3369
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), css=css) as demo:
3370
+ # Persistent top anchor for scroll-to-top navigation
3371
+ gr.HTML("<div id='app_top_anchor' style='height:0;'></div>")
3372
+
3373
+ # Navigation loading overlay with spinner and dynamic message
3374
+ gr.HTML("""
3375
+ <div id='nav-loading-overlay'>
3376
+ <div class='nav-spinner'></div>
3377
+ <span id='nav-loading-text'>Loading...</span>
3378
+ </div>
3379
+ """)
3380
+
3381
+ # Concurrency Note: Do NOT read per-user state from os.environ here.
3382
+ # Username and other per-user data are managed via gr.State objects
3383
+ # and populated during handle_load_with_session_auth.
3384
+
3385
+ # Loading screen
3386
+ with gr.Column(visible=False) as loading_screen:
3387
+ gr.Markdown(
3388
+ """
3389
+ <div style='text-align:center; padding:100px 0;'>
3390
+ <h2 style='font-size:2rem; color:#6b7280;'>⏳ Loading...</h2>
3391
+ </div>
3392
+ """
3393
+ )
3394
+
3395
+ # --- Briefing Slideshow (Updated with New Cards) ---
3396
+
3397
+ # Slide 1: From Understanding to Building (Retained as transition)
3398
+ with gr.Column(visible=True, elem_id="slide-1") as briefing_slide_1:
3399
+ gr.Markdown("<h1 style='text-align:center;'>🔄 De la teoria a la práctica</h1>")
3400
+ gr.HTML(
3401
+ """
3402
+ <div class='slide-content'>
3403
+ <div class='panel-box'>
3404
+ <h3 style='font-size: 1.5rem; text-align:center; margin-top:0;'>¡Buen trabajo! Ahora ya has conseguido:</h3>
3405
+
3406
+ <ul style='list-style: none; padding-left: 0; margin-top: 24px; margin-bottom: 24px;'>
3407
+ <li style='font-size: 1.1rem; font-weight: 500; margin-bottom: 12px;'>
3408
+ <span style='font-size: 1.5rem; vertical-align: middle;'>✅</span>
3409
+ Tomar decisiones difíciles como juez o jueza utilizando predicciones de IA
3410
+ </li>
3411
+ <li style='font-size: 1.1rem; font-weight: 500; margin-bottom: 12px;'>
3412
+ <span style='font-size: 1.5rem; vertical-align: middle;'>✅</span>
3413
+ Aprender qué son los falsos positivos (falsas alarmas) y los falsos negativos (alertas ignoradas)
3414
+ </li>
3415
+ <li style='font-size: 1.1rem; font-weight: 500; margin-bottom: 12px;'>
3416
+ <span style='font-size: 1.5rem; vertical-align: middle;'>✅</span>
3417
+ Entender los principios básicos de cómo funciona la IA:
3418
+ </li>
3419
+ </ul>
3420
+
3421
+ <div style='background:white; padding:16px; border-radius:12px; margin:12px 0; text-align:center;'>
3422
+ <div style='display:inline-block; background:#dbeafe; padding:12px 16px; border-radius:8px; margin:4px;'>
3423
+ <h3 style='margin:0; color:#0369a1;'>ENTRADA</h3>
3424
+ </div>
3425
+ <div style='display:inline-block; font-size:1.5rem; margin:0 8px; color:#6b7280;'>→</div>
3426
+ <div style='display:inline-block; background:#fef3c7; padding:12px 16px; border-radius:8px; margin:4px;'>
3427
+ <h3 style='margin:0; color:#92400e;'>MODELO</h3>
3428
+ </div>
3429
+ <div style='display:inline-block; font-size:1.5rem; margin:0 8px; color:#6b7280;'>→</div>
3430
+ <div style='display:inline-block; background:#f0fdf4; padding:12px 16px; border-radius:8px; margin:4px;'>
3431
+ <h3 style='margin:0; color:#15803d;'>RESULTADO</h3>
3432
+ </div>
3433
+ </div>
3434
+
3435
+ <hr style='margin: 24px 0; border-top: 2px solid #c7d2fe;'>
3436
+
3437
+ <h3 style='font-size: 1.5rem; text-align:center;'>Ahora ha llegado el momento de ponerte en la piel de una persona ingeniera de IA.</h3>
3438
+ <p style='font-size: 1.1rem; text-align:center; margin-top: 12px;'>
3439
+ <strong>Tu nuevo reto:</strong> Crear modelos de IA que sean más precisos que el que utilizaste en el rol de juzgar casos.
3440
+ </p>
3441
+ <p style='font-size: 1.1rem; text-align:center; margin-top: 12px;'>
3442
+ Recuerda: has vivido en primera persona cómo las predicciones de la IA afectan la vida real de las personas. Usa ese conocimiento para construir algo mejor.
3443
+ </p>
3444
+ </div>
3445
+ </div>
3446
+ """
3447
+ )
3448
+ briefing_1_next = gr.Button("Siguiente ▶️", variant="primary", size="lg")
3449
+
3450
+ # Slide 2: Card 1 (Your Engineering Mission)
3451
+ with gr.Column(visible=False, elem_id="slide-2") as briefing_slide_2:
3452
+ gr.Markdown("<h1 style='text-align:center;'>📋 Tu misión - Construir una IA mejor</h1>")
3453
+
3454
+ gr.HTML(
3455
+ """
3456
+ <div class='slide-content'>
3457
+ <div class='panel-box'>
3458
+ <h3>La misión</h3>
3459
+ <p>Crea un modelo de IA que ayude a los tribunales a tomar decisiones más acertadas. El modelo que utilizaste antes te daba recomendaciones imperfectas. Ahora tu tarea es construir un modelo nuevo que prediga el riesgo con mayor precisión y ofrezca a quienes juzgan información fiable para poder ser justos y justas.</p>
3460
+
3461
+ <h3>La competición</h3>
3462
+ <p>Para lograrlo, competirás con otras personas ingenieras. Formarás parte de un equipo de ingeniería que te ayudará en tu misión. Tus resultados se registrarán tanto de forma individual como colectiva en las clasificaciones en tiempo real.</p>
3463
+ </div>
3464
+
3465
+ <div class='leaderboard-box' style='max-width: 600px; margin: 16px auto; text-align: center; padding: 16px;'>
3466
+ <p style='font-size: 1.1rem; margin:0;'>Te unirás a un equipo como...</p>
3467
+ <h3 style='font-size: 1.75rem; color: #6b7280; margin: 8px 0;'>
3468
+ 🛡️ Los Exploradores Éticos
3469
+ </h3>
3470
+ </div>
3471
+
3472
+ <div class='mock-ui-box'>
3473
+ <h3>El reto de los datos</h3>
3474
+ <p>Para competir, tendrás acceso a miles de expedientes de casos antiguos. Dispones de dos tipos de información:</p>
3475
+ <ol style='list-style-position: inside; padding-left: 20px;'>
3476
+ <li><strong>Perfiles de personas presas:</strong> Es la información que tenía el tribunal en el momento de la detención.
3477
+ <ul style='margin-left: 20px; list-style-type: disc;'>
3478
+ <li><em>Edad, número de antecedentes penales, tipo de cargo penal.</em></li>
3479
+ </ul>
3480
+ </li>
3481
+ <li><strong>Resultados históricos:</strong> Esto es lo que ocurrió con esas personas pasado un tiempo.
3482
+ <ul style='margin-left: 20px; list-style-type: disc;'>
3483
+ <li><em>¿Volvieron a cometer un delito en dos años? (Sí/No)</em></li>
3484
+ </ul>
3485
+ </li>
3486
+ </ol>
3487
+
3488
+ <h3>La tarea principal</h3>
3489
+ <p>Debes enseñar a tu modelo de IA a analizar los "perfiles" y predecir con precisión el "resultado".</p>
3490
+ <p><strong>¿Te animas a construir algo que podría cambiar la manera en que funciona la justicia?</strong></p>
3491
+ </div>
3492
+ </div>
3493
+ """
3494
+ )
3495
+
3496
+ with gr.Row():
3497
+ briefing_2_back = gr.Button("◀️ Atrás", size="lg")
3498
+ briefing_2_next = gr.Button("Siguiente ▶️", variant="primary", size="lg")
3499
+
3500
+ # Slide 3: Card 2 (What is a "Model"?)
3501
+ with gr.Column(visible=False, elem_id="slide-3") as briefing_slide_3:
3502
+ gr.Markdown("<h1 style='text-align:center;'>🧠 ¿Qué es un sistema de IA?</h1>")
3503
+
3504
+ # --- FIX FOR SLIDE 3 ---
3505
+ # Combined all content into single gr.HTML()
3506
+ gr.HTML(
3507
+ """
3508
+ <div class='slide-content'>
3509
+ <div class='panel-box'>
3510
+ <p>Antes de empezar a competir, veamos con claridad qué es exactamente lo que vas a construir.</p>
3511
+ <h3>Piensa en un sistema de IA como una "Máquina de Predicción".</h3>
3512
+ <p>Ya conoces el flujo:</p>
3513
+
3514
+ <div style='background:white; padding:16px; border-radius:12px; margin:12px 0; text-align:center;'>
3515
+ <div style='display:inline-block; background:#dbeafe; padding:12px 16px; border-radius:8px; margin:4px;'>
3516
+ <h3 style='margin:0; color:#0369a1;'>ENTRADA</h3>
3517
+ </div>
3518
+ <div style='display:inline-block; font-size:1.5rem; margin:0 8px; color:#6b7280;'>→</div>
3519
+ <div style='display:inline-block; background:#fef3c7; padding:12px 16px; border-radius:8px; margin:4px;'>
3520
+ <h3 style='margin:0; color:#92400e;'>MODELO</h3>
3521
+ </div>
3522
+ <div style='display:inline-block; font-size:1.5rem; margin:0 8px; color:#6b7280;'>→</div>
3523
+ <div style='display:inline-block; background:#f0fdf4; padding:12px 16px; border-radius:8px; margin:4px;'>
3524
+ <h3 style='margin:0; color:#15803d;'>SALIDA</h3>
3525
+ </div>
3526
+ </div>
3527
+
3528
+ <p>Como persona ingeniera, no necesitas escribir código complejo desde cero. En lugar de eso, vas a construir esta máquina combinando tres componentes principales.</p>
3529
+ </div>
3530
+
3531
+ <div class='mock-ui-box'>
3532
+ <h3>Los tres componentes:</h3>
3533
+ <p><strong>1. La entrada (Datos)</strong><br>
3534
+ La información que le das a la máquina.<br>
3535
+ <em>* Ejemplos: edad, número de antecedentes penales, detalles del cargo penal.</em></p>
3536
+
3537
+ <p><strong>2. El modelo (El cerebro)</strong><br>
3538
+ Este es el "cerebro" de tu máquina. Estudia los datos de entrada e intenta averiguar cómo se conectan las cosas para hacer una predicción. Puedes elegir diferentes estrategias de modelo (cerebros) para tu máquina.<br>
3539
+ <em>* Ejemplos: Algunos "cerebros" solo encuentran reglas sencillas (como marcar un correo si dice 'dinero gratis'). Otros tienen la capacidad de encontrar patrones complejos (como reconocer una cara específica en una multitud).</em></p>
3540
+
3541
+ <p><strong>3. La salida (Predicción)</strong><br>
3542
+ Lo que el modelo intenta adivinar como mejor opción.<br>
3543
+ <em>* Ejemplo: Nivel de riesgo: Alto o Bajo.</em></p>
3544
+
3545
+ <hr>
3546
+
3547
+ <p><strong>Cómo aprende:</strong> Muestras al modelo miles de casos antiguos (Entradas) + lo que pasó realmente (Salidas). El modelo los estudia para encontrar las reglas y así poder hacer predicciones sobre casos nuevos que no ha visto antes.</p>
3548
+ </div>
3549
+ </div>
3550
+ """
3551
+ )
3552
+ # --- END FIX ---
3553
+
3554
+ with gr.Row():
3555
+ briefing_3_back = gr.Button("◀️ Atrás", size="lg")
3556
+ briefing_3_next = gr.Button("Siguiente ▶️", variant="primary", size="lg")
3557
+
3558
+ # Slide 4: Card 3 (How Engineers Work — The Loop)
3559
+ with gr.Column(visible=False, elem_id="slide-4") as briefing_slide_4:
3560
+ gr.Markdown("<h1 style='text-align:center;'>🔁 Cómo trabajan las personas ingenieras — El Bucle</h1>")
3561
+
3562
+ # --- FIX FOR SLIDE 4 ---
3563
+ # Combined all content into single gr.HTML()
3564
+ gr.HTML(
3565
+ """
3566
+ <div class='slide-content'>
3567
+ <div class='panel-box'>
3568
+ <p>Ahora que ya conoces los componentes de un modelo, ¿cómo puedes construir uno mejor?</p>
3569
+ <h3>Aquí tienes el secreto:</h3>
3570
+ <p>Los equipos de IA reales casi nunca aciertan a la primera. En su lugar, siguen un bucle continuo de experimentación: <strong>Probar, comprobar, aprender y repetir.</strong></p>
3571
+
3572
+ <h3>El bucle de experimentación:</h3>
3573
+ <ol style='list-style-position: inside;'>
3574
+ <li><strong>Construye un modelo:</strong> Elige tus componentes y obtendrás una puntuación inicial de precisión.</li>
3575
+ <li><strong>Hazte una pregunta:</strong> (p. ej., "¿Qué pasa si cambio el 'Cerebro' —el tipo de modelo—?")</li>
3576
+ <li><strong>Comprueba y compara:</strong> ¿La puntuación ha mejorado... o ha empeorado?</li>
3577
+ </ol>
3578
+ </div>
3579
+
3580
+ <h3>¡Harás exactamente lo mismo en la competición!</h3>
3581
+
3582
+ <div class='step-visual'>
3583
+ <div class='step-visual-box'><b>1. Configura</b><br/>Usa los controles para seleccionar el tipo de modelo y los datos.</div>
3584
+ <div class='step-visual-arrow'>→</div>
3585
+ <div class='step-visual-box'><b>2. Envía</b><br/>Haz clic en "construir y enviar" para entrenar tu modelo.</div>
3586
+ <div class='step-visual-arrow'>→</div>
3587
+ <div class='step-visual-box'><b>3. Analiza</b><br/>Mira tu posición en la clasificación en tiempo real.</div>
3588
+ <div class='step-visual-arrow'>→</div>
3589
+ <div class='step-visual-box'><b>4. Mejora</b><br/>Cambia una opción y vuelve a enviarlo.</div>
3590
+ </div>
3591
+
3592
+ <div class='leaderboard-box' style='text-align:center;'>
3593
+ <p><strong>Consejo:</strong> Intenta cambiar solo una cosa a la vez. Si cambias demasiadas cosas de golpe, ¡no sabrás qué es lo que ha hecho que tu modelo mejore o empeore!</p>
3594
+ </div>
3595
+ </div>
3596
+ """
3597
+ )
3598
+ # --- END FIX ---
3599
+
3600
+ with gr.Row():
3601
+ briefing_4_back = gr.Button("◀️ Atrás", size="lg")
3602
+ briefing_4_next = gr.Button("Siguiente ▶️", variant="primary", size="lg")
3603
+
3604
+ # Slide 5: Card 4 (Control Knobs — The "Brain" Settings)
3605
+ with gr.Column(visible=False, elem_id="slide-5") as briefing_slide_5:
3606
+ gr.Markdown("<h1 style='text-align:center;'>🎛️ Controles — La configuración del \"cerebro\"</h1>")
3607
+
3608
+ # --- FIX FOR SLIDE 5 ---
3609
+ # Combined all content into single gr.HTML()
3610
+ gr.HTML(
3611
+ """
3612
+ <div class='slide-content'>
3613
+ <div class='mock-ui-inner'>
3614
+ <p>Para construir tu sistema de IA, usarás controles para configurar tu Máquina de Predicción. Los dos primeros controles te permiten elegir la estrategia del modelo (el cerebro) y ajustar cómo aprende patrones a partir de los datos.</p>
3615
+ <hr style='margin: 16px 0;'>
3616
+
3617
+ <h3 style='margin-top:0;'>1. Estrategia del modelo (Tipo de modelo)</h3>
3618
+ <div style='font-size: 1rem; margin-bottom:12px;'>
3619
+ <b>Qué es:</b> El cerebro de tu Máquina de Predicción. Utiliza un método matemático específico —llamado algoritmo— para encontrar patrones en los datos. Una vez aprende de esos patrones, se convierte en un modelo listo para hacer su mejor predicción.
3620
+ </div>
3621
+ <div class='mock-ui-control-box'>
3622
+ <p style='font-size: 1.1rem; margin: 8px 0;'>
3623
+ <span class='mock-ui-radio-on'>◉</span>
3624
+ <b>El Generalista Equilibrado:</b> Aprende a partir de todos los datos y tiene en cuenta varios factores en cada decisión, lo que ayuda a obtener resultados coherentes en diferentes situaciones.
3625
+ </p>
3626
+ <p style='font-size: 1.1rem; margin: 8px 0;'>
3627
+ <span class='mock-ui-radio-off'>○</span>
3628
+ <b>El Creador de Reglas:</b> Utiliza reglas claras del tipo “Si… entonces…”, fáciles de entender pero menos flexibles (Por ejemplo: si hay delitos previos > 2, entonces hay riesgo alto).
3629
+ </p>
3630
+ <p style='font-size: 1.1rem; margin: 8px 0;'>
3631
+ <span class='mock-ui-radio-off'>○</span>
3632
+ <b>El Buscador de Patrones Profundos:</b> Un modelo complejo que encuentra patrones ocultos en los datos, pero cuyas decisiones son más difíciles de explicar.
3633
+ </p>
3634
+ </div>
3635
+
3636
+ <hr style='margin: 24px 0;'>
3637
+
3638
+ <h3>2. Complejidad del Modelo (Nivel de ajuste)</h3>
3639
+ <div class='mock-ui-control-box' style='text-align: center;'>
3640
+ <p style='font-size: 1.1rem; margin:0;'>Rango: Nivel 1 ─── ● ─── 10</p>
3641
+ </div>
3642
+
3643
+ <div style='margin-top: 16px; font-size: 1rem;'>
3644
+ <ul style='list-style-position: inside;'>
3645
+ <li><b>Qué es:</b> Es el nivel de detalle con el que el modelo aprende a partir de los datos: si se centra en patrones generales o también en casos muy específicos.</li>
3646
+ <li><b>El equilibrio:</b>
3647
+ <ul style='list-style-position: inside; margin-left: 20px;'>
3648
+ <li><b>Bajo (Nivel 1):</b> Aprende principalmente a partir de patrones generales de los datos.</li>
3649
+ <li><b>Alto (Nivel 5):</b> Aprende tanto patrones generales como detalles muy finos.</li>
3650
+ </ul>
3651
+ </li>
3652
+ </ul>
3653
+ <p style='color:#b91c1c; font-weight:bold; margin-top:10px;'>Aviso: Si este valor es demasiado alto, el modelo puede “memorizar” detalles aleatorios o coincidencias irrelevantes (ruido) de los datos pasados, en lugar de aprender la regla general.</p>
3654
+ </div>
3655
+ </div>
3656
+ </div>
3657
+ """
3658
+ )
3659
+ # --- END FIX ---
3660
+
3661
+ with gr.Row():
3662
+ briefing_5_back = gr.Button("◀️ Atrás", size="lg")
3663
+ briefing_5_next = gr.Button("Siguiente ▶️", variant="primary", size="lg")
3664
+
3665
+ # Slide 6: Card 5 (Control Knobs — The "Data" Settings)
3666
+ with gr.Column(visible=False, elem_id="slide-6") as briefing_slide_6:
3667
+ gr.Markdown("<h1 style='text-align:center;'>🎛️ Controles — La configuración de los \"datos\"</h1>")
3668
+
3669
+ # --- FIX FOR SLIDE 6 ---
3670
+ # Combined all content into single gr.HTML()
3671
+ gr.HTML(
3672
+ """
3673
+ <div class='slide-content'>
3674
+ <div class='mock-ui-inner'>
3675
+ <p>Ahora que has configurado tu máquina de predicción, debes decidir qué información procesará. Estos selectores controlan los datos de entrada del sistema de IA.</p>
3676
+ <hr style='margin: 16px 0;'>
3677
+
3678
+ <h3 style='margin-top:0;'>3. Variables de datos</h3>
3679
+ <div style='font-size: 1rem; margin-bottom:12px;'>
3680
+ <b>Qué es:</b> Los puntos de datos específicos a los que el sistema de IA (la máquina) tiene permiso para acceder.
3681
+ <br><b>Por qué es importante:</b> El resultado del sistema depende totalmente de la información que recibe.
3682
+ </div>
3683
+
3684
+ <div class='mock-ui-control-box'>
3685
+ <p style='font-size: 1.1rem; margin: 8px 0;'>
3686
+ <span class='mock-ui-radio-on'>☑</span>
3687
+ <b>Datos de comportamiento:</b> Información commo el <i>número de delitos juveniles</i> ayuda al sistema a identificar patrones de riesgo basados en hechos.
3688
+ </p>
3689
+ <p style='font-size: 1.1rem; margin: 8px 0;'>
3690
+ <span class='mock-ui-radio-off'>☐</span>
3691
+ <b>Datos demográficos:</b> Datos como la <i>raza</i> pueden ayudar al modelo a aprender, pero también pueden replicar sesgos humanos.
3692
+ </p>
3693
+ </p>
3694
+ </div>
3695
+ <p style='margin-top:10px;'><b>Tu tarea:</b> Marcar ☑ o Desmarcar ☐ las casillas para elegir qué información "alimentará" a tu modelo.</p>
3696
+
3697
+ <hr style='margin: 24px 0;'>
3698
+
3699
+ <h3>4. Volumen de datos (Volumen de entrenamiento)</h3>
3700
+ <div style='font-size: 1rem; margin-bottom:12px;'>
3701
+ <b>What it is:</b> La cantidad de casos históricos que el sistema de IA utiliza para aprender patrones.
3702
+ </div>
3703
+
3704
+ <div class='mock-ui-control-box'>
3705
+ <p style='font-size: 1.1rem; margin: 8px 0;'>
3706
+ <span class='mock-ui-radio-on'>◉</span>
3707
+ <b>Pequeño (20%):</b> Procesamiento rápido. Ideal para hacer pruebas rápidas y revisar tu configuración.
3708
+ </p>
3709
+ <p style='font-size: 1.1rem; margin: 8px 0;'>
3710
+ <span class='mock-ui-radio-off'>○</span>
3711
+ <b>Completo (100%):</b> Procesamiento máximo de datos. Tarda más en construirse, pero da al sistema de IA la mejor oportunidad para calibrar su precisión.
3712
+ </p>
3713
+
3714
+ </div>
3715
+
3716
+ </div>
3717
+ </div>
3718
+ """
3719
+ )
3720
+ # --- END FIX ---
3721
+
3722
+ with gr.Row():
3723
+ briefing_6_back = gr.Button("◀️ Back", size="lg")
3724
+ briefing_6_next = gr.Button("Next ▶️", variant="primary", size="lg")
3725
+
3726
+ # Slide 7: Card 6 (Your Score as an Engineer)
3727
+ with gr.Column(visible=False, elem_id="slide-7") as briefing_slide_7:
3728
+ gr.Markdown("<h1 style='text-align:center;'>🏆 Tu puntuación como ingeniero/a</h1>")
3729
+
3730
+ # --- FIX FOR SLIDE 7 ---
3731
+ # Combined all content into single gr.HTML()
3732
+ gr.HTML(
3733
+ """
3734
+ <div class='slide-content'>
3735
+ <div class='panel-box'>
3736
+ <p>Ahora que ya sabes cómo construir un modelo, es hora de poner a prueba tus habilidades. Aquí tienes cómo mediremos tu éxito y cómo podrás subir en la clasificación:</p>
3737
+
3738
+ <h3>Cómo se calcula tu puntuación</h3>
3739
+ <ul style='list-style-position: inside;'>
3740
+ <li><strong>Precisión de la predicción:</strong> Tu modelo se pone a prueba con Datos Ocultos (casos guardados en una "caja fuerte secreta" que tu modelo nunca ha visto). Esto simula la predicción del futuro para garantizar que obtengas una puntuación de precisión realista.</li>
3741
+ <li><strong>La clasificación:</strong> Los marcadores en directo siguen tu progreso individualmente y en equipo.</li>
3742
+ </ul>
3743
+
3744
+ <h3>Cómo puedes mejorar: El Juego</h3>
3745
+ <ul style='list-style-position: inside;'>
3746
+ <li><strong>Compite para mejorar:</strong> Refina tu modelo para superar tu mejor marca personal.</li>
3747
+ <li><strong>Progresa como persona ingeniera y desbloquea herramientas:</strong> A medida que envíes más modelos, ganarás posiciones y desbloquearás mejores herramientas de análisis.</li>
3748
+ </ul>
3749
+
3750
+ </div>
3751
+ </div>
3752
+ """
3753
+ )
3754
+ # --- END FIX ---
3755
+
3756
+ with gr.Row():
3757
+ briefing_7_back = gr.Button("◀️ Back", size="lg")
3758
+ briefing_7_next = gr.Button("Begin Model Building ▶️", variant="primary", size="lg")
3759
+
3760
+ # --- End Briefing Slideshow ---
3761
+
3762
+
3763
+ # Model Building App (Main Interface)
3764
+ with gr.Column(visible=False, elem_id="model-step") as model_building_step:
3765
+ gr.Markdown("<h1 style='text-align:center;'>🛠️ Model Building Arena</h1>")
3766
+
3767
+ # Status panel for initialization progress - HIDDEN
3768
+ init_status_display = gr.HTML(value="", visible=False)
3769
+
3770
+ # Banner for UI state
3771
+
3772
+ init_banner = gr.HTML(
3773
+ value=(
3774
+ "<div class='init-banner'>"
3775
+ "<p class='init-banner__text'>"
3776
+ "⏳ Initializing data & leaderboard… you can explore but must wait for readiness to submit."
3777
+ "</p>"
3778
+ "</div>"
3779
+ ),
3780
+ visible=True)
3781
+
3782
+ # Session-based authentication state objects
3783
+ # Concurrency Note: These are initialized to None/empty and populated
3784
+ # during handle_load_with_session_auth. Do NOT use os.environ here.
3785
+ username_state = gr.State(None)
3786
+ token_state = gr.State(None)
3787
+
3788
+ team_name_state = gr.State(None) # Populated via handle_load_with_session_auth
3789
+ last_submission_score_state = gr.State(0.0)
3790
+ last_rank_state = gr.State(0)
3791
+ best_score_state = gr.State(0.0)
3792
+ submission_count_state = gr.State(0)
3793
+ first_submission_score_state = gr.State(None)
3794
+
3795
+ # New states for readiness gating and preview tracking
3796
+ readiness_state = gr.State(False)
3797
+ was_preview_state = gr.State(False)
3798
+ kpi_meta_state = gr.State({})
3799
+ last_seen_ts_state = gr.State(None) # Track last seen user timestamp
3800
+
3801
+ # Buffered states for all dynamic inputs
3802
+ model_type_state = gr.State(DEFAULT_MODEL)
3803
+ complexity_state = gr.State(2)
3804
+ feature_set_state = gr.State(DEFAULT_FEATURE_SET)
3805
+ data_size_state = gr.State(DEFAULT_DATA_SIZE)
3806
+
3807
+ rank_message_display = gr.Markdown("### Rank loading...")
3808
+ with gr.Row():
3809
+ with gr.Column(scale=1):
3810
+
3811
+ model_type_radio = gr.Radio(
3812
+ label="1. Estrategia del modelo",
3813
+ # UPDATED: Use the list of tuples [(Spanish, English)]
3814
+ choices=MODEL_RADIO_CHOICES,
3815
+ value=DEFAULT_MODEL, # "The Balanced Generalist" (English Key)
3816
+ interactive=False
3817
+ )
3818
+ model_card_display = gr.Markdown(get_model_card(DEFAULT_MODEL))
3819
+
3820
+ gr.Markdown("---") # Separator
3821
+
3822
+ complexity_slider = gr.Slider(
3823
+ label="2. Model Complexity (1–10)",
3824
+ minimum=1, maximum=3, step=1, value=2,
3825
+ info="Higher values allow deeper pattern learning; very high values may overfit."
3826
+ )
3827
+
3828
+ gr.Markdown("---") # Separator
3829
+
3830
+ feature_set_checkbox = gr.CheckboxGroup(
3831
+ label="3. Select Data Ingredients",
3832
+ choices=FEATURE_SET_ALL_OPTIONS,
3833
+ value=DEFAULT_FEATURE_SET,
3834
+ interactive=False,
3835
+ info="More ingredients unlock as you rank up!"
3836
+ )
3837
+
3838
+ gr.Markdown("---") # Separator
3839
+
3840
+ data_size_radio = gr.Radio(
3841
+ label="4. Data Size",
3842
+ choices=[DEFAULT_DATA_SIZE],
3843
+ value=DEFAULT_DATA_SIZE,
3844
+ interactive=False
3845
+ )
3846
+
3847
+ gr.Markdown("---") # Separator
3848
+
3849
+ # Attempt tracker display
3850
+ attempts_tracker_display = gr.HTML(
3851
+ value="<div style='text-align:center; padding:8px; margin:8px 0; background:#f0f9ff; border-radius:8px; border:1px solid #bae6fd;'>"
3852
+ "<p style='margin:0; color:#0369a1; font-weight:600; font-size:1rem;'>📊 Attempts used: 0/10</p>"
3853
+ "</div>",
3854
+ visible=True
3855
+ )
3856
+
3857
+ submit_button = gr.Button(
3858
+ value="5. 🔬 Build & Submit Model",
3859
+ variant="primary",
3860
+ size="lg"
3861
+ )
3862
+
3863
+ with gr.Column(scale=1):
3864
+ gr.HTML(
3865
+ """
3866
+ <div class='leaderboard-box'>
3867
+ <h3 style='margin-top:0;'>🏆 Live Standings</h3>
3868
+ <p style='margin:0;'>Submit a model to see your rank.</p>
3869
+ </div>
3870
+ """
3871
+ )
3872
+
3873
+ # KPI Card
3874
+ submission_feedback_display = gr.HTML(
3875
+ "<p style='text-align:center; color:#6b7280; padding:20px 0;'>Submit your first model to get feedback!</p>"
3876
+ )
3877
+
3878
+ # Inline Login Components (initially hidden)
3879
+ login_username = gr.Textbox(
3880
+ label="Username",
3881
+ placeholder="Enter your modelshare.ai username",
3882
+ visible=False
3883
+ )
3884
+ login_password = gr.Textbox(
3885
+ label="Password",
3886
+ type="password",
3887
+ placeholder="Enter your password",
3888
+ visible=False
3889
+ )
3890
+ login_submit = gr.Button(
3891
+ "Sign In & Submit",
3892
+ variant="primary",
3893
+ visible=False
3894
+ )
3895
+ login_error = gr.HTML(
3896
+ value="",
3897
+ visible=False
3898
+ )
3899
+
3900
+ with gr.Tabs():
3901
+ with gr.TabItem("Team Standings"):
3902
+ team_leaderboard_display = gr.HTML(
3903
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Submit a model to see team rankings.</p>"
3904
+ )
3905
+ with gr.TabItem("Individual Standings"):
3906
+ individual_leaderboard_display = gr.HTML(
3907
+ "<p style='text-align:center; color:#6b7280; padding-top:20px;'>Submit a model to see individual rankings.</p>"
3908
+ )
3909
+
3910
+ # REMOVED: Ethical Reminder HTML Block
3911
+ step_2_next = gr.Button("Finish & Reflect ▶️", variant="secondary")
3912
+
3913
+ # Conclusion Step
3914
+ with gr.Column(visible=False, elem_id="conclusion-step") as conclusion_step:
3915
+ gr.Markdown("<h1 style='text-align:center;'>✅ Section Complete</h1>")
3916
+ final_score_display = gr.HTML(value="<p>Preparing final summary...</p>")
3917
+ step_3_back = gr.Button("◀️ Back to Experiment")
3918
+
3919
+ # --- Navigation Logic ---
3920
+ all_steps_nav = [
3921
+ briefing_slide_1, briefing_slide_2, briefing_slide_3,
3922
+ briefing_slide_4, briefing_slide_5, briefing_slide_6, briefing_slide_7,
3923
+ model_building_step, conclusion_step, loading_screen
3924
+ ]
3925
+
3926
+ def create_nav(current_step, next_step):
3927
+ """
3928
+ Simplified navigation: directly switches visibility without artificial loading screen.
3929
+ Loading screen only shown when entering arena if not yet ready.
3930
+ """
3931
+ def _nav():
3932
+ # Direct single-step navigation
3933
+ updates = {next_step: gr.update(visible=True)}
3934
+ for s in all_steps_nav:
3935
+ if s != next_step:
3936
+ updates[s] = gr.update(visible=False)
3937
+ return updates
3938
+ return _nav
3939
+
3940
+ def finalize_and_show_conclusion(best_score, submissions, rank, first_score, feature_set):
3941
+ """Build dynamic conclusion HTML and navigate to conclusion step."""
3942
+ html = build_final_conclusion_html(best_score, submissions, rank, first_score, feature_set)
3943
+ updates = {
3944
+ conclusion_step: gr.update(visible=True),
3945
+ final_score_display: gr.update(value=html)
3946
+ }
3947
+ for s in all_steps_nav:
3948
+ if s != conclusion_step:
3949
+ updates[s] = gr.update(visible=False)
3950
+ return [updates[s] if s in updates else gr.update() for s in all_steps_nav] + [html]
3951
+
3952
+ # Helper function to generate navigation JS with loading overlay
3953
+ def nav_js(target_id: str, message: str, min_show_ms: int = 1200) -> str:
3954
+ """
3955
+ Generate JavaScript for enhanced slide navigation with loading overlay.
3956
+
3957
+ Args:
3958
+ target_id: Element ID of the target slide (e.g., 'slide-2', 'model-step')
3959
+ message: Loading message to display during transition
3960
+ min_show_ms: Minimum time to show overlay (prevents flicker)
3961
+
3962
+ Returns:
3963
+ JavaScript arrow function string for Gradio's js parameter
3964
+ """
3965
+ return f"""
3966
+ ()=>{{
3967
+ try {{
3968
+ // Show overlay immediately
3969
+ const overlay = document.getElementById('nav-loading-overlay');
3970
+ const messageEl = document.getElementById('nav-loading-text');
3971
+ if(overlay && messageEl) {{
3972
+ messageEl.textContent = '{message}';
3973
+ overlay.style.display = 'flex';
3974
+ setTimeout(() => {{ overlay.style.opacity = '1'; }}, 10);
3975
+ }}
3976
+
3977
+ const startTime = Date.now();
3978
+
3979
+ // Scroll to top after brief delay
3980
+ setTimeout(() => {{
3981
+ const anchor = document.getElementById('app_top_anchor');
3982
+ const container = document.querySelector('.gradio-container') || document.scrollingElement || document.documentElement;
3983
+
3984
+ function doScroll() {{
3985
+ if(anchor) {{ anchor.scrollIntoView({{behavior:'smooth', block:'start'}}); }}
3986
+ else {{ container.scrollTo({{top:0, behavior:'smooth'}}); }}
3987
+
3988
+ // Best-effort Colab iframe scroll
3989
+ try {{
3990
+ if(window.parent && window.parent !== window && window.frameElement) {{
3991
+ const top = window.frameElement.getBoundingClientRect().top + window.parent.scrollY;
3992
+ window.parent.scrollTo({{top: Math.max(top - 10, 0), behavior:'smooth'}});
3993
+ }}
3994
+ }} catch(e2) {{}}
3995
+ }}
3996
+
3997
+ doScroll();
3998
+ // Retry scroll to combat layout shifts
3999
+ let scrollAttempts = 0;
4000
+ const scrollInterval = setInterval(() => {{
4001
+ scrollAttempts++;
4002
+ doScroll();
4003
+ if(scrollAttempts >= 3) clearInterval(scrollInterval);
4004
+ }}, 130);
4005
+ }}, 40);
4006
+
4007
+ // Poll for target visibility and minimum display time
4008
+ const targetId = '{target_id}';
4009
+ const minShowMs = {min_show_ms};
4010
+ let pollCount = 0;
4011
+ const maxPolls = 77; // ~7 seconds max
4012
+
4013
+ const pollInterval = setInterval(() => {{
4014
+ pollCount++;
4015
+ const elapsed = Date.now() - startTime;
4016
+ const target = document.getElementById(targetId);
4017
+ const isVisible = target && target.offsetParent !== null &&
4018
+ window.getComputedStyle(target).display !== 'none';
4019
+
4020
+ // Hide overlay when target is visible AND minimum time elapsed
4021
+ if((isVisible && elapsed >= minShowMs) || pollCount >= maxPolls) {{
4022
+ clearInterval(pollInterval);
4023
+ if(overlay) {{
4024
+ overlay.style.opacity = '0';
4025
+ setTimeout(() => {{ overlay.style.display = 'none'; }}, 300);
4026
+ }}
4027
+ }}
4028
+ }}, 90);
4029
+
4030
+ }} catch(e) {{ console.warn('nav-js error', e); }}
4031
+ }}
4032
+ """
4033
+
4034
+
4035
+ # Wire up slide buttons with enhanced navigation
4036
+ briefing_1_next.click(
4037
+ fn=create_nav(briefing_slide_1, briefing_slide_2),
4038
+ inputs=None, outputs=all_steps_nav,
4039
+ js=nav_js("slide-2", "Cargando la misión...")
4040
+ )
4041
+ briefing_2_back.click(
4042
+ fn=create_nav(briefing_slide_2, briefing_slide_1),
4043
+ inputs=None, outputs=all_steps_nav,
4044
+ js=nav_js("slide-1", "Volviendo a la introducción...")
4045
+ )
4046
+ briefing_2_next.click(
4047
+ fn=create_nav(briefing_slide_2, briefing_slide_3),
4048
+ inputs=None, outputs=all_steps_nav,
4049
+ js=nav_js("slide-3", "Explorando el concepto de modelo...")
4050
+ )
4051
+ briefing_3_back.click(
4052
+ fn=create_nav(briefing_slide_3, briefing_slide_2),
4053
+ inputs=None, outputs=all_steps_nav,
4054
+ js=nav_js("slide-2", "Retrocediendo...")
4055
+ )
4056
+ briefing_3_next.click(
4057
+ fn=create_nav(briefing_slide_3, briefing_slide_4),
4058
+ inputs=None, outputs=all_steps_nav,
4059
+ js=nav_js("slide-4", "Entendiendo el ciclo del experimento...")
4060
+ )
4061
+ briefing_4_back.click(
4062
+ fn=create_nav(briefing_slide_4, briefing_slide_3),
4063
+ inputs=None, outputs=all_steps_nav,
4064
+ js=nav_js("slide-3", "Repasando conceptos anteriores...")
4065
+ )
4066
+ briefing_4_next.click(
4067
+ fn=create_nav(briefing_slide_4, briefing_slide_5),
4068
+ inputs=None, outputs=all_steps_nav,
4069
+ js=nav_js("slide-5", "Configurando los controles del modelo...")
4070
+ )
4071
+ briefing_5_back.click(
4072
+ fn=create_nav(briefing_slide_5, briefing_slide_4),
4073
+ inputs=None, outputs=all_steps_nav,
4074
+ js=nav_js("slide-4", "Volviendo al ciclo...")
4075
+ )
4076
+ briefing_5_next.click(
4077
+ fn=create_nav(briefing_slide_5, briefing_slide_6),
4078
+ inputs=None, outputs=all_steps_nav,
4079
+ js=nav_js("slide-6", "Configurando los datos...")
4080
+ )
4081
+ briefing_6_back.click(
4082
+ fn=create_nav(briefing_slide_6, briefing_slide_5),
4083
+ inputs=None, outputs=all_steps_nav,
4084
+ js=nav_js("slide-5", "Ajustando la estrategia del modelo...")
4085
+ )
4086
+ briefing_6_next.click(
4087
+ fn=create_nav(briefing_slide_6, briefing_slide_7),
4088
+ inputs=None, outputs=all_steps_nav,
4089
+ js=nav_js("slide-7", "Preparando el resumen de la puntuación...")
4090
+ )
4091
+ briefing_7_back.click(
4092
+ fn=create_nav(briefing_slide_7, briefing_slide_6),
4093
+ inputs=None, outputs=all_steps_nav,
4094
+ js=nav_js("slide-6", "Repasando los controles de datos...")
4095
+ )
4096
+ # Slide 7 -> App
4097
+ briefing_7_next.click(
4098
+ fn=create_nav(briefing_slide_7, model_building_step),
4099
+ inputs=None, outputs=all_steps_nav,
4100
+ js=nav_js("model-step", "Entrando en el área de construcción de modelos...")
4101
+ )
4102
+
4103
+ # App -> Conclusion
4104
+ step_2_next.click(
4105
+ fn=finalize_and_show_conclusion,
4106
+ inputs=[
4107
+ best_score_state,
4108
+ submission_count_state,
4109
+ last_rank_state,
4110
+ first_submission_score_state,
4111
+ feature_set_state
4112
+ ],
4113
+ outputs=all_steps_nav + [final_score_display],
4114
+ js=nav_js("conclusion-step", "Generando el resumen de rendimiento...")
4115
+ )
4116
+
4117
+ # Conclusion -> App
4118
+ step_3_back.click(
4119
+ fn=create_nav(conclusion_step, model_building_step),
4120
+ inputs=None, outputs=all_steps_nav,
4121
+ js=nav_js("model-step", "Volviendo al área de trabajo del experimento...")
4122
+ )
4123
+
4124
+ # Events
4125
+ model_type_radio.change(
4126
+ fn=get_model_card,
4127
+ inputs=model_type_radio,
4128
+ outputs=model_card_display
4129
+ )
4130
+ model_type_radio.change(
4131
+ fn=lambda v: v or DEFAULT_MODEL,
4132
+ inputs=model_type_radio,
4133
+ outputs=model_type_state
4134
+ )
4135
+ complexity_slider.change(fn=lambda v: v, inputs=complexity_slider, outputs=complexity_state)
4136
+
4137
+ feature_set_checkbox.change(
4138
+ fn=lambda v: v or [],
4139
+ inputs=feature_set_checkbox,
4140
+ outputs=feature_set_state
4141
+ )
4142
+ data_size_radio.change(
4143
+ fn=lambda v: v or DEFAULT_DATA_SIZE,
4144
+ inputs=data_size_radio,
4145
+ outputs=data_size_state
4146
+ )
4147
+
4148
+ all_outputs = [
4149
+ submission_feedback_display,
4150
+ team_leaderboard_display,
4151
+ individual_leaderboard_display,
4152
+ last_submission_score_state,
4153
+ last_rank_state,
4154
+ best_score_state,
4155
+ submission_count_state,
4156
+ first_submission_score_state,
4157
+ rank_message_display,
4158
+ model_type_radio,
4159
+ complexity_slider,
4160
+ feature_set_checkbox,
4161
+ data_size_radio,
4162
+ submit_button,
4163
+ login_username,
4164
+ login_password,
4165
+ login_submit,
4166
+ login_error,
4167
+ attempts_tracker_display,
4168
+ was_preview_state,
4169
+ kpi_meta_state,
4170
+ last_seen_ts_state
4171
+ ]
4172
+
4173
+ # Wire up login button
4174
+ login_submit.click(
4175
+ fn=perform_inline_login,
4176
+ inputs=[login_username, login_password],
4177
+ outputs=[
4178
+ login_username,
4179
+ login_password,
4180
+ login_submit,
4181
+ login_error,
4182
+ submit_button,
4183
+ submission_feedback_display,
4184
+ team_name_state,
4185
+ username_state, # NEW
4186
+ token_state # NEW
4187
+ ]
4188
+ )
4189
+
4190
+ # Removed gr.State(username) from the inputs list
4191
+ submit_button.click(
4192
+ fn=run_experiment,
4193
+ inputs=[
4194
+ model_type_state,
4195
+ complexity_state,
4196
+ feature_set_state,
4197
+ data_size_state,
4198
+ team_name_state,
4199
+ last_submission_score_state,
4200
+ last_rank_state,
4201
+ submission_count_state,
4202
+ first_submission_score_state,
4203
+ best_score_state,
4204
+ username_state, # NEW: Session-based auth
4205
+ token_state, # NEW: Session-based auth
4206
+ readiness_state, # Renamed to readiness_flag in function signature
4207
+ was_preview_state, # Renamed to was_preview_prev in function signature
4208
+ # kpi_meta_state removed from inputs - used only as output
4209
+ ],
4210
+ outputs=all_outputs,
4211
+ show_progress="full",
4212
+ js=nav_js("model-step", "Running experiment...", 500)
4213
+ )
4214
+
4215
+ # Timer for polling initialization status
4216
+ status_timer = gr.Timer(value=0.5, active=True) # Poll every 0.5 seconds
4217
+
4218
+ def update_init_status():
4219
+ """
4220
+ Poll initialization status and update UI elements.
4221
+ Returns status HTML, banner visibility, submit button state, data size choices, and readiness_state.
4222
+ """
4223
+ status_html, ready = poll_init_status()
4224
+
4225
+ # Update banner visibility - hide when ready
4226
+ banner_visible = not ready
4227
+
4228
+ # Update submit button
4229
+ if ready:
4230
+ submit_label = "5. 🔬 Build & Submit Model"
4231
+ submit_interactive = True
4232
+ else:
4233
+ submit_label = "⏳ Waiting for data..."
4234
+ submit_interactive = False
4235
+
4236
+ # Get available data sizes based on init progress
4237
+ available_sizes = get_available_data_sizes()
4238
+
4239
+ # Stop timer once fully initialized
4240
+ timer_active = not (ready and INIT_FLAGS.get("pre_samples_full", False))
4241
+
4242
+ return (
4243
+ status_html,
4244
+ gr.update(visible=banner_visible),
4245
+ gr.update(value=submit_label, interactive=submit_interactive),
4246
+ gr.update(choices=available_sizes),
4247
+ timer_active,
4248
+ ready # readiness_state
4249
+ )
4250
+
4251
+ status_timer.tick(
4252
+ fn=update_init_status,
4253
+ inputs=None,
4254
+ outputs=[init_status_display, init_banner, submit_button, data_size_radio, status_timer, readiness_state]
4255
+ )
4256
+
4257
+ # Handle session-based authentication on page load
4258
+ def handle_load_with_session_auth(request: "gr.Request"):
4259
+ """
4260
+ Check for session token, auto-login if present, then load initial UI with stats.
4261
+
4262
+ Concurrency Note: This function does NOT set per-user values in os.environ.
4263
+ All authentication state is returned via gr.State objects (username_state,
4264
+ token_state, team_name_state) to prevent cross-user data leakage.
4265
+ """
4266
+ success, username, token = _try_session_based_auth(request)
4267
+
4268
+ if success and username and token:
4269
+ _log(f"Session auth successful on load for {username}")
4270
+
4271
+ # Get user stats and team from cache/leaderboard
4272
+ stats = _compute_user_stats(username, token)
4273
+ team_name = stats.get("team_name", "")
4274
+
4275
+ # Concurrency Note: Do NOT set os.environ for per-user values.
4276
+ # Return state via gr.State objects exclusively.
4277
+
4278
+ # Hide login form since user is authenticated via session
4279
+ # Return initial load results plus login form hidden
4280
+ # Pass token explicitly for authenticated leaderboard fetch
4281
+ initial_results = on_initial_load(username, token=token, team_name=team_name)
4282
+ return initial_results + (
4283
+ gr.update(visible=False), # login_username
4284
+ gr.update(visible=False), # login_password
4285
+ gr.update(visible=False), # login_submit
4286
+ gr.update(visible=False), # login_error (hide any messages)
4287
+ username, # username_state
4288
+ token, # token_state
4289
+ team_name, # team_name_state
4290
+ )
4291
+ else:
4292
+ _log("No valid session on load, showing login form")
4293
+ # No valid session, proceed with normal load (show login form)
4294
+ # No token available, call without token
4295
+ initial_results = on_initial_load(None, token=None, team_name="")
4296
+ return initial_results + (
4297
+ gr.update(visible=True), # login_username
4298
+ gr.update(visible=True), # login_password
4299
+ gr.update(visible=True), # login_submit
4300
+ gr.update(visible=False), # login_error
4301
+ None, # username_state
4302
+ None, # token_state
4303
+ "", # team_name_state
4304
+ )
4305
+
4306
+ demo.load(
4307
+ fn=handle_load_with_session_auth,
4308
+ inputs=None, # Request is auto-injected
4309
+ outputs=[
4310
+ model_card_display,
4311
+ team_leaderboard_display,
4312
+ individual_leaderboard_display,
4313
+ rank_message_display,
4314
+ model_type_radio,
4315
+ complexity_slider,
4316
+ feature_set_checkbox,
4317
+ data_size_radio,
4318
+ login_username,
4319
+ login_password,
4320
+ login_submit,
4321
+ login_error,
4322
+ username_state, # NEW
4323
+ token_state, # NEW
4324
+ team_name_state, # NEW
4325
+ ]
4326
+ )
4327
+
4328
+ return demo
4329
+
4330
+ # -------------------------------------------------------------------------
4331
+ # 4. Convenience Launcher
4332
+ # -------------------------------------------------------------------------
4333
+
4334
+ def launch_model_building_game_es_app(height: int = 1200, share: bool = False, debug: bool = False) -> None:
4335
+ """
4336
+ Create and directly launch the Model Building Game app inline (e.g., in notebooks).
4337
+ """
4338
+ global playground, X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST
4339
+ if playground is None:
4340
+ try:
4341
+ playground = Competition(MY_PLAYGROUND_ID)
4342
+ except Exception as e:
4343
+ print(f"WARNING: Could not connect to playground: {e}")
4344
+ playground = None
4345
+
4346
+ if X_TRAIN_RAW is None:
4347
+ X_TRAIN_RAW, X_TEST_RAW, Y_TRAIN, Y_TEST = load_and_prep_data()
4348
+
4349
+ demo = create_model_building_game_es_app()
4350
+ port = int(os.environ.get("PORT", 8080))
4351
+ demo.launch(share=share, inline=True, debug=debug, height=height, server_port=port)