Skip to content

Commit 085d859

Browse files
Add example for text2image (#920)
Signed-off-by: Ye, Xinyu <xinyu.ye@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 15cc457 commit 085d859

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

Text2Image/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Text-to-Image Microservice
2+
3+
Text-to-Image is a task that generate image conditioning on the provided text. This microservice supports text-to-image task by using Stable Diffusion (SD) model.
4+
5+
## Deploy Text-to-Image Service
6+
7+
### Deploy Text-to-Image Service on Xeon
8+
9+
Refer to the [Xeon Guide](./docker_compose/intel/cpu/xeon/README.md) for detail.
10+
11+
### Deploy Text-to-Image Service on Gaudi
12+
13+
Refer to the [Gaudi Guide](./docker_compose/intel/hpu/gaudi/README.md) for detail.
14+
15+
## Consume Text-to-Image Service
16+
17+
Use below command to generate image.
18+
19+
```bash
20+
http_proxy="" curl http://localhost:9379/v1/text2image -XPOST -d '{"prompt":"An astronaut riding a green horse", "num_images_per_prompt":1}' -H 'Content-Type: application/json'
21+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Deploy Text-to-Image Service on Xeon
2+
3+
This document outlines the deployment process for a text-to-image service utilizing the [GenAIComps](https://github.com/opea-project/GenAIComps.git) microservice on Intel Xeon server. The steps include Docker image creation, container deployment. We will publish the Docker images to Docker Hub, it will simplify the deployment process for this service.
4+
5+
## 🚀 Build Docker Images
6+
7+
First of all, you need to build Docker Images locally. This step can be ignored after the Docker images published to Docker hub.
8+
9+
### 1. Build Docker Image
10+
11+
Build text-to-image service image on Xeon with below command:
12+
13+
```bash
14+
git clone https://github.com/opea-project/GenAIComps.git
15+
cd GenAIComps
16+
docker build -t opea/text2image:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/text2image/Dockerfile .
17+
```
18+
19+
### 2. Run Docker with CLI
20+
21+
Select Stable Diffusion (SD) model and assign its name to a environment variable as below:
22+
23+
```bash
24+
# SD1.5
25+
export MODEL=stable-diffusion-v1-5/stable-diffusion-v1-5
26+
# SD2.1
27+
export MODEL=stabilityai/stable-diffusion-2-1
28+
# SDXL
29+
export MODEL=stabilityai/stable-diffusion-xl-base-1.0
30+
# SD3
31+
export MODEL=stabilityai/stable-diffusion-3-medium-diffusers
32+
```
33+
34+
Set huggingface token:
35+
36+
```bash
37+
export HF_TOKEN=<your huggingface token>
38+
```
39+
40+
Start text-to-image service on Xeon with below command:
41+
42+
```bash
43+
docker run --ipc=host -p 9379:9379 -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e HF_TOKEN=$HF_TOKEN -e MODEL=$MODEL opea/text2image:latest
44+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Deploy Text-to-Image Service on Gaudi
2+
3+
This document outlines the deployment process for a text-to-image service utilizing the [GenAIComps](https://github.com/opea-project/GenAIComps.git) microservice on Intel Xeon server. The steps include Docker image creation, container deployment. We will publish the Docker images to Docker Hub, it will simplify the deployment process for this service.
4+
5+
## 🚀 Build Docker Images
6+
7+
First of all, you need to build Docker Images locally. This step can be ignored after the Docker images published to Docker hub.
8+
9+
### 1. Build Docker Image
10+
11+
Build text-to-image service image on Gaudi with below command:
12+
13+
```bash
14+
git clone https://github.com/opea-project/GenAIComps.git
15+
cd GenAIComps
16+
docker build -t opea/text2image-gaudi:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/text2image/Dockerfile.intel_hpu .
17+
```
18+
19+
### 2. Run Docker with CLI
20+
21+
Select Stable Diffusion (SD) model and assign its name to a environment variable as below:
22+
23+
```bash
24+
# SD1.5
25+
export MODEL=stable-diffusion-v1-5/stable-diffusion-v1-5
26+
# SD2.1
27+
export MODEL=stabilityai/stable-diffusion-2-1
28+
# SDXL
29+
export MODEL=stabilityai/stable-diffusion-xl-base-1.0
30+
# SD3
31+
export MODEL=stabilityai/stable-diffusion-3-medium-diffusers
32+
```
33+
34+
Set huggingface token:
35+
36+
```bash
37+
export HF_TOKEN=<your huggingface token>
38+
```
39+
40+
Start text-to-image service on Gaudi with below command:
41+
42+
```bash
43+
docker run -p 9379:9379 --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e HF_TOKEN=$HF_TOKEN -e MODEL=$MODEL opea/text2image-gaudi:latest
44+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
services:
5+
text2image:
6+
build:
7+
args:
8+
http_proxy: ${http_proxy}
9+
https_proxy: ${https_proxy}
10+
no_proxy: ${no_proxy}
11+
context: GenAIComps
12+
dockerfile: comps/text2image/Dockerfile
13+
image: ${REGISTRY:-opea}/text2image:${TAG:-latest}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
set -x
5+
IMAGE_REPO=${IMAGE_REPO:-"opea"}
6+
IMAGE_TAG=${IMAGE_TAG:-"latest"}
7+
echo "REGISTRY=IMAGE_REPO=${IMAGE_REPO}"
8+
echo "TAG=IMAGE_TAG=${IMAGE_TAG}"
9+
export REGISTRY=${IMAGE_REPO}
10+
export TAG=${IMAGE_TAG}
11+
12+
WORKPATH=$(dirname "$PWD")
13+
LOG_PATH="$WORKPATH/tests"
14+
ip_address=$(hostname -I | awk '{print $1}')
15+
text2image_service_port=9379
16+
MODEL=stabilityai/stable-diffusion-2-1
17+
18+
function build_docker_images() {
19+
cd $WORKPATH/docker_image_build
20+
if [ ! -d "GenAIComps" ] ; then
21+
git clone https://github.com/opea-project/GenAIComps.git
22+
fi
23+
docker compose -f build.yaml build --no-cache > ${LOG_PATH}/docker_image_build.log
24+
}
25+
26+
function start_service() {
27+
export no_proxy="localhost,127.0.0.1,"${ip_address}
28+
docker run -d --name="text2image-server" -p $text2image_service_port:$text2image_service_port --runtime=runc --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e MODEL=$MODEL -e no_proxy=$no_proxy ${IMAGE_REPO}/text2image:${IMAGE_TAG}
29+
sleep 30s
30+
}
31+
32+
function validate_microservice() {
33+
cd $LOG_PATH
34+
export no_proxy="localhost,127.0.0.1,"${ip_address}
35+
36+
# test /v1/text2image generate image
37+
URL="http://${ip_address}:$text2image_service_port/v1/text2image"
38+
HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X POST -d '{"prompt":"An astronaut riding a green horse", "num_images_per_prompt":1}' -H 'Content-Type: application/json' "$URL")
39+
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
40+
RESPONSE_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
41+
SERVICE_NAME="text2image-server - generate image"
42+
43+
if [ "$HTTP_STATUS" -ne "200" ]; then
44+
echo "[ $SERVICE_NAME ] HTTP status is not 200. Received status was $HTTP_STATUS"
45+
docker logs text2image-server >> ${LOG_PATH}/text2image-server_generate_image.log
46+
exit 1
47+
else
48+
echo "[ $SERVICE_NAME ] HTTP status is 200. Checking content..."
49+
fi
50+
# Check if the parsed values match the expected values
51+
if [[ $RESPONSE_BODY == *"images"* ]]; then
52+
echo "Content correct."
53+
else
54+
echo "Content wrong."
55+
docker logs text2image-server >> ${LOG_PATH}/text2image-server_generate_image.log
56+
exit 1
57+
fi
58+
}
59+
60+
function stop_docker() {
61+
cid=$(docker ps -aq --filter "name=text2image-server*")
62+
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
63+
}
64+
65+
function main() {
66+
67+
stop_docker
68+
69+
build_docker_images
70+
start_service
71+
72+
validate_microservice
73+
74+
stop_docker
75+
echo y | docker system prune
76+
77+
}
78+
79+
main

0 commit comments

Comments
 (0)