Skip to content

Commit

Permalink
Add ability to load_dataset without a template for simpler usage for …
Browse files Browse the repository at this point in the history
…beginners (#1350)

* Add ability to run without template for simpler api

Signed-off-by: elronbandel <elronbandel@gmail.com>

* Fix to be more genral

Signed-off-by: elronbandel <elronbandel@gmail.com>

* Fix

Signed-off-by: elronbandel <elronbandel@gmail.com>

---------

Signed-off-by: elronbandel <elronbandel@gmail.com>
  • Loading branch information
elronbandel authored Nov 17, 2024
1 parent 88d428e commit 4afed10
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
21 changes: 17 additions & 4 deletions src/unitxt/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def verify(self):

if self.template is None:
raise ValueError(
"You must set in the recipe either `template`, `template_card_index` or `templates`."
"You must set in the recipe either `template`, `template_card_index`."
)

if isinstance(self.template, list):
Expand Down Expand Up @@ -411,9 +411,22 @@ def prepare(self):
assert (
self.template_card_index is None or self.template is None
), f"Specify either template ({self.template}) or template_card_index ({self.template_card_index}) but not both"
assert not (
self.template_card_index is None and self.template is None
), "Specify either template or template_card_index in card"

if self.template_card_index is None and self.template is None:
if self.card is not None:
self.template_card_index = (
0
if isinstance(self.card.templates, list)
else next(iter(self.card.templates.keys()))
)
logger.warning(
"Template was not specified in recipe, using the first template from the card by default."
)
else:
raise ValueError(
"Specify a template or template_card_index, or a card to get a default template from."
)

if self.template_card_index is not None:
try:
self.template = self.card.templates[self.template_card_index]
Expand Down
6 changes: 6 additions & 0 deletions tests/library/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
class TestAPI(UnitxtTestCase):
maxDiff = None

def test_load_dataset_without_template(self):
dataset = load_dataset(
"card=cards.stsb",
)
self.assertEqual(len(dataset["train"]), 282)

def test_load_dataset(self):
dataset = load_dataset(
"card=cards.stsb,template=templates.regression.two_texts.simple,max_train_instances=5,max_validation_instances=5,max_test_instances=5",
Expand Down
7 changes: 0 additions & 7 deletions tests/library/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,6 @@ def test_standard_recipe_with_no_test(self):
self.assertTrue(len(results) > 0)

def test_standard_recipe_with_template_errors(self):
# Check some template was specified
with self.assertRaises(AssertionError) as cm:
StandardRecipeWithIndexes(card="cards.wnli")
self.assertEqual(
str(cm.exception), "Specify either template or template_card_index in card"
)

# Check either template or template index was specified , but not both
with self.assertRaises(AssertionError) as cm:
StandardRecipeWithIndexes(
Expand Down

0 comments on commit 4afed10

Please sign in to comment.