|
1 | 1 | import importlib.metadata |
2 | 2 | import io |
3 | 3 | import os |
| 4 | +import threading |
4 | 5 | import time |
5 | 6 | from importlib.resources import files |
6 | 7 | from pathlib import Path |
|
13 | 14 |
|
14 | 15 | import trackio |
15 | 16 | from trackio.sqlite_storage import SQLiteStorage |
16 | | -from trackio.utils import preprocess_space_and_dataset_ids |
| 17 | +from trackio.utils import get_or_create_project_hash, preprocess_space_and_dataset_ids |
17 | 18 |
|
18 | 19 | SPACE_HOST_URL = "https://{user_name}-{space_name}.hf.space/" |
19 | 20 | SPACE_URL = "https://huggingface.co/spaces/{space_id}" |
@@ -294,25 +295,52 @@ def upload_db_to_space(project: str, space_id: str, force: bool = False) -> None |
294 | 295 |
|
295 | 296 |
|
296 | 297 | def sync( |
297 | | - project: str, space_id: str, private: bool | None = None, force: bool = False |
298 | | -) -> None: |
| 298 | + project: str, |
| 299 | + space_id: str | None = None, |
| 300 | + private: bool | None = None, |
| 301 | + force: bool = False, |
| 302 | + run_in_background: bool = False, |
| 303 | +) -> str: |
299 | 304 | """ |
300 | 305 | Syncs a local Trackio project's database to a Hugging Face Space. |
301 | 306 | If the Space does not exist, it will be created. |
302 | 307 |
|
303 | 308 | Args: |
304 | 309 | project (`str`): The name of the project to upload. |
305 | | - space_id (`str`): The ID of the Space to upload to (e.g., `"username/space_id"`). |
| 310 | + space_id (`str`, *optional*): The ID of the Space to upload to (e.g., `"username/space_id"`). |
| 311 | + If not provided, a random space_id (e.g. "username/project-2ac3z2aA") will be used. |
306 | 312 | private (`bool`, *optional*): |
307 | 313 | Whether to make the Space private. If None (default), the repo will be |
308 | 314 | public unless the organization's default is private. This value is ignored |
309 | 315 | if the repo already exists. |
310 | 316 | force (`bool`, *optional*, defaults to `False`): |
311 | 317 | If `True`, overwrite the existing database without prompting for confirmation. |
312 | 318 | If `False`, prompt the user before overwriting an existing database. |
| 319 | + run_in_background (`bool`, *optional*, defaults to `False`): |
| 320 | + If `True`, the Space creation and database upload will be run in a background thread. |
| 321 | + If `False`, all the steps will be run synchronously. |
| 322 | + Returns: |
| 323 | + `str`: The Space ID of the synced project. |
313 | 324 | """ |
| 325 | + if space_id is None: |
| 326 | + space_id = f"{project}-{get_or_create_project_hash(project)}" |
314 | 327 | space_id, _ = preprocess_space_and_dataset_ids(space_id, None) |
315 | | - create_space_if_not_exists(space_id, private=private) |
316 | | - wait_until_space_exists(space_id) |
317 | | - upload_db_to_space(project, space_id, force=force) |
318 | | - print(f"Synced successfully to space: {SPACE_URL.format(space_id=space_id)}") |
| 328 | + |
| 329 | + def space_creation_and_upload( |
| 330 | + space_id: str, private: bool | None = None, force: bool = False |
| 331 | + ): |
| 332 | + print( |
| 333 | + f"* Syncing local Trackio project to: {SPACE_URL.format(space_id=space_id)} (please wait...)" |
| 334 | + ) |
| 335 | + create_space_if_not_exists(space_id, private=private) |
| 336 | + wait_until_space_exists(space_id) |
| 337 | + upload_db_to_space(project, space_id, force=force) |
| 338 | + print(f"* Synced successfully to space: {SPACE_URL.format(space_id=space_id)}") |
| 339 | + |
| 340 | + if run_in_background: |
| 341 | + threading.Thread( |
| 342 | + target=space_creation_and_upload, args=(space_id, private, force) |
| 343 | + ).start() |
| 344 | + else: |
| 345 | + space_creation_and_upload(space_id, private, force) |
| 346 | + return space_id |
0 commit comments