> For the complete documentation index, see [llms.txt](https://innocuous-book.gitbook.io/innocuous-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://innocuous-book.gitbook.io/innocuous-book/endpoint/magic.md).

# Magic

## How to import package

```python
import innocuous.Endpoint as endpoint
```

## innocuous.Endpoint

**Methods**

* [download](/innocuous-book/endpoint/magic.md#download)
* [save](/innocuous-book/endpoint/magic.md#save)
* [ModelLoader](/innocuous-book/endpoint/magic.md#modelloader)
  * [checkpoint\_path](/innocuous-book/endpoint/magic.md#checkpoint_path)
  * [update\_model](/innocuous-book/endpoint/magic.md#update_model-object)
  * [save\_model](/innocuous-book/endpoint/magic.md#save_model)
  * [get\_model](/innocuous-book/endpoint/magic.md#get_model)
  * [save\_config](/innocuous-book/endpoint/magic.md#save_config-object)
  * [get\_config](/innocuous-book/endpoint/magic.md#get_config)
* [PipelineHelper](/innocuous-book/endpoint/magic.md#pipelinehelper)
  * [last\_metric](/innocuous-book/endpoint/magic.md#last_metric)
  * [metric](/innocuous-book/endpoint/magic.md#metric)
  * [update\_metric](/innocuous-book/endpoint/magic.md#update_metric-metric)
* [FileHelper](/innocuous-book/endpoint/magic.md#filehelper)
  * [get](/innocuous-book/endpoint/magic.md#get-source)
  * [save](/innocuous-book/endpoint/magic.md#save-source-destination)
  * [import\_package](/innocuous-book/endpoint/magic.md#import_package-path)

### download

download predict files

```python
endpoint.download(urls)
```

| Arguments |             |
| --------- | ----------- |
| urls      | List (urls) |

| Result |                        |
| ------ | ---------------------- |
|        | List (local file path) |

Example:

```python
def predict(data):
    ...
    files = endpoint.download(data['images'])
    ...
```

### save

save predict files

```python
endpoint.save(files)
```

| Arguments |             |
| --------- | ----------- |
| files     | List (file) |

| Result |                        |
| ------ | ---------------------- |
|        | List (local file path) |

Example:

```python
def predict_file(files):
    files = endpoint.save(files)
    ...
```

## ModelLoader

```python
modelLoader = endpoint.ModelLoader()
```

### checkpoint\_path

get checkpoint path from Web setting

| Result |                                |
| ------ | ------------------------------ |
|        | String (local checkpoint path) |

Example:

```python
path = modelLoader.checkpoint_path
```

### update\_model(**object**)

update model for model loader

| Arguments |                              |
| --------- | ---------------------------- |
| object    | Object (any object of model) |

Example:

```python
modelLoader.update_model(model)
```

### save\_model()

download new model to local

Example:

```python
modelLoader.save_model()
```

### get\_model()

get model from modelloader

| Result |                              |
| ------ | ---------------------------- |
|        | Object (any object of model) |

Example:

```python
model = modelLoader.get_model()
```

### save\_config(**object**)

save config

| Arguments |                               |
| --------- | ----------------------------- |
| object    | Object (any object of config) |

Example:

```python
modelLoader.save_config(model)
```

### get\_config()

get config

| Result |                               |
| ------ | ----------------------------- |
|        | Object (any object of config) |

Example:

```python
config = modelLoader.get_config()
```

### ModelLoader Example

```python
# Keras
def load_model():
    model = keras.models.load_model(modelLoader.path)
    modelLoader.update_model(model)   # update model

# Pytorch
def load_model():
    model = Model()
    model.load_state_dict(torch.load(modelLoader.path))
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model.to(device)
    model.eval()
    modelLoader.update_model(model)   # update model

# Common
def predict(data):
    model = modelLoader.get_model()   # get model
    config = modelLoader.get_config() # get config
    ...

# Common
def on_train_completed(metric, config, new_model_path):
    ...
    modelLoader.save_model()           # Save model
    modelLoader.save_config(config)    # Save config
    ...
```

## PipelineHelper

```python
pipelineHelper = endpoint.PipelineHelper()
```

### last\_metric&#x20;

get last metric

Example:

```python
last_metric = pipelineHelper.last_metric
```

### metric

get metric list or oneResult

| Result |                   |
| ------ | ----------------- |
|        | List (all mtrice) |

Example:

```python
all_metric = pipelineHelper.metric
metric_1 = pipelineHelper.metric[1]
metric_2 = pipelineHelper.metric(2)
```

### update\_metric(metric)

update metric

| Arguments |       |
| --------- | ----- |
| metric    | float |

Example:

```python
pipelineHelper.update_metric(1.2345)
```

### PipelineHelper Example

```python
def on_train_completed(metric, config, new_model_path):
    if metric > pipelineHelper.last_metric:     # if new metirc better last
        pipelineHelper.update_metric(metric)    # update now metric
    print(pipelineHelper.metric)                # show all metric e.g. [0.1, 0.2]
    print(pipelineHelper.last_metric)           # show last metric e.g. 0.2
```

## FileHelper

```python
fileHelper = endpoint.FileHelper()
```

### get(source)

download file from s3 to local

| Arguments |                           |
| --------- | ------------------------- |
| source    | String (remote file path) |

| Result |                          |
| ------ | ------------------------ |
|        | String (local file path) |

Example:

```python
local_path = fileHelper.get("data://xxx/ooo/config.json")
```

### save(source, destination)

save file from local to s3

| Arguments   |                           |
| ----------- | ------------------------- |
| source      | String (local file path)  |
| destination | String (remote file path) |

Example:

```python
fileHelper.save("local_file_path.json", "data://xxx/ooo/config.json")
```

### import\_package(path)

import module from path

| Arguments |                             |
| --------- | --------------------------- |
| path      | String (local package path) |

| Result |        |
| ------ | ------ |
|        | Module |

Example:

```python
md = fileHelper.import_package("local/path/model.py")
```
