feat: publishing infernet-container-starter v0.2.0
This commit is contained in:
25
projects/torch-iris/container/Dockerfile
Normal file
25
projects/torch-iris/container/Dockerfile
Normal file
@ -0,0 +1,25 @@
|
||||
FROM python:3.11-slim as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PIP_NO_CACHE_DIR 1
|
||||
ENV RUNTIME docker
|
||||
ENV PYTHONPATH src
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y git curl
|
||||
|
||||
# install uv
|
||||
ADD --chmod=755 https://astral.sh/uv/install.sh /install.sh
|
||||
RUN /install.sh && rm /install.sh
|
||||
|
||||
COPY src/requirements.txt .
|
||||
|
||||
RUN /root/.cargo/bin/uv pip install --system --no-cache -r requirements.txt
|
||||
|
||||
COPY src src
|
||||
|
||||
ENTRYPOINT ["hypercorn", "app:create_app()"]
|
||||
CMD ["-b", "0.0.0.0:3000"]
|
17
projects/torch-iris/container/Makefile
Normal file
17
projects/torch-iris/container/Makefile
Normal file
@ -0,0 +1,17 @@
|
||||
DOCKER_ORG := ritualnetwork
|
||||
EXAMPLE_NAME := torch-iris
|
||||
TAG := $(DOCKER_ORG)/example-$(EXAMPLE_NAME)-infernet:latest
|
||||
|
||||
.phony: build run
|
||||
|
||||
build:
|
||||
@docker build -t $(TAG) .
|
||||
|
||||
run:
|
||||
docker run -p 3000:3000 $(TAG)
|
||||
|
||||
# You may need to set up a docker builder, to do so run:
|
||||
# docker buildx create --name mybuilder --bootstrap --use
|
||||
# refer to https://docs.docker.com/build/building/multi-platform/#building-multi-platform-images for more info
|
||||
build-multiplatform:
|
||||
docker buildx build --platform linux/amd64,linux/arm64 -t $(TAG) --push .
|
110
projects/torch-iris/container/README.md
Normal file
110
projects/torch-iris/container/README.md
Normal file
@ -0,0 +1,110 @@
|
||||
# Iris Classification via Torch
|
||||
|
||||
This example uses a pre-trained model to classify iris flowers. The code for the model
|
||||
is located at
|
||||
our [simple-ml-models](https://github.com/ritual-net/simple-ml-models/tree/main/iris_classification)
|
||||
repository.
|
||||
|
||||
## Overview
|
||||
|
||||
We're making use of
|
||||
the [TorchInferenceWorkflow](https://github.com/ritual-net/infernet-ml-internal/blob/main/src/ml/workflows/inference/torch_inference_workflow.py)
|
||||
class to run the model. This is one of many workflows that we currently support in our
|
||||
[infernet-ml](https://github.com/ritual-net/infernet-ml-internal). Consult the library's
|
||||
documentation for more info on workflows that
|
||||
are supported.
|
||||
|
||||
## Building & Running the Container in Isolation
|
||||
|
||||
Note that this container is meant to be started by the infernet-node. For development &
|
||||
testing purposes, you can run the container in isolation using the following commands.
|
||||
|
||||
### Building the Container
|
||||
|
||||
Simply run the following command to build the container.
|
||||
|
||||
```bash
|
||||
make build
|
||||
```
|
||||
|
||||
Consult the [Makefile](./Makefile) for the build command.
|
||||
|
||||
### Running the Container
|
||||
|
||||
To run the container, you can use the following command:
|
||||
|
||||
```bash
|
||||
make run
|
||||
```
|
||||
|
||||
## Testing the Container
|
||||
|
||||
Run the following command to perform an inference:
|
||||
|
||||
```bash
|
||||
curl -X POST "http://127.0.0.1:3000/service_output" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"source":1, "data": {"input": [[1.0380048, 0.5586108, 1.1037828, 1.712096]]}}'
|
||||
```
|
||||
|
||||
#### Note Regarding the Input
|
||||
|
||||
The inputs provided above correspond to an iris flower with the following
|
||||
characteristics. Refer to the
|
||||
|
||||
1. Sepal Length: `5.5cm`
|
||||
2. Sepal Width: `2.4cm`
|
||||
3. Petal Length: `3.8cm`
|
||||
4. Petal Width: `1.1cm`
|
||||
|
||||
Putting this input into a vector and scaling it, we get the following scaled input:
|
||||
|
||||
```python
|
||||
[1.0380048, 0.5586108, 1.1037828, 1.712096]
|
||||
```
|
||||
|
||||
Refer
|
||||
to [this function in the model's repository](https://github.com/ritual-net/simple-ml-models/blob/03ebc6fb15d33efe20b7782505b1a65ce3975222/iris_classification/iris_inference_pytorch.py#L13)
|
||||
for more information on how the input is scaled.
|
||||
|
||||
For more context on the Iris dataset, refer to
|
||||
the [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets/iris).
|
||||
|
||||
### Output
|
||||
|
||||
By running the above command, you should get a response similar to the following:
|
||||
|
||||
```json
|
||||
{
|
||||
"input_data": [
|
||||
[
|
||||
1.0380048,
|
||||
0.5586108,
|
||||
1.1037828,
|
||||
1.712096
|
||||
]
|
||||
],
|
||||
"input_shapes": [
|
||||
[
|
||||
4
|
||||
]
|
||||
],
|
||||
"output_data": [
|
||||
[
|
||||
0.0016699483385309577,
|
||||
0.021144982427358627,
|
||||
0.977185070514679
|
||||
]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
The `output_data` corresponds to the model's prediction for each of the classes:
|
||||
|
||||
```python
|
||||
['setosa', 'versicolor', 'virginica']
|
||||
```
|
||||
|
||||
In this case, the model predicts that the input corresponds to the class `virginica`
|
||||
with
|
||||
a probability of `0.977185070514679` (97.7%).
|
50
projects/torch-iris/container/config.json
Normal file
50
projects/torch-iris/container/config.json
Normal file
@ -0,0 +1,50 @@
|
||||
{
|
||||
"log_path": "infernet_node.log",
|
||||
"server": {
|
||||
"port": 4000
|
||||
},
|
||||
"chain": {
|
||||
"enabled": true,
|
||||
"trail_head_blocks": 0,
|
||||
"rpc_url": "http://host.docker.internal:8545",
|
||||
"coordinator_address": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
|
||||
"wallet": {
|
||||
"max_gas_limit": 4000000,
|
||||
"private_key": "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
|
||||
}
|
||||
},
|
||||
"startup_wait": 1.0,
|
||||
"docker": {
|
||||
"username": "your-username",
|
||||
"password": ""
|
||||
},
|
||||
"redis": {
|
||||
"host": "redis",
|
||||
"port": 6379
|
||||
},
|
||||
"forward_stats": true,
|
||||
"containers": [
|
||||
{
|
||||
"id": "torch-iris",
|
||||
"image": "ritualnetwork/example-torch-iris-infernet:latest",
|
||||
"external": true,
|
||||
"port": "3000",
|
||||
"allowed_delegate_addresses": [],
|
||||
"allowed_addresses": [],
|
||||
"allowed_ips": [],
|
||||
"command": "--bind=0.0.0.0:3000 --workers=2",
|
||||
"env": {}
|
||||
},
|
||||
{
|
||||
"id": "anvil-node",
|
||||
"image": "ritualnetwork/infernet-anvil:0.0.0",
|
||||
"external": true,
|
||||
"port": "8545",
|
||||
"allowed_delegate_addresses": [],
|
||||
"allowed_addresses": [],
|
||||
"allowed_ips": [],
|
||||
"command": "",
|
||||
"env": {}
|
||||
}
|
||||
]
|
||||
}
|
52
projects/torch-iris/container/scripts/sample_endpoints.py
Normal file
52
projects/torch-iris/container/scripts/sample_endpoints.py
Normal file
@ -0,0 +1,52 @@
|
||||
import asyncio
|
||||
|
||||
import aiohttp
|
||||
from eth_abi import encode, decode # type: ignore
|
||||
|
||||
|
||||
async def ping(session: aiohttp.ClientSession) -> None:
|
||||
async with session.get("http://127.0.0.1:3000/") as response:
|
||||
print(await response.text())
|
||||
|
||||
|
||||
async def post_directly_web2(session: aiohttp.ClientSession) -> None:
|
||||
async with session.post(
|
||||
"http://127.0.0.1:3000/service_output",
|
||||
json={
|
||||
"source": 1,
|
||||
"data": {"input": [[1.0380048, 0.5586108, 1.1037828, 1.712096]]},
|
||||
},
|
||||
) as response:
|
||||
print(await response.json())
|
||||
|
||||
|
||||
async def post_directly_web3(session: aiohttp.ClientSession) -> None:
|
||||
async with session.post(
|
||||
"http://127.0.0.1:3000/service_output",
|
||||
json={
|
||||
"source": 0,
|
||||
"data": encode(
|
||||
["uint256[]"], [[1_038_004, 558_610, 1_103_782, 1_712_096]]
|
||||
).hex(),
|
||||
},
|
||||
) as response:
|
||||
print(await response.text())
|
||||
result = await response.json()
|
||||
output = result["raw_output"]
|
||||
result = decode(["uint256[]"], bytes.fromhex(output))[0]
|
||||
print(f"result: {result}")
|
||||
|
||||
|
||||
# async main
|
||||
async def main(session: aiohttp.ClientSession) -> None:
|
||||
await post_directly_web3(session)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# run main async
|
||||
|
||||
async def provide_session() -> None:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
await main(session)
|
||||
|
||||
asyncio.run(provide_session())
|
110
projects/torch-iris/container/src/app.py
Normal file
110
projects/torch-iris/container/src/app.py
Normal file
@ -0,0 +1,110 @@
|
||||
import logging
|
||||
from typing import Any, cast, List
|
||||
|
||||
from eth_abi import decode, encode # type: ignore
|
||||
from infernet_ml.utils.model_loader import ModelSource
|
||||
from infernet_ml.utils.service_models import InfernetInput, InfernetInputSource
|
||||
from infernet_ml.workflows.inference.torch_inference_workflow import (
|
||||
TorchInferenceWorkflow,
|
||||
)
|
||||
from quart import Quart, request
|
||||
|
||||
# Note: the IrisClassificationModel needs to be imported in this file for it to exist
|
||||
# in the classpath. This is because pytorch requires the model to be in the classpath.
|
||||
# Simply downloading the weights and model from the hub is not enough.
|
||||
from iris_classification_model import IrisClassificationModel
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def create_app() -> Quart:
|
||||
app = Quart(__name__)
|
||||
# we are downloading the model from the hub.
|
||||
# model repo is located at: https://huggingface.co/Ritual-Net/iris-dataset
|
||||
model_source = ModelSource.HUGGINGFACE_HUB
|
||||
model_args = {"repo_id": "Ritual-Net/iris-dataset", "filename": "iris.torch"}
|
||||
|
||||
workflow = TorchInferenceWorkflow(model_source=model_source, model_args=model_args)
|
||||
workflow.setup()
|
||||
|
||||
@app.route("/")
|
||||
def index() -> str:
|
||||
"""
|
||||
Utility endpoint to check if the service is running.
|
||||
"""
|
||||
return (
|
||||
f"Torch Iris Classifier Example Program: {IrisClassificationModel.__name__}"
|
||||
)
|
||||
|
||||
@app.route("/service_output", methods=["POST"])
|
||||
async def inference() -> dict[str, Any]:
|
||||
req_data = await request.get_json()
|
||||
"""
|
||||
InfernetInput has the format:
|
||||
source: (0 on-chain, 1 off-chain)
|
||||
data: dict[str, Any]
|
||||
"""
|
||||
infernet_input: InfernetInput = InfernetInput(**req_data)
|
||||
|
||||
if infernet_input.source == InfernetInputSource.OFFCHAIN:
|
||||
web2_input = cast(dict[str, Any], infernet_input.data)
|
||||
values = cast(List[List[float]], web2_input["input"])
|
||||
else:
|
||||
# On-chain requests are sent as a generalized hex-string which we will
|
||||
# decode to the appropriate format.
|
||||
web3_input: List[int] = decode(
|
||||
["uint256[]"], bytes.fromhex(cast(str, infernet_input.data))
|
||||
)[0]
|
||||
values = [[float(v) / 1e6 for v in web3_input]]
|
||||
|
||||
"""
|
||||
The input to the torch inference workflow needs to conform to this format:
|
||||
|
||||
{
|
||||
"dtype": str,
|
||||
"values": list[Any]
|
||||
}
|
||||
|
||||
For more information refer to:
|
||||
https://docs.ritual.net/ml-workflows/inference-workflows/torch_inference_workflow
|
||||
|
||||
"""
|
||||
inference_result = workflow.inference({"dtype": "float", "values": values})
|
||||
|
||||
result = [o.detach().numpy().reshape([-1]).tolist() for o in inference_result]
|
||||
|
||||
if infernet_input.source == InfernetInputSource.OFFCHAIN:
|
||||
"""
|
||||
In case of an off-chain request, the result is returned as is.
|
||||
"""
|
||||
return {"result": result}
|
||||
else:
|
||||
"""
|
||||
In case of an on-chain request, the result is returned in the format:
|
||||
{
|
||||
"raw_input": str,
|
||||
"processed_input": str,
|
||||
"raw_output": str,
|
||||
"processed_output": str,
|
||||
"proof": str,
|
||||
}
|
||||
refer to: https://docs.ritual.net/infernet/node/containers for more info.
|
||||
"""
|
||||
predictions = cast(List[List[float]], result)
|
||||
predictions_normalized = [int(p * 1e6) for p in predictions[0]]
|
||||
return {
|
||||
"raw_input": "",
|
||||
"processed_input": "",
|
||||
"raw_output": encode(["uint256[]"], [predictions_normalized]).hex(),
|
||||
"processed_output": "",
|
||||
"proof": "",
|
||||
}
|
||||
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""
|
||||
Utility to run the app locally. For development purposes only.
|
||||
"""
|
||||
create_app().run(port=3000)
|
@ -0,0 +1,23 @@
|
||||
import torch.nn as nn
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
"""
|
||||
The IrisClassificationModel torch module. This is the computation graph that was used to
|
||||
train the model. Refer to:
|
||||
https://github.com/ritual-net/simple-ml-models/tree/main/iris_classification
|
||||
"""
|
||||
|
||||
|
||||
class IrisClassificationModel(nn.Module):
|
||||
def __init__(self, input_dim: int) -> None:
|
||||
super(IrisClassificationModel, self).__init__()
|
||||
self.layer1 = nn.Linear(input_dim, 50)
|
||||
self.layer2 = nn.Linear(50, 50)
|
||||
self.layer3 = nn.Linear(50, 3)
|
||||
|
||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
||||
x = F.relu(self.layer1(x))
|
||||
x = F.relu(self.layer2(x))
|
||||
x = F.softmax(self.layer3(x), dim=1)
|
||||
return x
|
7
projects/torch-iris/container/src/requirements.txt
Normal file
7
projects/torch-iris/container/src/requirements.txt
Normal file
@ -0,0 +1,7 @@
|
||||
quart==0.19.4
|
||||
infernet_ml==0.1.0
|
||||
PyArweave @ git+https://github.com/ritual-net/pyarweave.git
|
||||
huggingface-hub==0.17.3
|
||||
sk2torch==1.2.0
|
||||
torch==2.1.2
|
||||
web3==6.15.0
|
Reference in New Issue
Block a user