|
37 | 37 | from lighteval.tasks.lighteval_task import LightevalTask, LightevalTaskConfig
|
38 | 38 | from lighteval.utils.imports import CANNOT_USE_EXTENDED_TASKS_MSG, can_load_extended_tasks
|
39 | 39 |
|
| 40 | +# Import community tasks |
| 41 | +AVAILABLE_COMMUNITY_TASKS_MODULES = [] |
| 42 | +def load_community_tasks(): |
| 43 | + """Dynamically load community tasks, handling errors gracefully.""" |
| 44 | + modules = [] |
| 45 | + try: |
| 46 | + # Community tasks are in the lighteval directory, not under src |
| 47 | + import sys |
| 48 | + import os |
| 49 | + community_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "community_tasks") |
| 50 | + if os.path.exists(community_path): |
| 51 | + sys.path.insert(0, os.path.dirname(community_path)) |
| 52 | + |
| 53 | + # List all python files in community_tasks |
| 54 | + community_files = [f[:-3] for f in os.listdir(community_path) |
| 55 | + if f.endswith('.py') and not f.startswith('_')] |
| 56 | + |
| 57 | + for module_name in community_files: |
| 58 | + try: |
| 59 | + module = importlib.import_module(f"community_tasks.{module_name}") |
| 60 | + if hasattr(module, 'TASKS_TABLE'): |
| 61 | + modules.append(module) |
| 62 | + logger.info(f"Successfully loaded community tasks from {module_name}") |
| 63 | + except Exception as e: |
| 64 | + logger.warning(f"Failed to load community tasks from {module_name}: {e}") |
| 65 | + except Exception as e: |
| 66 | + logger.warning(f"Error loading community tasks directory: {e}") |
| 67 | + |
| 68 | + return modules |
| 69 | + |
40 | 70 |
|
41 | 71 | logger = logging.getLogger(__name__)
|
42 | 72 |
|
@@ -137,6 +167,23 @@ def task_registry(self) -> dict[str, LightevalTaskConfig]:
|
137 | 167 | else:
|
138 | 168 | logger.warning(CANNOT_USE_EXTENDED_TASKS_MSG)
|
139 | 169 |
|
| 170 | + # Load community tasks |
| 171 | + community_modules = load_community_tasks() |
| 172 | + for community_task_module in community_modules: |
| 173 | + custom_tasks_module.append(community_task_module) |
| 174 | + |
| 175 | + # Load multilingual tasks |
| 176 | + MULTILINGUAL_TASKS_AVAILABLE = False |
| 177 | + multilingual_tasks = None |
| 178 | + try: |
| 179 | + import lighteval.tasks.multilingual.tasks as multilingual_tasks |
| 180 | + MULTILINGUAL_TASKS_AVAILABLE = True |
| 181 | + except ImportError as e: |
| 182 | + logger.warning(f"Could not load multilingual tasks: {e}. You may need to install additional dependencies.") |
| 183 | + |
| 184 | + if MULTILINGUAL_TASKS_AVAILABLE and multilingual_tasks is not None: |
| 185 | + custom_tasks_module.append(multilingual_tasks) |
| 186 | + |
140 | 187 | for module in custom_tasks_module:
|
141 | 188 | custom_task_configs.extend(module.TASKS_TABLE)
|
142 | 189 | logger.info(f"Found {len(module.TASKS_TABLE)} custom tasks in {module.__file__}")
|
@@ -285,13 +332,26 @@ def print_all_tasks(self):
|
285 | 332 | Print all the tasks in the task registry.
|
286 | 333 | """
|
287 | 334 | tasks_names = list(self.task_registry.keys())
|
| 335 | + |
| 336 | + # Ensure all default suites are present |
| 337 | + suites_in_registry = {name.split("|")[0] for name in tasks_names} |
| 338 | + for suite in DEFAULT_SUITES: |
| 339 | + if suite not in suites_in_registry: |
| 340 | + # We add a dummy task to make sure the suite is printed |
| 341 | + tasks_names.append(f"{suite}|") |
| 342 | + |
288 | 343 | tasks_names.sort()
|
| 344 | + |
289 | 345 | for suite, g in groupby(tasks_names, lambda x: x.split("|")[0]):
|
290 |
| - tasks_names = list(g) |
291 |
| - tasks_names.sort() |
| 346 | + tasks_in_suite = [name for name in g if name.split("|")[1]] # Filter out dummy tasks |
| 347 | + tasks_in_suite.sort() |
| 348 | + |
292 | 349 | print(f"\n- {suite}:")
|
293 |
| - for task_name in tasks_names: |
294 |
| - print(f" - {task_name}") |
| 350 | + if not tasks_in_suite: |
| 351 | + print(" (no tasks in this suite)") |
| 352 | + else: |
| 353 | + for task_name in tasks_in_suite: |
| 354 | + print(f" - {task_name}") |
295 | 355 |
|
296 | 356 | @staticmethod
|
297 | 357 | def create_custom_tasks_module(custom_tasks: str | Path | ModuleType) -> ModuleType:
|
|
0 commit comments