Fix HF interactive
Browse files
src/ui.py
CHANGED
|
@@ -34,23 +34,44 @@ def refresh_wrapper(app):
|
|
| 34 |
|
| 35 |
return app, choices_list, empty_labels, log_update
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def on_app_load(app, profile: Optional[gr.OAuthProfile] = None):
|
| 38 |
"""
|
| 39 |
Combined wrapper for initial load:
|
| 40 |
1. Initializes/Refreshes App Session
|
| 41 |
-
2.
|
|
|
|
| 42 |
"""
|
| 43 |
-
# 1.
|
| 44 |
app, stories, labels, text_update = refresh_wrapper(app)
|
| 45 |
|
| 46 |
-
# 2.
|
| 47 |
-
|
| 48 |
-
username = profile.username if is_logged_in else None
|
| 49 |
-
|
| 50 |
-
hub_interactive = gr.update(interactive=is_logged_in)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def update_repo_preview(username, repo_name):
|
| 56 |
"""Updates the markdown preview to show 'username/repo_name'."""
|
|
@@ -299,6 +320,10 @@ def build_interface() -> gr.Blocks:
|
|
| 299 |
).then(
|
| 300 |
# Unlock all buttons (including downloads now that we have a model)
|
| 301 |
fn=lambda: set_interactivity(True), outputs=action_buttons
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
)
|
| 303 |
|
| 304 |
# 5. Downloads
|
|
|
|
| 34 |
|
| 35 |
return app, choices_list, empty_labels, log_update
|
| 36 |
|
| 37 |
+
def update_hub_interactive(app, username: Optional[str] = None):
|
| 38 |
+
"""
|
| 39 |
+
Updates the interactivity of Hub components.
|
| 40 |
+
Accepts 'username' (str) to allow reuse by both on_app_load (profile extraction)
|
| 41 |
+
and post-training events (using stored username state).
|
| 42 |
+
"""
|
| 43 |
+
is_logged_in = username is not None
|
| 44 |
+
|
| 45 |
+
# Check if model is ready (app exists and has a dataset generated)
|
| 46 |
+
has_model_tuned = (app is not None) and (getattr(app, 'last_hn_dataset', None) is not None)
|
| 47 |
+
|
| 48 |
+
# Repo input only needs login
|
| 49 |
+
repo_interactive = gr.update(interactive=is_logged_in)
|
| 50 |
+
|
| 51 |
+
# Push button needs Login AND a Ready Model
|
| 52 |
+
can_push = is_logged_in and has_model_tuned
|
| 53 |
+
push_interactive = gr.update(interactive=can_push)
|
| 54 |
+
|
| 55 |
+
return repo_interactive, push_interactive
|
| 56 |
+
|
| 57 |
def on_app_load(app, profile: Optional[gr.OAuthProfile] = None):
|
| 58 |
"""
|
| 59 |
Combined wrapper for initial load:
|
| 60 |
1. Initializes/Refreshes App Session
|
| 61 |
+
2. Extracts Username from Profile
|
| 62 |
+
3. Updates Hub Buttons based on login status
|
| 63 |
"""
|
| 64 |
+
# 1. Initialize/Refresh Session
|
| 65 |
app, stories, labels, text_update = refresh_wrapper(app)
|
| 66 |
|
| 67 |
+
# 2. Extract Username safely
|
| 68 |
+
username = profile.username if profile else None
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# 3. Get UI Updates using the helper
|
| 71 |
+
repo_update, push_update = update_hub_interactive(app, username)
|
| 72 |
+
|
| 73 |
+
# Return 7 items: App state, Data updates (3), Hub updates (2), Username state (1)
|
| 74 |
+
return app, stories, labels, text_update, repo_update, push_update, username
|
| 75 |
|
| 76 |
def update_repo_preview(username, repo_name):
|
| 77 |
"""Updates the markdown preview to show 'username/repo_name'."""
|
|
|
|
| 320 |
).then(
|
| 321 |
# Unlock all buttons (including downloads now that we have a model)
|
| 322 |
fn=lambda: set_interactivity(True), outputs=action_buttons
|
| 323 |
+
).then(
|
| 324 |
+
fn=update_hub_interactive,
|
| 325 |
+
inputs=[session_state, username_state],
|
| 326 |
+
outputs=[repo_name_input, push_to_hub_btn]
|
| 327 |
)
|
| 328 |
|
| 329 |
# 5. Downloads
|