# 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)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://innocuous-book.gitbook.io/innocuous-book/endpoint/endpoint-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
