Repository: gradsflow/gradsflow-automl (language: python)

Issue:
Automatic Task Selection
#### Is your feature request related to a problem? Please describe.
Create Tasks directly from `AutoClassifier` instead of explicitly calling `AutoImageClassification` or `AutoTextSummarization`

#### Describe the solution you'd like

```python
model=AutoClassification(datamodule, data_type="image")  # expected `data_type`-> image, text, infer
model.hp_tune()
```



Discussion / hints:
Can we have it like:

```python
model=AutoClassification(datamodule, task="summasrisation")  # expected `data_type`-> image, text, infer
model.hp_tune()
```
@gagan3012 yes, but we already have something like `from gradsflow import AutoSummarization` and the summarization task doesn't align with the classification task 😄 

How about -

```python
autotask = AutoTask(datamodule, task='summarization')
```

or 
```python
# way-1
autotask = AutoTask(datamodule, data_type='infer')

# way-2
autotask = AutoTask(datamodule, data_type='text')
```

For way-1, we can auto infer the task type.

Let me know your thoughts on this.

Hi, 
I think the 
```python
autotask = AutoTask(datamodule, task='summarization')
```
is better
cool! would you like to create a PR for this?

Fix patch:
diff --git a/gradsflow/autotasks/__init__.py b/gradsflow/autotasks/__init__.py
index 038c3feb..c57e6077 100644
--- a/gradsflow/autotasks/__init__.py
+++ b/gradsflow/autotasks/__init__.py
@@ -15,3 +15,4 @@
 from .autoclassification.image import AutoImageClassifier
 from .autoclassification.text import AutoTextClassifier
 from .autosummarization import AutoSummarization
+from .autotasks import autotasks
diff --git a/gradsflow/autotasks/autotasks.py b/gradsflow/autotasks/autotasks.py
new file mode 100644
index 00000000..63f478de
--- /dev/null
+++ b/gradsflow/autotasks/autotasks.py
@@ -0,0 +1,77 @@
+#  Copyright (c) 2021 GradsFlow. All rights reserved.
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+from typing import Optional, Union, List, Dict
+from flash.core.data.data_module import DataModule
+
+from .autoclassification.image import AutoImageClassifier
+from .autoclassification.text import AutoTextClassifier
+from .autosummarization import AutoSummarization
+
+SUPPORTED_TASKS = {
+    "image": AutoImageClassifier,
+    "text": AutoTextClassifier,
+    "summarsization": AutoSummarization
+}
+
+
+def autotasks(
+        task,
+        datamodule: DataModule,
+        max_epochs: int = 10,
+        max_steps: int = 10,
+        n_trials: int = 100,
+        optimization_metric: Optional[str] = None,
+        suggested_backbones: Union[List, str, None] = None,
+        suggested_conf: Optional[dict] = None,
+        tune_confs: Optional[Dict] = None,
+        timeout: int = 600,
+        prune: bool = True,
+):
+    """
+
+    Args:
+        task:
+        datamodule [DataModule]: PL Lightning DataModule with `num_classes` property.
+        max_epochs [int]: default=10.
+        n_trials [int]: default=100.
+        optimization_metric [Optional[str]]: defaults None
+        suggested_backbones Union[List, str, None]: defaults None
+        suggested_conf [Optional[dict] = None]: This sets Trial suggestions for optimizer,
+            learning rate, and all the hyperparameters.
+        timeout [int]: Hyperparameter search will stop after timeout.
+
+    Returns:
+
+    """
+    if task not in SUPPORTED_TASKS:
+        raise KeyError(
+            "Unknown task {}, available tasks are {}".format(
+                task, list(SUPPORTED_TASKS.keys())
+            )
+        )
+
+    targeted_task = SUPPORTED_TASKS[task]
+
+    return targeted_task(
+        datamodule=datamodule,
+        max_epochs=max_epochs,
+        max_steps=max_steps,
+        n_trials=n_trials,
+        optimization_metric=optimization_metric,
+        suggested_backbones=suggested_backbones,
+        suggested_conf=suggested_conf,
+        tune_confs=tune_confs,
+        timeout=timeout,
+        prune=prune
+    )


Test patch:
diff --git a/tests/tasks/test_auto.py b/tests/tasks/test_auto.py
new file mode 100644
index 00000000..1dd357d7
--- /dev/null
+++ b/tests/tasks/test_auto.py
@@ -0,0 +1,44 @@
+import warnings
+from pathlib import Path
+
+import pytest
+import torch
+from flash.image import ImageClassificationData, ImageClassifier
+
+from gradsflow.autotasks import autotasks
+
+warnings.filterwarnings("ignore")
+
+data_dir = Path.cwd()
+datamodule = ImageClassificationData.from_folders(
+    train_folder=f"{data_dir}/data/hymenoptera_data/train/",
+    val_folder=f"{data_dir}/data/hymenoptera_data/val/",
+)
+
+
+def test_build_model():
+    model = autotasks(
+        task="image",
+        datamodule=datamodule,
+        max_epochs=1,
+        timeout=5,
+        suggested_backbones="ssl_resnet18",
+        n_trials=1,
+    )
+    kwargs = {"backbone": "ssl_resnet18", "optimizer": "adam", "lr": 1e-1}
+    model.model = model.build_model(kwargs)
+    assert isinstance(model.model, ImageClassifier)
+
+
+def test_hp_tune():
+    model = autotasks(
+        task="image",
+        datamodule=datamodule,
+        max_epochs=1,
+        max_steps=2,
+        timeout=30,
+        suggested_backbones="ssl_resnet18",
+        optimization_metric="val_accuracy",
+        n_trials=1,
+    )
+    model.hp_tune(name="my-experiment", mode="max", gpu=0, trainer_config={"fast_dev_run": True})