Fix HF interactive
Browse files
src/ui.py
CHANGED
|
@@ -13,14 +13,6 @@ LABEL_DIS = "👎"
|
|
| 13 |
# --- Session Wrappers ---
|
| 14 |
|
| 15 |
def refresh_wrapper(app):
|
| 16 |
-
"""
|
| 17 |
-
Initializes the session if it's not already created, then runs the refresh.
|
| 18 |
-
Returns:
|
| 19 |
-
1. App instance
|
| 20 |
-
2. Stories List (List[str])
|
| 21 |
-
3. Empty Labels Dict (Dict)
|
| 22 |
-
4. Log text
|
| 23 |
-
"""
|
| 24 |
if app is None or callable(app) or isinstance(app, type):
|
| 25 |
print("Initializing new HackerNewsFineTuner session...")
|
| 26 |
app = HackerNewsFineTuner(AppConfig)
|
|
@@ -35,32 +27,12 @@ def refresh_wrapper(app):
|
|
| 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 |
-
|
| 46 |
-
has_model_tuned = (app is not None) and app.last_hn_dataset
|
| 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 |
|
|
@@ -282,12 +254,18 @@ def build_interface() -> gr.Blocks:
|
|
| 282 |
# ----------------
|
| 283 |
clear_reload_btn.click(
|
| 284 |
fn=lambda: set_interactivity(False), outputs=action_buttons
|
|
|
|
|
|
|
| 285 |
).then(
|
| 286 |
fn=refresh_wrapper,
|
| 287 |
inputs=[session_state],
|
| 288 |
outputs=[session_state, stories_state, labels_state, output]
|
| 289 |
).then(
|
| 290 |
fn=lambda: [gr.update(interactive=True)]*2, outputs=[clear_reload_btn, run_training_btn]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
)
|
| 292 |
|
| 293 |
# Reset Selection Button Logic
|
|
|
|
| 13 |
# --- Session Wrappers ---
|
| 14 |
|
| 15 |
def refresh_wrapper(app):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if app is None or callable(app) or isinstance(app, type):
|
| 17 |
print("Initializing new HackerNewsFineTuner session...")
|
| 18 |
app = HackerNewsFineTuner(AppConfig)
|
|
|
|
| 27 |
return app, choices_list, empty_labels, log_update
|
| 28 |
|
| 29 |
def update_hub_interactive(app, username: Optional[str] = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
is_logged_in = username is not None
|
| 31 |
+
has_model_tuned = app is not None and bool(app.last_hn_dataset)
|
| 32 |
|
| 33 |
+
return gr.update(interactive=is_logged_in), gr.update(interactive=is_logged_in and has_model_tuned)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def on_app_load(app, profile: Optional[gr.OAuthProfile] = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# 1. Initialize/Refresh Session
|
| 37 |
app, stories, labels, text_update = refresh_wrapper(app)
|
| 38 |
|
|
|
|
| 254 |
# ----------------
|
| 255 |
clear_reload_btn.click(
|
| 256 |
fn=lambda: set_interactivity(False), outputs=action_buttons
|
| 257 |
+
).then(
|
| 258 |
+
fn=lambda: gr.update(interactive=False), outputs=push_to_hub_btn
|
| 259 |
).then(
|
| 260 |
fn=refresh_wrapper,
|
| 261 |
inputs=[session_state],
|
| 262 |
outputs=[session_state, stories_state, labels_state, output]
|
| 263 |
).then(
|
| 264 |
fn=lambda: [gr.update(interactive=True)]*2, outputs=[clear_reload_btn, run_training_btn]
|
| 265 |
+
).then(
|
| 266 |
+
fn=update_hub_interactive,
|
| 267 |
+
inputs=[session_state, username_state],
|
| 268 |
+
outputs=[repo_name_input, push_to_hub_btn]
|
| 269 |
)
|
| 270 |
|
| 271 |
# Reset Selection Button Logic
|