> 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/endpoint-api.md).

# Endpoint API

## Default function

* [load\_model](/innocuous-book/endpoint/endpoint-api.md#load_model)
* [predict](/innocuous-book/endpoint/endpoint-api.md#predict)
* [predict\_file](/innocuous-book/endpoint/endpoint-api.md#predict_file)
* [on\_train\_completed](/innocuous-book/endpoint/endpoint-api.md#on_train_completed)

### load\_model

load model and checkpoint here

Example:

```python
class Model:
    ...

def load_model():
    model = Model()
    model.load_state_dict(torch.load(modelLoader.path))
    model.to(device)
    model.eval()
    modelLoader.update_model(model)
```

### predict(data)

predict with http post body

| Arguments |                       |
| --------- | --------------------- |
| data      | Dict (http post body) |

Example:

```python
def predict(data):
    model = modelLoader.get_model()
    files = magic.download(data['images'])
    predict = []
    file_name = []
    for file in files:
        img, label = load_image(file)
        result = model(img)
        _, pred = torch.max(result, 1)
        predict.append(int(pred))
        file_name.append(label)
    result = {
        'data': {
            'predict': predict,
            'file_name': file_name
        }
    }
    return result
```

### predict\_file(files)

predict with http post file

| Arguments |                       |
| --------- | --------------------- |
| files     | File (http post file) |

Example:

```python
def predict_file(files):
    model = modelLoader.get_model()
    files = magic.save(files)
    predict = []
    file_name = []
    for file in files:
        img, label = load_image(file)
        result = model(img)
        _, pred = torch.max(result, 1)
        predict.append(int(pred))
        file_name.append(label)
    result = {
        'data': {
            'predict': predict,
            'file_name': file_name
        }
    }
    return result
```

### on\_train\_completed(metric, config)

Will call after the experiment is completed

| Arguments |                                     |
| --------- | ----------------------------------- |
| metric    | Float (this experiment metric)      |
| config    | Dict (this experiment best conf9ig) |

Example:

```python
def on_train_completed(metric, config):
    if metric > pipelineHepler.last_metric:
        modelLoader.save_model()
        load_model()
        pipelineHepler.update_metric(metric)
```
