Skip to content

Commit

Permalink
Merge pull request #41 from sbip-sg/examples
Browse files Browse the repository at this point in the history
Reorganize examples for SDK with docs
  • Loading branch information
jeryongchan authored Jan 3, 2024
2 parents 3104552 + 7a36544 commit 15db958
Show file tree
Hide file tree
Showing 24 changed files with 1,044 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PORT = 1212
# DID_VERIFIER_SERVICE_API_URL=http://localhost:8080
# DID_ISSUER_SERVICE_API_URL=http://localhost:9090
# PAYMENT_SERVICE_API_URL=http://localhost:7002
# TEST_SERVICE_API_URL=http://localhost:8000
# TRANSACTION_SERVICE_API_URL=http://localhost:8000

# MQ_URL=amqp://localhost:5672
TASK_EXECUTOR_URL=http://localhost:2591
Expand Down
16 changes: 10 additions & 6 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@ This SDK only provides offloading asynchronously and receiving results via callb

### Test

Start desktop app, docker daemon, executor and build dockerized task with
Start desktop app, docker daemon, executor, go to example_containers and build dockerized task with
```
docker build -t test-offload -f containerized_example/Dockerfile.example containerized_example
docker build -t <tag> example_containers/knn
```
Start virtual env, install requirements and run test-offload.py.
Go to knn.py and change the container_ref to the tag you just built.
Start virtual env, install requirements and run knn.py.

```
pip install torch==2.0.1+cpu --index-url https://download.pytorch.org/whl/cpu
pip install -r requirements.txt
python knn.py
```

### Examples

[test_basic_offload.py](test_basic_offload.py)
This folder contains examples of how to use the SDK to offload tasks. The example_containers folder contains example tasks that can be called and run by the host.

[test-offload.py](test-offload.py)
- [basic_offload.py](basic_offload.py) uses the [sampleserver](../task_executor/sample/) container.

- [knn.py](knn.py) uses the [knn](example_containers/knn) container.

## JavaScript

Not updated
Not in use
File renamed without changes.
51 changes: 51 additions & 0 deletions sdk/example_containers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Example task containers

This folder contains example task containers showing how to build your own task container. Each folder contains a Dockerfile and an entrypoint file that handles the request, which is all that is needed for building the task container.

## Building a task container

You only need 2 files.

### 1. App that handles requests

The app will receive a post request at `localhost:8080\` and return the result. The input format is any json object and the output format is a string. After the app starts up, print `"meca-init-done"`.

For example, we create a flask app:

```python
from flask import Flask, request
import json

app = Flask(__name__)

@app.route('/', methods=['POST'])
def hello_world():
data = request.json
return data['input']

print("meca-init-done")
```

### 2. Dockerfile

Expose port 8080 to the app of the request handler.

Following the flask app created above, the Dockerfile may look like this:

```dockerfile
FROM python:3.9-slim

WORKDIR /app

COPY flask_app.py flask_app.py

EXPOSE 8080

CMD ["python", "-m", "flask", "--app", "flask_app.py", "run", "--host=0.0.0.0", "--port=8080"]
```

### 3. Using the container

Build the image and push it to a image repository that the host has access to (which may be public).

Call the meca api with the image name.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ COPY requirements.txt requirements.txt
RUN pip install torch==2.0.1+cpu --index-url https://download.pytorch.org/whl/cpu
RUN pip install -r requirements.txt

COPY test-docker.py test-docker.py
COPY app.py app.py

EXPOSE 8080

CMD ["python", "-m", "flask", "--app", "test-docker.py", "run", "--host=0.0.0.0", "--port=8080"]
CMD ["python", "-m", "flask", "--app", "app.py", "run", "--host=0.0.0.0", "--port=8080"]
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions sdk/example_containers/stablediffusion/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv/
136 changes: 136 additions & 0 deletions sdk/example_containers/stablediffusion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/out/
output.png

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

tmp/
.idea
result.png
17 changes: 17 additions & 0 deletions sdk/example_containers/stablediffusion/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.9.0
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install openvino-dev[onnx,pytorch]==2022.3.0
RUN pip install -r requirements.txt
RUN pip install flask
RUN pip install flask-restful
RUN apt-get update && apt-get install -y python3-opencv
RUN pip install opencv-python
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash
RUN apt-get install -y git-lfs
RUN git lfs install
RUN git clone https://huggingface.co/bes-dev/stable-diffusion-v1-4-openvino
COPY . .
EXPOSE 8080
CMD ["flask", "run", "--host=0.0.0.0", "--port=8080"]

Loading

0 comments on commit 15db958

Please sign in to comment.