feat: publishing infernet-container-starter v0.2.0
This commit is contained in:
3
projects/prompt-to-nft/container/.gitignore
vendored
Normal file
3
projects/prompt-to-nft/container/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
wallet
|
||||
config.json
|
||||
**/keyfile-arweave.json
|
27
projects/prompt-to-nft/container/Dockerfile
Normal file
27
projects/prompt-to-nft/container/Dockerfile
Normal file
@ -0,0 +1,27 @@
|
||||
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 wallet wallet
|
||||
|
||||
COPY src src
|
||||
|
||||
ENTRYPOINT ["hypercorn", "app:create_app()"]
|
||||
CMD ["-b", "0.0.0.0:3000"]
|
23
projects/prompt-to-nft/container/Makefile
Normal file
23
projects/prompt-to-nft/container/Makefile
Normal file
@ -0,0 +1,23 @@
|
||||
DOCKER_ORG := ritualnetwork
|
||||
EXAMPLE_NAME := prompt-to-nft
|
||||
TAG := $(DOCKER_ORG)/example-$(EXAMPLE_NAME)-infernet:latest
|
||||
|
||||
.phony: build run build-multiplatform
|
||||
|
||||
build:
|
||||
ifdef CI
|
||||
mkdir -p wallet # in CI we don't have a wallet directory. This enables to bypass that and ensure that the image
|
||||
# is built successfully
|
||||
endif
|
||||
@docker build -t $(TAG) .
|
||||
|
||||
wallet_dir ?= /app/wallet
|
||||
|
||||
run:
|
||||
docker run -p 3000:3000 -v ./wallet:$(wallet_dir) --env-file prompt_to_nft.env $(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 .
|
91
projects/prompt-to-nft/container/README.md
Normal file
91
projects/prompt-to-nft/container/README.md
Normal file
@ -0,0 +1,91 @@
|
||||
# Prompt-to-NFT Container
|
||||
|
||||
|
||||
## Overview
|
||||
|
||||
|
||||
## 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.
|
||||
|
||||
### Adding Arweave File
|
||||
Add your arweave wallet file
|
||||
|
||||
|
||||
|
||||
### Running the Container
|
||||
|
||||
To run the container, you can use the following command:
|
||||
|
||||
```bash
|
||||
make run
|
||||
```
|
||||
|
||||
## Testing the Container
|
||||
|
||||
Run the following command to run an inference:
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:3000/service_output \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"source":1, "data": {"prompt": "a golden retriever skiing"}}'
|
||||
```
|
||||
|
||||
#### 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
|
||||
[
|
||||
[
|
||||
[
|
||||
0.0010151526657864451,
|
||||
0.014391022734344006,
|
||||
0.9845937490463257
|
||||
]
|
||||
]
|
||||
]
|
||||
```
|
||||
|
||||
The response 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.9845937490463257`(~98.5%).
|
53
projects/prompt-to-nft/container/config.sample.json
Normal file
53
projects/prompt-to-nft/container/config.sample.json
Normal file
@ -0,0 +1,53 @@
|
||||
{
|
||||
"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": "prompt-to-nft",
|
||||
"image": "ritualnetwork/example-prompt-to-nft-infernet:latest",
|
||||
"external": true,
|
||||
"port": "3000",
|
||||
"allowed_delegate_addresses": [],
|
||||
"allowed_addresses": [],
|
||||
"allowed_ips": [],
|
||||
"command": "--bind=0.0.0.0:3000 --workers=2",
|
||||
"env": {
|
||||
"ARWEAVE_WALLET_FILE_PATH": "wallet/keyfile-arweave.json",
|
||||
"IMAGE_GEN_SERVICE_URL": "http://your.services.ip:port"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "anvil-node",
|
||||
"image": "ritualnetwork/infernet-anvil:0.0.0",
|
||||
"external": true,
|
||||
"port": "8545",
|
||||
"allowed_delegate_addresses": [],
|
||||
"allowed_addresses": [],
|
||||
"allowed_ips": [],
|
||||
"command": "",
|
||||
"env": {}
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
ARWEAVE_WALLET_FILE_PATH=
|
||||
IMAGE_GEN_SERVICE_URL=
|
0
projects/prompt-to-nft/container/src/__init__.py
Normal file
0
projects/prompt-to-nft/container/src/__init__.py
Normal file
109
projects/prompt-to-nft/container/src/app.py
Normal file
109
projects/prompt-to-nft/container/src/app.py
Normal file
@ -0,0 +1,109 @@
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
|
||||
import aiohttp
|
||||
from eth_abi import decode, encode # type: ignore
|
||||
from infernet_ml.utils.arweave import upload, load_wallet
|
||||
from infernet_ml.utils.service_models import InfernetInput, InfernetInputSource
|
||||
from quart import Quart, request
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def run_inference(prompt: str, output_path: str) -> None:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
app_url = os.getenv("IMAGE_GEN_SERVICE_URL")
|
||||
async with session.post(
|
||||
f"{app_url}/service_output",
|
||||
json={
|
||||
"prompt": prompt,
|
||||
},
|
||||
) as response:
|
||||
image_bytes = await response.read()
|
||||
with open(output_path, "wb") as f:
|
||||
f.write(image_bytes)
|
||||
|
||||
|
||||
def ensure_env_vars() -> None:
|
||||
if not os.getenv("IMAGE_GEN_SERVICE_URL"):
|
||||
raise ValueError("IMAGE_GEN_SERVICE_URL environment variable not set")
|
||||
load_wallet()
|
||||
|
||||
|
||||
def create_app() -> Quart:
|
||||
app = Quart(__name__)
|
||||
ensure_env_vars()
|
||||
|
||||
@app.route("/")
|
||||
def index() -> str:
|
||||
"""
|
||||
Utility endpoint to check if the service is running.
|
||||
"""
|
||||
return "Stable Diffusion Example Program"
|
||||
|
||||
@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)
|
||||
temp_file = "image.png"
|
||||
|
||||
if infernet_input.source == InfernetInputSource.OFFCHAIN:
|
||||
prompt: str = cast(dict[str, str], infernet_input.data)["prompt"]
|
||||
else:
|
||||
# On-chain requests are sent as a generalized hex-string which we will
|
||||
# decode to the appropriate format.
|
||||
(prompt, mintTo) = decode(
|
||||
["string", "address"], bytes.fromhex(cast(str, infernet_input.data))
|
||||
)
|
||||
log.info("mintTo: %s", mintTo)
|
||||
log.info("prompt: %s", prompt)
|
||||
|
||||
# run the inference and download the image to a temp file
|
||||
await run_inference(prompt, temp_file)
|
||||
|
||||
tx = upload(Path(temp_file), {"Content-Type": "image/png"})
|
||||
|
||||
if infernet_input.source == InfernetInputSource.OFFCHAIN:
|
||||
"""
|
||||
In case of an off-chain request, the result is returned as is.
|
||||
"""
|
||||
return {
|
||||
"prompt": prompt,
|
||||
"hash": tx.id,
|
||||
"image_url": f"https://arweave.net/{tx.id}",
|
||||
}
|
||||
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.
|
||||
"""
|
||||
return {
|
||||
"raw_input": infernet_input.data,
|
||||
"processed_input": "",
|
||||
"raw_output": encode(["string"], [tx.id]).hex(),
|
||||
"processed_output": "",
|
||||
"proof": "",
|
||||
}
|
||||
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""
|
||||
Utility to run the app locally. For development purposes only.
|
||||
"""
|
||||
create_app().run(host="0.0.0.0", port=3000)
|
5
projects/prompt-to-nft/container/src/requirements.txt
Normal file
5
projects/prompt-to-nft/container/src/requirements.txt
Normal file
@ -0,0 +1,5 @@
|
||||
quart==0.19.4
|
||||
infernet_ml==0.1.0
|
||||
PyArweave @ git+https://github.com/ritual-net/pyarweave.git
|
||||
web3==6.15.0
|
||||
tqdm==4.66.1
|
Reference in New Issue
Block a user