Skip to content

Commit

Permalink
Minor documentation changes
Browse files Browse the repository at this point in the history
Signed-off-by: mikelam-us-aixplain <[email protected]>
  • Loading branch information
mikelam-us-aixplain committed Sep 29, 2023
1 parent 1af106b commit 9424e9c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 6 deletions.
133 changes: 133 additions & 0 deletions docs/user/model_setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# How to develop an aiXplain hosted model?

## Model development

The aixplain-models package organizes your model's code in a standardized format in order to deploy these models on aixplain model hosting instances. The following description covers how to organize your aiXplain hosted model.

### Model directory structure

The directory structure needs to be following:
```
src
│ model.py
│ bash.sh [Optional]
│ requirements.txt [Optional]
| Addtional files required by your code [Optional]
```

### The model artefact directory

The hosted model might depend on files for loading parameters, configurations or other model assets. Create a model directory having the same name as the value provided in `ASSET_URI` and place all your dependant model assets in this directory.

Note:
1. The contents of this directory would be accessed or loaded by the model class' load function.
2. The environment variable `ASSET_URI` defaults to the value `asset`.

### Implementing the model.py file

Each hosted model needs to be an instance of a function based aiXplain model. If the model that your are building is a translation model, the model class implementation should inherit the TranslationModel class interface as shown below.

```
class ExampleTranslationModel(TranslationModel):
```

To implement the model interface, define the following functions:

#### Load function

Implement the load function to load all model artefacts from the model directory specified in `ASSET_URI`. The model artefacts loaded here can be used by the model during prediction time, i.e. executing run_model().
Set the value self.ready as 'True' to indicate that loading has successfully executed.

```
def load(self):
model_path = AssetResolver.resolve_path()
if not os.path.exists(model_path):
raise ValueError('Model not found')
self.model = pickle.load(os.path.join(model_path, 'model.pkl'))
self.ready = True
```

#### Run model function

The run model function should contain the business logic to obtain a prediction from the loaded model.

Input:
The input to the run model function is a dictionary. This dictionary has a key "instances" having values in a list containing AI function based subclass of APIInput values, for example TranslationInput.

Output:
The output to the run model function is a dictionary. This dictionary has a key "predictions" having values in a list containing AI function based subclass of APIOutput values, for example TranslationOutput.
The output is expected to return the predictions from the model in the same order as the input instances were received.

```
def run_model(self, api_input: Dict[str, List[TranslationInput]]) -> Dict[str, List[TranslationOutput]]:
src_text = self.parse_inputs(api_input["instances"])
translated = self.model.generate(
**self.tokenizer(
src_text, return_tensors="pt", padding=True
)
)
predictions = []
for t in translated:
data = self.tokenizer.decode(t, skip_special_tokens=True)
details = TextSegmentDetails(text=data)
output_dict = {
"data": data,
"details": details
}
translation_output = TranslationOutput(**output_dict)
predictions.append(translation_output)
predict_output = {"predictions": predictions}
return predict_output
```


### The system and Python requirements files

The bash.sh file:
This file implementation should include installing any system dependencies using bash commands.

The requirements.txt file:
Include all python packages that you need to run the model by extracting the requirements using the command below

```
pip freeze >> requirements.txt
```
### Testing the model locally

Run your model with the following command:
```
ASSET_DIR=<path/to/model_artefacts_dir> ASSET_URI=<asset_uri> python -m model
```

Make an inference call:

```
ASSET_URI=<asset_uri>
curl -v -H http://localhost:8080/v1/models/$ASSET_URI:predict -d '{"instances": [{"supplier": <supplier>, "function": <function>, "data": <data>}]}'
```

The input parameter in request above needs to be modified according to the target model's function input. Refer to the [function input definition documentation.](/src/aixplain_models/schemas/function_input.py)

### Dockerfile
Create an image using the following sample Dockerfile. Add features as needed:
```Dockerfile
FROM python:3.8.10

RUN mkdir /code
WORKDIR /code
COPY . /code/

RUN pip install -r --no-cache-dir requirements.txt

RUN chmod +x /code/bash.sh
RUN ./bash.sh

CMD python -m model
```

### The environment variables

- `ASSET_DIR`: The relative or absolute path of the model artefacts directory (ASSET_URI) on your system. This defaults to current directory.
- `ASSET_URI`: The name of the model artefacts directory. The default name is `asset`.
25 changes: 23 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ namespaces = true

[project]
name = "model-interfaces"
version = "0.0.1rc1"
version = "0.0.1rc2"
description = "A package specifying the model interfaces supported by aiXplain"
license = { text = "Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0" }
license = { text = "Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0" }
dependencies = [
"kserve>=0.10.0",
"multiprocess==0.70.14",
"protobuf>=3.19.4",
"pydantic>=1.9.1",
"pydub>=0.25.1",
"requests>=2.28.1",
"tornado>=6.2",
"validators>=0.20.0"
]

[project.optional-dependencies]
test = [
"pytest>=7.1.2",
"sentencepiece>=0.1.96",
"torch>=1.13.1",
"transformers>=4.21.1"
]

[project.urls]
Homepage = "https://github.com/aixplain/model-interfaces"
8 changes: 4 additions & 4 deletions setup.py → setup_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
about = {}
here = os.path.abspath(os.path.dirname(__file__))

# with open(os.path.join(here, "aixplain", "model_interfaces", "__version__.py"), "r") as f:
# exec(f.read(), about)
with open(os.path.join(here, "aixplain", "model_interfaces", "__version__.py"), "r") as f:
exec(f.read(), about)

with open("README.md", "r") as f:
readme = f.read()

setup(
name="model_interfaces",
# version=about["__version__"],
version=about["__version__"],
description=about["__description__"],
long_description=readme,
long_description_content_type="text/markdown",
Expand Down Expand Up @@ -107,6 +107,6 @@
},
project_urls={
"Documentation": "",
"Source": "https://github.com/aixplain/aixplain-models"
"Source": "https://github.com/aixplain/model-interfaces"
},
)

0 comments on commit 9424e9c

Please sign in to comment.