88from typing import Any
99
1010import gradio as gr
11- import huggingface_hub as hf
1211import numpy as np
1312import pandas as pd
1413
15- HfApi = hf .HfApi ()
16-
1714try :
1815 import trackio .utils as utils
1916 from trackio .file_storage import FileStorage
@@ -267,63 +264,13 @@ def toggle_timer(cb_value):
267264 return gr .Timer (active = False )
268265
269266
270- def check_auth (hf_token : str | None ) -> None :
271- if os .getenv ("SYSTEM" ) == "spaces" : # if we are running in Spaces
272- # check auth token passed in
273- if hf_token is None :
274- raise PermissionError (
275- "Expected a HF_TOKEN to be provided when logging to a Space"
276- )
277- who = HfApi .whoami (hf_token )
278- access_token = who ["auth" ]["accessToken" ]
279- owner_name = os .getenv ("SPACE_AUTHOR_NAME" )
280- repo_name = os .getenv ("SPACE_REPO_NAME" )
281- # make sure the token user is either the author of the space,
282- # or is a member of an org that is the author.
283- orgs = [o ["name" ] for o in who ["orgs" ]]
284- if owner_name != who ["name" ] and owner_name not in orgs :
285- raise PermissionError (
286- "Expected the provided hf_token to be the user owner of the space, or be a member of the org owner of the space"
287- )
288- # reject fine-grained tokens without specific repo access
289- if access_token ["role" ] == "fineGrained" :
290- matched = False
291- for item in access_token ["fineGrained" ]["scoped" ]:
292- if (
293- item ["entity" ]["type" ] == "space"
294- and item ["entity" ]["name" ] == f"{ owner_name } /{ repo_name } "
295- and "repo.write" in item ["permissions" ]
296- ):
297- matched = True
298- break
299- if (
300- (
301- item ["entity" ]["type" ] == "user"
302- or item ["entity" ]["type" ] == "org"
303- )
304- and item ["entity" ]["name" ] == owner_name
305- and "repo.write" in item ["permissions" ]
306- ):
307- matched = True
308- break
309- if not matched :
310- raise PermissionError (
311- "Expected the provided hf_token with fine grained permissions to provide write access to the space"
312- )
313- # reject read-only tokens
314- elif access_token ["role" ] != "write" :
315- raise PermissionError (
316- "Expected the provided hf_token to provide write permissions"
317- )
318-
319-
320267def upload_db_to_space (
321268 project : str , uploaded_db : gr .FileData , hf_token : str | None
322269) -> None :
323270 """
324271 Uploads the database of a local Trackio project to a Hugging Face Space.
325272 """
326- check_auth (hf_token )
273+ fns . check_hf_token_has_write_access (hf_token )
327274 db_project_path = SQLiteStorage .get_project_db_path (project )
328275 if os .path .exists (db_project_path ):
329276 raise gr .Error (
@@ -337,7 +284,7 @@ def bulk_upload_media(uploads: list[UploadEntry], hf_token: str | None) -> None:
337284 """
338285 Uploads media files to a Trackio dashboard. Each entry in the list is a tuple of the project, run, and media file to be uploaded.
339286 """
340- check_auth (hf_token )
287+ fns . check_hf_token_has_write_access (hf_token )
341288 for upload in uploads :
342289 media_path = FileStorage .init_project_media_path (
343290 upload ["project" ], upload ["run" ], upload ["step" ]
@@ -357,7 +304,7 @@ def log(
357304 is kept for backwards compatibility for users who are connecting to a newer version of
358305 a Trackio Spaces dashboard with an older version of Trackio installed locally.
359306 """
360- check_auth (hf_token )
307+ fns . check_hf_token_has_write_access (hf_token )
361308 SQLiteStorage .log (project = project , run = run , metrics = metrics , step = step )
362309
363310
@@ -368,7 +315,7 @@ def bulk_log(
368315 """
369316 Logs a list of metrics to a Trackio dashboard. Each entry in the list is a dictionary of the project, run, a dictionary of metrics, and optionally, a step and config.
370317 """
371- check_auth (hf_token )
318+ fns . check_hf_token_has_write_access (hf_token )
372319
373320 logs_by_run = {}
374321 for log_entry in logs :
@@ -627,12 +574,17 @@ def create_media_section(media_by_run: dict[str, dict[str, list[MediaData]]]):
627574
628575 if (writeToken) {
629576 setCookie('trackio_write_token', writeToken, 7);
630-
631- urlParams.delete('write_token');
632- const newUrl = window.location.pathname +
633- (urlParams.toString() ? '?' + urlParams.toString() : '') +
634- window.location.hash;
635- window.history.replaceState({}, document.title, newUrl);
577+
578+ // Only remove write_token from URL if not in iframe
579+ // In iframes, keep it in URL as cookies may be blocked
580+ const inIframe = window.self !== window.top;
581+ if (!inIframe) {
582+ urlParams.delete('write_token');
583+ const newUrl = window.location.pathname +
584+ (urlParams.toString() ? '?' + urlParams.toString() : '') +
585+ window.location.hash;
586+ window.history.replaceState({}, document.title, newUrl);
587+ }
636588 }
637589})();
638590</script>
@@ -968,7 +920,7 @@ def update_dashboard(
968920 master_df = pd .DataFrame ()
969921
970922 if master_df .empty :
971- if space_id := os . environ . get ( "SPACE_ID" ):
923+ if space_id := utils . get_space ( ):
972924 gr .Markdown (INSTRUCTIONS_SPACES .format (space_id ))
973925 else :
974926 gr .Markdown (INSTRUCTIONS_LOCAL )
0 commit comments