Skip to content

Commit 158dd7d

Browse files
authored
Merge pull request #1340 from transformerlab/add/list-datasets-facade
Add an option to access generated datasets via lab facade
2 parents 97879c0 + 3ba73aa commit 158dd7d

4 files changed

Lines changed: 126 additions & 2 deletions

File tree

api/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependencies = [
3434
"soundfile==0.13.1",
3535
"tensorboardX==2.6.2.2",
3636
"timm==1.0.15",
37-
"transformerlab==0.0.78",
37+
"transformerlab==0.0.79",
3838
"transformerlab-inference==0.2.52",
3939
"transformers==4.57.1",
4040
"wandb==0.23.1",

lab-sdk/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "transformerlab"
7-
version = "0.0.78"
7+
version = "0.0.79"
88
description = "Python SDK for Transformer Lab"
99
readme = "README.md"
1010
requires-python = ">=3.10"

lab-sdk/src/lab/lab_facade.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,39 @@ async def async_get_artifact_paths(self) -> list[str]:
15191519
self._ensure_initialized()
15201520
return await self._job.get_artifact_paths() # type: ignore[union-attr]
15211521

1522+
def list_datasets(self) -> list[Dict[str, Any]]:
1523+
"""
1524+
List all local datasets available in the workspace.
1525+
1526+
Returns:
1527+
List of dictionaries containing dataset metadata. Each dictionary includes:
1528+
- dataset_id: The dataset identifier
1529+
- location: Where the dataset is stored (e.g. "local")
1530+
- description: A human-readable description
1531+
- size: The dataset size (-1 if unknown)
1532+
- json_data: Additional dataset metadata
1533+
"""
1534+
return _run_async(Dataset.list_all())
1535+
1536+
def get_dataset(self, dataset_id: str, job_id: Optional[str] = None) -> Dataset:
1537+
"""
1538+
Get a specific local dataset by ID.
1539+
1540+
The lookup is automatically scoped to the current team/organization context.
1541+
1542+
Args:
1543+
dataset_id: The identifier of the dataset to retrieve
1544+
job_id: Optional job ID. If provided, looks up the dataset in the
1545+
job-specific datasets directory instead of the global one.
1546+
1547+
Returns:
1548+
Dataset: A Dataset instance for the specified dataset
1549+
1550+
Raises:
1551+
FileNotFoundError: If the dataset directory doesn't exist
1552+
"""
1553+
return _run_async(Dataset.get(dataset_id, job_id=job_id))
1554+
15221555
def list_models(self) -> list[Dict[str, Any]]:
15231556
"""
15241557
List all local models available in the workspace.

lab-sdk/tests/test_lab_facade.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,97 @@ def test_lab_ensure_initialized(tmp_path, monkeypatch):
811811
pass # Expected
812812

813813

814+
def test_lab_list_datasets(tmp_path, monkeypatch):
815+
_fresh(monkeypatch)
816+
home = tmp_path / ".tfl_home"
817+
ws = tmp_path / ".tfl_ws"
818+
home.mkdir()
819+
ws.mkdir()
820+
monkeypatch.setenv("TFL_HOME_DIR", str(home))
821+
monkeypatch.setenv("TFL_WORKSPACE_DIR", str(ws))
822+
823+
from lab.lab_facade import Lab
824+
from lab.dataset import Dataset
825+
826+
# Create test datasets
827+
ds1 = asyncio.run(Dataset.create("test_dataset_1"))
828+
asyncio.run(ds1.set_metadata(description="First dataset"))
829+
830+
ds2 = asyncio.run(Dataset.create("test_dataset_2"))
831+
asyncio.run(ds2.set_metadata(description="Second dataset"))
832+
833+
lab = Lab()
834+
# list_datasets doesn't require initialization
835+
datasets = lab.list_datasets()
836+
837+
assert len(datasets) >= 2
838+
dataset_ids = [d.get("dataset_id") for d in datasets]
839+
assert "test_dataset_1" in dataset_ids
840+
assert "test_dataset_2" in dataset_ids
841+
842+
843+
def test_lab_list_datasets_empty(tmp_path, monkeypatch):
844+
_fresh(monkeypatch)
845+
home = tmp_path / ".tfl_home"
846+
ws = tmp_path / ".tfl_ws"
847+
home.mkdir()
848+
ws.mkdir()
849+
monkeypatch.setenv("TFL_HOME_DIR", str(home))
850+
monkeypatch.setenv("TFL_WORKSPACE_DIR", str(ws))
851+
852+
from lab.lab_facade import Lab
853+
854+
lab = Lab()
855+
datasets = lab.list_datasets()
856+
857+
assert isinstance(datasets, list)
858+
assert len(datasets) == 0
859+
860+
861+
def test_lab_get_dataset(tmp_path, monkeypatch):
862+
_fresh(monkeypatch)
863+
home = tmp_path / ".tfl_home"
864+
ws = tmp_path / ".tfl_ws"
865+
home.mkdir()
866+
ws.mkdir()
867+
monkeypatch.setenv("TFL_HOME_DIR", str(home))
868+
monkeypatch.setenv("TFL_WORKSPACE_DIR", str(ws))
869+
870+
from lab.lab_facade import Lab
871+
from lab.dataset import Dataset
872+
873+
# Create a test dataset
874+
ds = asyncio.run(Dataset.create("test_dataset_get"))
875+
asyncio.run(ds.set_metadata(description="My Dataset"))
876+
877+
lab = Lab()
878+
# get_dataset doesn't require initialization
879+
retrieved_ds = lab.get_dataset("test_dataset_get")
880+
881+
assert retrieved_ds.id == "test_dataset_get"
882+
metadata = asyncio.run(retrieved_ds.get_metadata())
883+
assert metadata["description"] == "My Dataset"
884+
885+
886+
def test_lab_get_dataset_nonexistent(tmp_path, monkeypatch):
887+
_fresh(monkeypatch)
888+
home = tmp_path / ".tfl_home"
889+
ws = tmp_path / ".tfl_ws"
890+
home.mkdir()
891+
ws.mkdir()
892+
monkeypatch.setenv("TFL_HOME_DIR", str(home))
893+
monkeypatch.setenv("TFL_WORKSPACE_DIR", str(ws))
894+
895+
from lab.lab_facade import Lab
896+
897+
lab = Lab()
898+
try:
899+
lab.get_dataset("nonexistent_dataset")
900+
assert False, "Should have raised FileNotFoundError"
901+
except FileNotFoundError:
902+
pass # Expected
903+
904+
814905
def test_lab_list_models(tmp_path, monkeypatch):
815906
_fresh(monkeypatch)
816907
home = tmp_path / ".tfl_home"

0 commit comments

Comments
 (0)